Datasets:
tools(metadata): make warnings visible and robust summary extraction
Browse files- tools/02_generate_metadata.py +47 -17
tools/02_generate_metadata.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
#!/usr/bin/env -S uv run
|
| 2 |
"""
|
| 3 |
Collect relevant metadata from local UD directories:
|
| 4 |
-
- extracting
|
| 5 |
-
|
| 6 |
- using the UD directory name for collecting metadata from the
|
| 7 |
codes_and_flags.yaml file
|
| 8 |
- collecting {dev,train,test}.conllu files.
|
|
@@ -13,6 +13,7 @@ import os
|
|
| 13 |
import xml.etree.ElementTree as ET
|
| 14 |
import argparse
|
| 15 |
import logging
|
|
|
|
| 16 |
from collections import defaultdict
|
| 17 |
from pathlib import Path
|
| 18 |
|
|
@@ -39,12 +40,16 @@ UD_VER = args.ud_ver
|
|
| 39 |
|
| 40 |
# Set up logging
|
| 41 |
logging.basicConfig(
|
| 42 |
-
level = max(logging.DEBUG, logging.
|
| 43 |
format='%(asctime)s [%(levelname)s] %(message)s',
|
| 44 |
-
datefmt='%Y-%m-%d %H:%M:%S'
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
GENUS_SEPARATED_FROM_UD_VERSION = (2, 15)
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def parse_ud_version(version: str) -> tuple[int, ...] | None:
|
|
@@ -82,6 +87,35 @@ def normalize_codes_and_flags_entry(entry: dict, ud_version: str) -> dict:
|
|
| 82 |
return normalized
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def extract_metadata(file_path) -> {}:
|
| 86 |
"""
|
| 87 |
Collect relevant metadata from UD directories.
|
|
@@ -114,22 +148,18 @@ def extract_metadata(file_path) -> {}:
|
|
| 114 |
|
| 115 |
with open(file_path, 'r') as file:
|
| 116 |
lines = [line.strip() for line in file.readlines()]
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if summary_start and summary_end:
|
| 127 |
-
# we have a summary
|
| 128 |
-
metadata["summary"] = (' '.join(lines[summary_start:summary_end])).strip()
|
| 129 |
|
| 130 |
# This is a (quite hackish) approach inspired by:
|
| 131 |
# https://github.com/UniversalDependencies/LICENSE/blob/master/generate_license_for_lindat.pl
|
| 132 |
-
for line in lines[
|
| 133 |
if ":" in line:
|
| 134 |
key, val = line.split(":", 1)
|
| 135 |
if key.lower() in metadata.keys():
|
|
|
|
| 1 |
#!/usr/bin/env -S uv run
|
| 2 |
"""
|
| 3 |
Collect relevant metadata from local UD directories:
|
| 4 |
+
- extracting a markdown Summary section and machine-readable metadata
|
| 5 |
+
from README.{md,txt}
|
| 6 |
- using the UD directory name for collecting metadata from the
|
| 7 |
codes_and_flags.yaml file
|
| 8 |
- collecting {dev,train,test}.conllu files.
|
|
|
|
| 13 |
import xml.etree.ElementTree as ET
|
| 14 |
import argparse
|
| 15 |
import logging
|
| 16 |
+
import re
|
| 17 |
from collections import defaultdict
|
| 18 |
from pathlib import Path
|
| 19 |
|
|
|
|
| 40 |
|
| 41 |
# Set up logging
|
| 42 |
logging.basicConfig(
|
| 43 |
+
level = max(logging.DEBUG, logging.WARNING - args.verbose * 10),
|
| 44 |
format='%(asctime)s [%(levelname)s] %(message)s',
|
| 45 |
+
datefmt='%Y-%m-%d %H:%M:%S',
|
| 46 |
+
force=True
|
| 47 |
)
|
| 48 |
|
| 49 |
GENUS_SEPARATED_FROM_UD_VERSION = (2, 15)
|
| 50 |
+
SUMMARY_HEADING_SCAN_LIMIT = 120
|
| 51 |
+
SUMMARY_HEADING_RE = re.compile(r"^#{1,2}\s+summary\s*$", re.IGNORECASE)
|
| 52 |
+
MARKDOWN_HEADING_RE = re.compile(r"^#{1,6}\s+\S")
|
| 53 |
|
| 54 |
|
| 55 |
def parse_ud_version(version: str) -> tuple[int, ...] | None:
|
|
|
|
| 87 |
return normalized
|
| 88 |
|
| 89 |
|
| 90 |
+
def find_summary_bounds(lines: list[str]) -> tuple[int, int] | None:
|
| 91 |
+
"""
|
| 92 |
+
Find the markdown Summary section bounds.
|
| 93 |
+
|
| 94 |
+
We look for an H1/H2 heading named "Summary" near the beginning
|
| 95 |
+
of the file and collect lines until the next markdown heading.
|
| 96 |
+
Returns (start_index, end_index_exclusive) for summary content lines.
|
| 97 |
+
"""
|
| 98 |
+
scan_limit = min(len(lines), SUMMARY_HEADING_SCAN_LIMIT)
|
| 99 |
+
summary_heading_idx = None
|
| 100 |
+
|
| 101 |
+
for i in range(scan_limit):
|
| 102 |
+
line = lines[i].strip()
|
| 103 |
+
if SUMMARY_HEADING_RE.match(line):
|
| 104 |
+
summary_heading_idx = i
|
| 105 |
+
break
|
| 106 |
+
|
| 107 |
+
if summary_heading_idx is None:
|
| 108 |
+
return None
|
| 109 |
+
|
| 110 |
+
start = summary_heading_idx + 1
|
| 111 |
+
end = len(lines)
|
| 112 |
+
for i in range(start, len(lines)):
|
| 113 |
+
if MARKDOWN_HEADING_RE.match(lines[i].strip()):
|
| 114 |
+
end = i
|
| 115 |
+
break
|
| 116 |
+
return (start, end)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
def extract_metadata(file_path) -> {}:
|
| 120 |
"""
|
| 121 |
Collect relevant metadata from UD directories.
|
|
|
|
| 148 |
|
| 149 |
with open(file_path, 'r') as file:
|
| 150 |
lines = [line.strip() for line in file.readlines()]
|
| 151 |
+
summary_bounds = find_summary_bounds(lines)
|
| 152 |
+
metadata_scan_start = 0
|
| 153 |
+
if summary_bounds is not None:
|
| 154 |
+
summary_start, summary_end = summary_bounds
|
| 155 |
+
metadata_scan_start = summary_end
|
| 156 |
+
summary = (' '.join(lines[summary_start:summary_end])).strip()
|
| 157 |
+
if summary:
|
| 158 |
+
metadata["summary"] = summary
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
# This is a (quite hackish) approach inspired by:
|
| 161 |
# https://github.com/UniversalDependencies/LICENSE/blob/master/generate_license_for_lindat.pl
|
| 162 |
+
for line in lines[metadata_scan_start:]:
|
| 163 |
if ":" in line:
|
| 164 |
key, val = line.split(":", 1)
|
| 165 |
if key.lower() in metadata.keys():
|