Datasets:
Upload TypePrediction dataset
Browse files- .gitattributes +1 -0
- README.md +83 -0
- build_type_prediction_dataset.py +372 -0
- push_to_hf.py +52 -0
- summary.json +166 -0
- type_predictor_data.jsonl +3 -0
.gitattributes
CHANGED
|
@@ -58,3 +58,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 58 |
# Video files - compressed
|
| 59 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 60 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 58 |
# Video files - compressed
|
| 59 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 60 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
| 61 |
+
type_predictor_data.jsonl filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TypePrediction
|
| 2 |
+
|
| 3 |
+
Unified mention-level type prediction data built from all available local Wojood NER datasets except `WojoodRelations`.
|
| 4 |
+
|
| 5 |
+
## Main file
|
| 6 |
+
|
| 7 |
+
- `type_predictor_data.jsonl`
|
| 8 |
+
|
| 9 |
+
Each row contains:
|
| 10 |
+
|
| 11 |
+
```json
|
| 12 |
+
{
|
| 13 |
+
"entity": "النص",
|
| 14 |
+
"type": "ORG",
|
| 15 |
+
"sentence": "الجملة الكاملة",
|
| 16 |
+
"start_token": 0,
|
| 17 |
+
"end_token": 1,
|
| 18 |
+
"start_char": 0,
|
| 19 |
+
"end_char": 10,
|
| 20 |
+
"raw_types": ["ORG", "NONGOV"],
|
| 21 |
+
"sources": [
|
| 22 |
+
{"dataset": "WojoodFine", "split": "train"},
|
| 23 |
+
{"dataset": "Wojood1_1_nested", "split": "train"}
|
| 24 |
+
]
|
| 25 |
+
}
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## Construction
|
| 29 |
+
|
| 30 |
+
1. Parse all sentence-separated CoNLL-style files from:
|
| 31 |
+
- `Wojood1_1_flat`
|
| 32 |
+
- `Wojood1_1_nested`
|
| 33 |
+
- `WojoodFine`
|
| 34 |
+
- `WojoodFine-Flat`
|
| 35 |
+
2. Extract every entity span, including nested spans when multiple BIO tags are present on the same token sequence.
|
| 36 |
+
3. Map fine labels to coarse labels using `wojood_ontology.json`.
|
| 37 |
+
4. Reconstruct full sentence text and both token and character spans.
|
| 38 |
+
5. Deduplicate by `(sentence, entity, type, start_token, end_token)` and merge provenance.
|
| 39 |
+
|
| 40 |
+
## Summary
|
| 41 |
+
|
| 42 |
+
- total rows: `132763`
|
| 43 |
+
- unique sentences: `34187`
|
| 44 |
+
- raw extracted mentions before dedupe: `408035`
|
| 45 |
+
- rows merged by dedupe: `275272`
|
| 46 |
+
|
| 47 |
+
## Source breakdown
|
| 48 |
+
|
| 49 |
+
- `Wojood1_1_flat/test`: 14573 mentions from 6606 sentences
|
| 50 |
+
- `Wojood1_1_flat/train`: 49959 mentions from 23125 sentences
|
| 51 |
+
- `Wojood1_1_flat/val`: 7143 mentions from 3304 sentences
|
| 52 |
+
- `Wojood1_1_nested/test`: 18045 mentions from 6606 sentences
|
| 53 |
+
- `Wojood1_1_nested/train`: 62377 mentions from 23125 sentences
|
| 54 |
+
- `Wojood1_1_nested/val`: 8944 mentions from 3304 sentences
|
| 55 |
+
- `WojoodFine/test`: 27848 mentions from 5748 sentences
|
| 56 |
+
- `WojoodFine/train`: 96188 mentions from 19484 sentences
|
| 57 |
+
- `WojoodFine/val`: 13800 mentions from 2828 sentences
|
| 58 |
+
- `WojoodFine-Flat/split10`: 10859 mentions from 3304 sentences
|
| 59 |
+
- `WojoodFine-Flat/split20`: 22208 mentions from 6606 sentences
|
| 60 |
+
- `WojoodFine-Flat/split70`: 76091 mentions from 23125 sentences
|
| 61 |
+
|
| 62 |
+
## Top coarse types
|
| 63 |
+
|
| 64 |
+
- `GPE`: 29992
|
| 65 |
+
- `ORG`: 29864
|
| 66 |
+
- `DATE`: 19765
|
| 67 |
+
- `PERS`: 10726
|
| 68 |
+
- `NORP`: 10220
|
| 69 |
+
- `ORDINAL`: 7817
|
| 70 |
+
- `OCC`: 7696
|
| 71 |
+
- `EVENT`: 3785
|
| 72 |
+
- `CARDINAL`: 3731
|
| 73 |
+
- `LOC`: 2418
|
| 74 |
+
- `FAC`: 1507
|
| 75 |
+
- `WEBSITE`: 1481
|
| 76 |
+
- `LAW`: 904
|
| 77 |
+
- `TIME`: 871
|
| 78 |
+
- `MONEY`: 465
|
| 79 |
+
- `CURR`: 456
|
| 80 |
+
- `LANGUAGE`: 332
|
| 81 |
+
- `PERCENT`: 312
|
| 82 |
+
- `PRODUCT`: 191
|
| 83 |
+
- `QUANTITY`: 115
|
build_type_prediction_dataset.py
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from collections import Counter, defaultdict
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
ROOT = Path("/root/knowledgegrapheval")
|
| 8 |
+
OUT_DIR = ROOT / "type_prediction_dataset"
|
| 9 |
+
OUT_PATH = OUT_DIR / "type_predictor_data.jsonl"
|
| 10 |
+
SUMMARY_PATH = OUT_DIR / "summary.json"
|
| 11 |
+
README_PATH = OUT_DIR / "README.md"
|
| 12 |
+
ONTOLOGY_PATH = ROOT / "data_preprocessed" / "wojood_ontology.json"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass(frozen=True)
|
| 16 |
+
class SourceSpec:
|
| 17 |
+
dataset: str
|
| 18 |
+
split: str
|
| 19 |
+
path: Path
|
| 20 |
+
mode: str
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
SOURCES = [
|
| 24 |
+
SourceSpec("Wojood1_1_flat", "train", ROOT / "Wojood" / "Wojood1_1_flat" / "train.txt", "space"),
|
| 25 |
+
SourceSpec("Wojood1_1_flat", "val", ROOT / "Wojood" / "Wojood1_1_flat" / "val.txt", "space"),
|
| 26 |
+
SourceSpec("Wojood1_1_flat", "test", ROOT / "Wojood" / "Wojood1_1_flat" / "test.txt", "space"),
|
| 27 |
+
SourceSpec("Wojood1_1_nested", "train", ROOT / "Wojood" / "Wojood1_1_nested" / "train.txt", "space"),
|
| 28 |
+
SourceSpec("Wojood1_1_nested", "val", ROOT / "Wojood" / "Wojood1_1_nested" / "val.txt", "space"),
|
| 29 |
+
SourceSpec("Wojood1_1_nested", "test", ROOT / "Wojood" / "Wojood1_1_nested" / "test.txt", "space"),
|
| 30 |
+
SourceSpec("WojoodFine", "train", ROOT / "WojoodFine" / "train.txt", "tab"),
|
| 31 |
+
SourceSpec("WojoodFine", "val", ROOT / "WojoodFine" / "val.txt", "tab"),
|
| 32 |
+
SourceSpec("WojoodFine", "test", ROOT / "WojoodFine" / "test.txt", "tab"),
|
| 33 |
+
SourceSpec("WojoodFine-Flat", "split70", ROOT / "WojoodFine-Flat" / "split70.conll", "tab"),
|
| 34 |
+
SourceSpec("WojoodFine-Flat", "split20", ROOT / "WojoodFine-Flat" / "split20.conll", "tab"),
|
| 35 |
+
SourceSpec("WojoodFine-Flat", "split10", ROOT / "WojoodFine-Flat" / "split10.conll", "tab"),
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def load_type_mapping() -> tuple[set[str], dict[str, str]]:
|
| 40 |
+
ontology = json.loads(ONTOLOGY_PATH.read_text(encoding="utf-8"))
|
| 41 |
+
coarse = {item["id"] for item in ontology["coarse_entity_types"]}
|
| 42 |
+
fine_to_coarse = {item["id"]: item["parent"] for item in ontology["fine_entity_types"]}
|
| 43 |
+
return coarse, fine_to_coarse
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def canonicalize_type_label(raw_type: str, coarse_types: set[str], fine_to_coarse: dict[str, str]) -> str:
|
| 47 |
+
candidates = [
|
| 48 |
+
raw_type,
|
| 49 |
+
raw_type.upper(),
|
| 50 |
+
raw_type.replace("-", "_"),
|
| 51 |
+
raw_type.upper().replace("-", "_"),
|
| 52 |
+
raw_type.replace("_", "-"),
|
| 53 |
+
raw_type.upper().replace("_", "-"),
|
| 54 |
+
]
|
| 55 |
+
known = coarse_types | set(fine_to_coarse)
|
| 56 |
+
for candidate in candidates:
|
| 57 |
+
if candidate in known:
|
| 58 |
+
return candidate
|
| 59 |
+
raise KeyError(raw_type)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def parse_line(line: str, mode: str) -> tuple[str, list[str]]:
|
| 63 |
+
if mode == "space":
|
| 64 |
+
parts = line.split()
|
| 65 |
+
token = parts[0]
|
| 66 |
+
tags = parts[1:]
|
| 67 |
+
elif mode == "tab":
|
| 68 |
+
parts = line.split("\t")
|
| 69 |
+
token = parts[0]
|
| 70 |
+
tags = []
|
| 71 |
+
for part in parts[1:]:
|
| 72 |
+
if not part:
|
| 73 |
+
continue
|
| 74 |
+
tags.extend(piece for piece in part.split() if piece)
|
| 75 |
+
else:
|
| 76 |
+
raise ValueError(f"Unknown parse mode: {mode}")
|
| 77 |
+
return token, tags
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def iter_sentences(spec: SourceSpec):
|
| 81 |
+
tokens: list[str] = []
|
| 82 |
+
tag_lists: list[list[str]] = []
|
| 83 |
+
with spec.path.open(encoding="utf-8") as handle:
|
| 84 |
+
for raw_line in handle:
|
| 85 |
+
line = raw_line.rstrip("\n\r")
|
| 86 |
+
if not line.strip():
|
| 87 |
+
if tokens:
|
| 88 |
+
yield tokens, tag_lists
|
| 89 |
+
tokens = []
|
| 90 |
+
tag_lists = []
|
| 91 |
+
continue
|
| 92 |
+
token, tags = parse_line(line, spec.mode)
|
| 93 |
+
tokens.append(token)
|
| 94 |
+
tag_lists.append(tags)
|
| 95 |
+
if tokens:
|
| 96 |
+
yield tokens, tag_lists
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def sentence_and_offsets(tokens: list[str]) -> tuple[str, list[int], list[int]]:
|
| 100 |
+
chars = []
|
| 101 |
+
starts = []
|
| 102 |
+
ends = []
|
| 103 |
+
pos = 0
|
| 104 |
+
for idx, token in enumerate(tokens):
|
| 105 |
+
if idx > 0:
|
| 106 |
+
chars.append(" ")
|
| 107 |
+
pos += 1
|
| 108 |
+
starts.append(pos)
|
| 109 |
+
chars.append(token)
|
| 110 |
+
pos += len(token)
|
| 111 |
+
ends.append(pos)
|
| 112 |
+
return "".join(chars), starts, ends
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def close_span(active, spans, token_idx):
|
| 116 |
+
if active is None:
|
| 117 |
+
return None
|
| 118 |
+
active["end_token"] = token_idx - 1
|
| 119 |
+
spans.append(active)
|
| 120 |
+
return None
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def extract_raw_spans(tokens: list[str], tag_lists: list[list[str]]) -> list[dict]:
|
| 124 |
+
spans = []
|
| 125 |
+
active_by_raw_type: dict[str, dict] = {}
|
| 126 |
+
|
| 127 |
+
for idx, tags in enumerate(tag_lists):
|
| 128 |
+
parsed_tags = []
|
| 129 |
+
seen_raw = set()
|
| 130 |
+
for tag in tags:
|
| 131 |
+
if tag == "O":
|
| 132 |
+
continue
|
| 133 |
+
if "-" not in tag:
|
| 134 |
+
continue
|
| 135 |
+
prefix, raw_type = tag.split("-", 1)
|
| 136 |
+
if raw_type in seen_raw:
|
| 137 |
+
continue
|
| 138 |
+
seen_raw.add(raw_type)
|
| 139 |
+
parsed_tags.append((prefix, raw_type))
|
| 140 |
+
|
| 141 |
+
current_raw = {raw_type for _, raw_type in parsed_tags}
|
| 142 |
+
for raw_type in list(active_by_raw_type):
|
| 143 |
+
if raw_type not in current_raw:
|
| 144 |
+
active_by_raw_type[raw_type] = close_span(active_by_raw_type[raw_type], spans, idx)
|
| 145 |
+
if active_by_raw_type[raw_type] is None:
|
| 146 |
+
del active_by_raw_type[raw_type]
|
| 147 |
+
|
| 148 |
+
for prefix, raw_type in parsed_tags:
|
| 149 |
+
active = active_by_raw_type.get(raw_type)
|
| 150 |
+
if prefix == "B":
|
| 151 |
+
if active is not None:
|
| 152 |
+
active_by_raw_type[raw_type] = close_span(active, spans, idx)
|
| 153 |
+
active_by_raw_type[raw_type] = {
|
| 154 |
+
"raw_type": raw_type,
|
| 155 |
+
"start_token": idx,
|
| 156 |
+
"end_token": idx,
|
| 157 |
+
}
|
| 158 |
+
elif prefix == "I":
|
| 159 |
+
if active is None:
|
| 160 |
+
active_by_raw_type[raw_type] = {
|
| 161 |
+
"raw_type": raw_type,
|
| 162 |
+
"start_token": idx,
|
| 163 |
+
"end_token": idx,
|
| 164 |
+
}
|
| 165 |
+
else:
|
| 166 |
+
active["end_token"] = idx
|
| 167 |
+
else:
|
| 168 |
+
raise ValueError(f"Unknown BIO prefix: {prefix}")
|
| 169 |
+
|
| 170 |
+
final_idx = len(tokens)
|
| 171 |
+
for raw_type in list(active_by_raw_type):
|
| 172 |
+
active_by_raw_type[raw_type] = close_span(active_by_raw_type[raw_type], spans, final_idx)
|
| 173 |
+
if active_by_raw_type[raw_type] is None:
|
| 174 |
+
del active_by_raw_type[raw_type]
|
| 175 |
+
return spans
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def build_record(
|
| 179 |
+
sentence: str,
|
| 180 |
+
char_starts: list[int],
|
| 181 |
+
char_ends: list[int],
|
| 182 |
+
tokens: list[str],
|
| 183 |
+
span: dict,
|
| 184 |
+
coarse_types: set[str],
|
| 185 |
+
fine_to_coarse: dict[str, str],
|
| 186 |
+
spec: SourceSpec,
|
| 187 |
+
) -> dict:
|
| 188 |
+
raw_type = canonicalize_type_label(span["raw_type"], coarse_types, fine_to_coarse)
|
| 189 |
+
coarse_type = raw_type if raw_type in coarse_types else fine_to_coarse[raw_type]
|
| 190 |
+
start_token = span["start_token"]
|
| 191 |
+
end_token = span["end_token"]
|
| 192 |
+
start_char = char_starts[start_token]
|
| 193 |
+
end_char = char_ends[end_token]
|
| 194 |
+
entity = sentence[start_char:end_char]
|
| 195 |
+
if entity != " ".join(tokens[start_token : end_token + 1]):
|
| 196 |
+
raise ValueError("Entity surface mismatch during reconstruction.")
|
| 197 |
+
return {
|
| 198 |
+
"entity": entity,
|
| 199 |
+
"type": coarse_type,
|
| 200 |
+
"sentence": sentence,
|
| 201 |
+
"start_token": start_token,
|
| 202 |
+
"end_token": end_token,
|
| 203 |
+
"start_char": start_char,
|
| 204 |
+
"end_char": end_char,
|
| 205 |
+
"raw_type": raw_type,
|
| 206 |
+
"source_dataset": spec.dataset,
|
| 207 |
+
"source_split": spec.split,
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def make_readme(summary: dict) -> str:
|
| 212 |
+
sources = "\n".join(
|
| 213 |
+
f"- `{item['dataset']}/{item['split']}`: {item['mentions']} mentions from {item['sentences']} sentences"
|
| 214 |
+
for item in summary["source_breakdown"]
|
| 215 |
+
)
|
| 216 |
+
top_types = "\n".join(
|
| 217 |
+
f"- `{item['type']}`: {item['count']}"
|
| 218 |
+
for item in summary["type_breakdown"][:20]
|
| 219 |
+
)
|
| 220 |
+
return f"""# TypePrediction
|
| 221 |
+
|
| 222 |
+
Unified mention-level type prediction data built from all available local Wojood NER datasets except `WojoodRelations`.
|
| 223 |
+
|
| 224 |
+
## Main file
|
| 225 |
+
|
| 226 |
+
- `type_predictor_data.jsonl`
|
| 227 |
+
|
| 228 |
+
Each row contains:
|
| 229 |
+
|
| 230 |
+
```json
|
| 231 |
+
{{
|
| 232 |
+
"entity": "النص",
|
| 233 |
+
"type": "ORG",
|
| 234 |
+
"sentence": "الجملة الكاملة",
|
| 235 |
+
"start_token": 0,
|
| 236 |
+
"end_token": 1,
|
| 237 |
+
"start_char": 0,
|
| 238 |
+
"end_char": 10,
|
| 239 |
+
"raw_types": ["ORG", "NONGOV"],
|
| 240 |
+
"sources": [
|
| 241 |
+
{{"dataset": "WojoodFine", "split": "train"}},
|
| 242 |
+
{{"dataset": "Wojood1_1_nested", "split": "train"}}
|
| 243 |
+
]
|
| 244 |
+
}}
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
## Construction
|
| 248 |
+
|
| 249 |
+
1. Parse all sentence-separated CoNLL-style files from:
|
| 250 |
+
- `Wojood1_1_flat`
|
| 251 |
+
- `Wojood1_1_nested`
|
| 252 |
+
- `WojoodFine`
|
| 253 |
+
- `WojoodFine-Flat`
|
| 254 |
+
2. Extract every entity span, including nested spans when multiple BIO tags are present on the same token sequence.
|
| 255 |
+
3. Map fine labels to coarse labels using `wojood_ontology.json`.
|
| 256 |
+
4. Reconstruct full sentence text and both token and character spans.
|
| 257 |
+
5. Deduplicate by `(sentence, entity, type, start_token, end_token)` and merge provenance.
|
| 258 |
+
|
| 259 |
+
## Summary
|
| 260 |
+
|
| 261 |
+
- total rows: `{summary['total_rows']}`
|
| 262 |
+
- unique sentences: `{summary['unique_sentences']}`
|
| 263 |
+
- raw extracted mentions before dedupe: `{summary['raw_mentions_before_dedupe']}`
|
| 264 |
+
- rows merged by dedupe: `{summary['duplicates_merged']}`
|
| 265 |
+
|
| 266 |
+
## Source breakdown
|
| 267 |
+
|
| 268 |
+
{sources}
|
| 269 |
+
|
| 270 |
+
## Top coarse types
|
| 271 |
+
|
| 272 |
+
{top_types}
|
| 273 |
+
"""
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def main():
|
| 277 |
+
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 278 |
+
coarse_types, fine_to_coarse = load_type_mapping()
|
| 279 |
+
|
| 280 |
+
grouped = {}
|
| 281 |
+
raw_mentions_before_dedupe = 0
|
| 282 |
+
source_sentence_counts = defaultdict(int)
|
| 283 |
+
source_mention_counts = defaultdict(int)
|
| 284 |
+
|
| 285 |
+
for spec in SOURCES:
|
| 286 |
+
for tokens, tag_lists in iter_sentences(spec):
|
| 287 |
+
source_sentence_counts[(spec.dataset, spec.split)] += 1
|
| 288 |
+
sentence, char_starts, char_ends = sentence_and_offsets(tokens)
|
| 289 |
+
spans = extract_raw_spans(tokens, tag_lists)
|
| 290 |
+
for span in spans:
|
| 291 |
+
raw_mentions_before_dedupe += 1
|
| 292 |
+
record = build_record(
|
| 293 |
+
sentence=sentence,
|
| 294 |
+
char_starts=char_starts,
|
| 295 |
+
char_ends=char_ends,
|
| 296 |
+
tokens=tokens,
|
| 297 |
+
span=span,
|
| 298 |
+
coarse_types=coarse_types,
|
| 299 |
+
fine_to_coarse=fine_to_coarse,
|
| 300 |
+
spec=spec,
|
| 301 |
+
)
|
| 302 |
+
source_mention_counts[(spec.dataset, spec.split)] += 1
|
| 303 |
+
key = (
|
| 304 |
+
record["sentence"],
|
| 305 |
+
record["entity"],
|
| 306 |
+
record["type"],
|
| 307 |
+
record["start_token"],
|
| 308 |
+
record["end_token"],
|
| 309 |
+
)
|
| 310 |
+
existing = grouped.get(key)
|
| 311 |
+
if existing is None:
|
| 312 |
+
grouped[key] = {
|
| 313 |
+
"entity": record["entity"],
|
| 314 |
+
"type": record["type"],
|
| 315 |
+
"sentence": record["sentence"],
|
| 316 |
+
"start_token": record["start_token"],
|
| 317 |
+
"end_token": record["end_token"],
|
| 318 |
+
"start_char": record["start_char"],
|
| 319 |
+
"end_char": record["end_char"],
|
| 320 |
+
"raw_types": [record["raw_type"]],
|
| 321 |
+
"sources": [
|
| 322 |
+
{
|
| 323 |
+
"dataset": record["source_dataset"],
|
| 324 |
+
"split": record["source_split"],
|
| 325 |
+
}
|
| 326 |
+
],
|
| 327 |
+
}
|
| 328 |
+
else:
|
| 329 |
+
if record["raw_type"] not in existing["raw_types"]:
|
| 330 |
+
existing["raw_types"].append(record["raw_type"])
|
| 331 |
+
source_entry = {
|
| 332 |
+
"dataset": record["source_dataset"],
|
| 333 |
+
"split": record["source_split"],
|
| 334 |
+
}
|
| 335 |
+
if source_entry not in existing["sources"]:
|
| 336 |
+
existing["sources"].append(source_entry)
|
| 337 |
+
|
| 338 |
+
rows = sorted(
|
| 339 |
+
grouped.values(),
|
| 340 |
+
key=lambda row: (row["sentence"], row["start_token"], row["end_token"], row["type"], row["entity"]),
|
| 341 |
+
)
|
| 342 |
+
with OUT_PATH.open("w", encoding="utf-8") as handle:
|
| 343 |
+
for row in rows:
|
| 344 |
+
handle.write(json.dumps(row, ensure_ascii=False) + "\n")
|
| 345 |
+
|
| 346 |
+
type_counts = Counter(row["type"] for row in rows)
|
| 347 |
+
summary = {
|
| 348 |
+
"total_rows": len(rows),
|
| 349 |
+
"unique_sentences": len({row["sentence"] for row in rows}),
|
| 350 |
+
"raw_mentions_before_dedupe": raw_mentions_before_dedupe,
|
| 351 |
+
"duplicates_merged": raw_mentions_before_dedupe - len(rows),
|
| 352 |
+
"source_breakdown": [
|
| 353 |
+
{
|
| 354 |
+
"dataset": dataset,
|
| 355 |
+
"split": split,
|
| 356 |
+
"sentences": source_sentence_counts[(dataset, split)],
|
| 357 |
+
"mentions": source_mention_counts[(dataset, split)],
|
| 358 |
+
}
|
| 359 |
+
for dataset, split in sorted(source_sentence_counts)
|
| 360 |
+
],
|
| 361 |
+
"type_breakdown": [
|
| 362 |
+
{"type": entity_type, "count": count}
|
| 363 |
+
for entity_type, count in type_counts.most_common()
|
| 364 |
+
],
|
| 365 |
+
}
|
| 366 |
+
SUMMARY_PATH.write_text(json.dumps(summary, ensure_ascii=False, indent=2), encoding="utf-8")
|
| 367 |
+
README_PATH.write_text(make_readme(summary), encoding="utf-8")
|
| 368 |
+
print(json.dumps(summary, ensure_ascii=False, indent=2))
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
if __name__ == "__main__":
|
| 372 |
+
main()
|
push_to_hf.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
from huggingface_hub import HfApi, create_repo
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
ROOT = Path("/root/knowledgegrapheval/type_prediction_dataset")
|
| 8 |
+
ENV_PATHS = [
|
| 9 |
+
Path("/root/knowledgegrapheval/.env"),
|
| 10 |
+
Path("/root/knowledge-graph-rag/.env"),
|
| 11 |
+
]
|
| 12 |
+
REPO_ID = "U4RASD/TypePrediction"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def load_env():
|
| 16 |
+
for env_path in ENV_PATHS:
|
| 17 |
+
if not env_path.exists():
|
| 18 |
+
continue
|
| 19 |
+
for line in env_path.read_text(encoding="utf-8").splitlines():
|
| 20 |
+
line = line.strip()
|
| 21 |
+
if not line or line.startswith("#") or "=" not in line:
|
| 22 |
+
continue
|
| 23 |
+
key, value = line.split("=", 1)
|
| 24 |
+
os.environ.setdefault(key.strip(), value.strip().strip('"').strip("'"))
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def main():
|
| 28 |
+
load_env()
|
| 29 |
+
token = os.environ.get("HF_TOKEN_UNIT") or os.environ.get("HF_TOKEN")
|
| 30 |
+
if not token:
|
| 31 |
+
raise RuntimeError("Missing HF token.")
|
| 32 |
+
|
| 33 |
+
create_repo(
|
| 34 |
+
repo_id=REPO_ID,
|
| 35 |
+
repo_type="dataset",
|
| 36 |
+
token=token,
|
| 37 |
+
exist_ok=True,
|
| 38 |
+
private=True,
|
| 39 |
+
)
|
| 40 |
+
api = HfApi(token=token)
|
| 41 |
+
api.upload_folder(
|
| 42 |
+
repo_id=REPO_ID,
|
| 43 |
+
repo_type="dataset",
|
| 44 |
+
folder_path=str(ROOT),
|
| 45 |
+
path_in_repo=".",
|
| 46 |
+
commit_message="Upload TypePrediction dataset",
|
| 47 |
+
)
|
| 48 |
+
print(REPO_ID)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
main()
|
summary.json
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"total_rows": 132763,
|
| 3 |
+
"unique_sentences": 34187,
|
| 4 |
+
"raw_mentions_before_dedupe": 408035,
|
| 5 |
+
"duplicates_merged": 275272,
|
| 6 |
+
"source_breakdown": [
|
| 7 |
+
{
|
| 8 |
+
"dataset": "Wojood1_1_flat",
|
| 9 |
+
"split": "test",
|
| 10 |
+
"sentences": 6606,
|
| 11 |
+
"mentions": 14573
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"dataset": "Wojood1_1_flat",
|
| 15 |
+
"split": "train",
|
| 16 |
+
"sentences": 23125,
|
| 17 |
+
"mentions": 49959
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"dataset": "Wojood1_1_flat",
|
| 21 |
+
"split": "val",
|
| 22 |
+
"sentences": 3304,
|
| 23 |
+
"mentions": 7143
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"dataset": "Wojood1_1_nested",
|
| 27 |
+
"split": "test",
|
| 28 |
+
"sentences": 6606,
|
| 29 |
+
"mentions": 18045
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"dataset": "Wojood1_1_nested",
|
| 33 |
+
"split": "train",
|
| 34 |
+
"sentences": 23125,
|
| 35 |
+
"mentions": 62377
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"dataset": "Wojood1_1_nested",
|
| 39 |
+
"split": "val",
|
| 40 |
+
"sentences": 3304,
|
| 41 |
+
"mentions": 8944
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"dataset": "WojoodFine",
|
| 45 |
+
"split": "test",
|
| 46 |
+
"sentences": 5748,
|
| 47 |
+
"mentions": 27848
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"dataset": "WojoodFine",
|
| 51 |
+
"split": "train",
|
| 52 |
+
"sentences": 19484,
|
| 53 |
+
"mentions": 96188
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"dataset": "WojoodFine",
|
| 57 |
+
"split": "val",
|
| 58 |
+
"sentences": 2828,
|
| 59 |
+
"mentions": 13800
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"dataset": "WojoodFine-Flat",
|
| 63 |
+
"split": "split10",
|
| 64 |
+
"sentences": 3304,
|
| 65 |
+
"mentions": 10859
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"dataset": "WojoodFine-Flat",
|
| 69 |
+
"split": "split20",
|
| 70 |
+
"sentences": 6606,
|
| 71 |
+
"mentions": 22208
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"dataset": "WojoodFine-Flat",
|
| 75 |
+
"split": "split70",
|
| 76 |
+
"sentences": 23125,
|
| 77 |
+
"mentions": 76091
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
"type_breakdown": [
|
| 81 |
+
{
|
| 82 |
+
"type": "GPE",
|
| 83 |
+
"count": 29992
|
| 84 |
+
},
|
| 85 |
+
{
|
| 86 |
+
"type": "ORG",
|
| 87 |
+
"count": 29864
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"type": "DATE",
|
| 91 |
+
"count": 19765
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"type": "PERS",
|
| 95 |
+
"count": 10726
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"type": "NORP",
|
| 99 |
+
"count": 10220
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"type": "ORDINAL",
|
| 103 |
+
"count": 7817
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"type": "OCC",
|
| 107 |
+
"count": 7696
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"type": "EVENT",
|
| 111 |
+
"count": 3785
|
| 112 |
+
},
|
| 113 |
+
{
|
| 114 |
+
"type": "CARDINAL",
|
| 115 |
+
"count": 3731
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"type": "LOC",
|
| 119 |
+
"count": 2418
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"type": "FAC",
|
| 123 |
+
"count": 1507
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
"type": "WEBSITE",
|
| 127 |
+
"count": 1481
|
| 128 |
+
},
|
| 129 |
+
{
|
| 130 |
+
"type": "LAW",
|
| 131 |
+
"count": 904
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"type": "TIME",
|
| 135 |
+
"count": 871
|
| 136 |
+
},
|
| 137 |
+
{
|
| 138 |
+
"type": "MONEY",
|
| 139 |
+
"count": 465
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"type": "CURR",
|
| 143 |
+
"count": 456
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"type": "LANGUAGE",
|
| 147 |
+
"count": 332
|
| 148 |
+
},
|
| 149 |
+
{
|
| 150 |
+
"type": "PERCENT",
|
| 151 |
+
"count": 312
|
| 152 |
+
},
|
| 153 |
+
{
|
| 154 |
+
"type": "PRODUCT",
|
| 155 |
+
"count": 191
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
"type": "QUANTITY",
|
| 159 |
+
"count": 115
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"type": "UNIT",
|
| 163 |
+
"count": 115
|
| 164 |
+
}
|
| 165 |
+
]
|
| 166 |
+
}
|
type_predictor_data.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c86d6152e1d2ca5ff2b96e92ff576ac35fe1effb6943453dc81d7a19e4f07d81
|
| 3 |
+
size 92382233
|