Datasets:
Add full English and Turkish text with three-way labels
Browse files- README.md +20 -20
- data/test-00000-of-00001.parquet +2 -2
- data/train-00000-of-00001.parquet +2 -2
- metadata/build_summary.json +16 -6
- metadata/source_manifest.json +5 -6
- scripts/build_dataset.py +106 -47
- tests/test_build_dataset.py +35 -9
README.md
CHANGED
|
@@ -31,27 +31,16 @@ configs:
|
|
| 31 |
|
| 32 |
HateSpeechMix is a research-oriented normalization of four multilingual hate/offensive-speech resources. It contains 187,315 rows across Amharic, Chinese, English, Hindi, and Turkish with deterministic `train` and `test` splits.
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
The English/Turkish source distributes tweet IDs and annotations but not tweet text, in accordance with Twitter/X terms. Its 128,907 rows therefore have `text = null` and `text_available = false`. Do not pass the full dataset directly to a tokenizer without filtering:
|
| 37 |
-
|
| 38 |
-
```python
|
| 39 |
-
from datasets import load_dataset
|
| 40 |
-
|
| 41 |
-
dataset = load_dataset("villacu/hatespeechmix")
|
| 42 |
-
text_dataset = dataset.filter(lambda row: row["text_available"])
|
| 43 |
-
```
|
| 44 |
-
|
| 45 |
-
The remaining 58,408 rows contain text. No tweet hydration or text reconstruction was attempted.
|
| 46 |
|
| 47 |
## Schema
|
| 48 |
|
| 49 |
| Field | Meaning |
|
| 50 |
|---|---|
|
| 51 |
-
| `text` | Source text
|
| 52 |
-
| `label` |
|
| 53 |
| `label_text` | Human-readable form of `label` |
|
| 54 |
-
| `fine_label` | Best available normalized subtype
|
| 55 |
| `language` | ISO 639-1 language code |
|
| 56 |
| `source` | Stable source-corpus identifier |
|
| 57 |
| `source_id` | Identifier within the source corpus |
|
|
@@ -61,7 +50,16 @@ The remaining 58,408 rows contain text. No tweet hydration or text reconstructio
|
|
| 61 |
| `target` | Source target type when available |
|
| 62 |
| `text_available` | Whether `text` contains usable text |
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
## Split policy
|
| 67 |
|
|
@@ -70,7 +68,7 @@ The primary label is deliberately called **abusive**, not **hate**. The Chinese
|
|
| 70 |
- Amharic RANLP 2023: preserve test; merge upstream dev into train.
|
| 71 |
- English/Turkish v2: create a deterministic 90/10 split, stratified by language, topic, and original three-way label. The source used 10-fold cross-validation and does not publish a fixed split.
|
| 72 |
|
| 73 |
-
After assigning splits, the build removes training rows whose exact `(source, text)` pair also occurs in test. Test rows are retained. This removes 56 leaked training rows (54 Chinese, one Hindi, and one Amharic), including
|
| 74 |
|
| 75 |
Rows are kept in source order and are not globally shuffled. Shuffle explicitly during training.
|
| 76 |
|
|
@@ -80,13 +78,15 @@ Rows are kept in source order and are not globally shuffled. Shuffle explicitly
|
|
| 80 |
|---|---:|---:|---:|
|
| 81 |
| Amharic (`am`) | 15,000 | 15,001 | 15,000 |
|
| 82 |
| Chinese (`zh`) | 37,426 | 37,480 | 37,426 |
|
| 83 |
-
| English (`en`) | 68,597 | 68,597 |
|
| 84 |
| Hindi (`hi`) | 5,982 | 5,983 | 5,982 |
|
| 85 |
-
| Turkish (`tr`) | 60,310 | 60,310 |
|
| 86 |
-
| **Total** | **187,315** | **187,371** | **
|
| 87 |
|
| 88 |
Exact split and label counts are recorded in [`metadata/build_summary.json`](metadata/build_summary.json). Source revisions and checksums are recorded in [`metadata/source_manifest.json`](metadata/source_manifest.json).
|
| 89 |
|
|
|
|
|
|
|
| 90 |
## Licensing and redistribution
|
| 91 |
|
| 92 |
This compilation does **not** have a single permissive license:
|
|
|
|
| 31 |
|
| 32 |
HateSpeechMix is a research-oriented normalization of four multilingual hate/offensive-speech resources. It contains 187,315 rows across Amharic, Chinese, English, Hindi, and Turkish with deterministic `train` and `test` splits.
|
| 33 |
|
| 34 |
+
All published rows contain text. English and Turkish use the authors' official 2024 full-text v2 release rather than the older ID-only export.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
## Schema
|
| 37 |
|
| 38 |
| Field | Meaning |
|
| 39 |
|---|---|
|
| 40 |
+
| `text` | Source text |
|
| 41 |
+
| `label` | Three-way `ClassLabel`: 0 = `neutral`, 1 = `offensive`, 2 = `hate` |
|
| 42 |
| `label_text` | Human-readable form of `label` |
|
| 43 |
+
| `fine_label` | Best available normalized subtype; preserves Hindi `profane` separately |
|
| 44 |
| `language` | ISO 639-1 language code |
|
| 45 |
| `source` | Stable source-corpus identifier |
|
| 46 |
| `source_id` | Identifier within the source corpus |
|
|
|
|
| 50 |
| `target` | Source target type when available |
|
| 51 |
| `text_available` | Whether `text` contains usable text |
|
| 52 |
|
| 53 |
+
## Label normalization
|
| 54 |
+
|
| 55 |
+
| Source | Unified mapping |
|
| 56 |
+
|---|---|
|
| 57 |
+
| English/Turkish | Normal → `neutral`; Offensive → `offensive`; Hate → `hate` |
|
| 58 |
+
| Amharic | Normal → `neutral`; Offensive → `offensive`; Hate → `hate` |
|
| 59 |
+
| Hindi | NONE → `neutral`; OFFN or PRFN → `offensive`; HATE → `hate` |
|
| 60 |
+
| Chinese | Safe → `neutral`; every offensive/attack category → `offensive` |
|
| 61 |
+
|
| 62 |
+
Chinese has no unified `hate` examples. Its test-only individual/group distinction remains in `source_label` and `target`; mapping group attacks to hate would add an interpretation absent from the source's main binary annotation. Hindi `PRFN` remains visible as `fine_label = "profane"` and in `source_label` even though its unified label is `offensive`.
|
| 63 |
|
| 64 |
## Split policy
|
| 65 |
|
|
|
|
| 68 |
- Amharic RANLP 2023: preserve test; merge upstream dev into train.
|
| 69 |
- English/Turkish v2: create a deterministic 90/10 split, stratified by language, topic, and original three-way label. The source used 10-fold cross-validation and does not publish a fixed split.
|
| 70 |
|
| 71 |
+
After assigning splits, the build removes training rows whose exact `(source, text)` pair also occurs in test. Test rows are retained. This removes 56 leaked training rows (54 Chinese, one Hindi, and one Amharic), including nine whose unified label conflicts with the test copy. Within-split duplicates are retained and reported rather than silently resolved because some have conflicting annotations.
|
| 72 |
|
| 73 |
Rows are kept in source order and are not globally shuffled. Shuffle explicitly during training.
|
| 74 |
|
|
|
|
| 78 |
|---|---:|---:|---:|
|
| 79 |
| Amharic (`am`) | 15,000 | 15,001 | 15,000 |
|
| 80 |
| Chinese (`zh`) | 37,426 | 37,480 | 37,426 |
|
| 81 |
+
| English (`en`) | 68,597 | 68,597 | 68,597 |
|
| 82 |
| Hindi (`hi`) | 5,982 | 5,983 | 5,982 |
|
| 83 |
+
| Turkish (`tr`) | 60,310 | 60,310 | 60,310 |
|
| 84 |
+
| **Total** | **187,315** | **187,371** | **187,315** |
|
| 85 |
|
| 86 |
Exact split and label counts are recorded in [`metadata/build_summary.json`](metadata/build_summary.json). Source revisions and checksums are recorded in [`metadata/source_manifest.json`](metadata/source_manifest.json).
|
| 87 |
|
| 88 |
+
The official English/Turkish TSV contains bare carriage returns inside some tweet texts. The builder normalizes these formatting characters before parsing, validates required fields and categorical values, and reports any dropped records under `source_quality` in the build summary. The pinned release retains all 128,907 English/Turkish rows.
|
| 89 |
+
|
| 90 |
## Licensing and redistribution
|
| 91 |
|
| 92 |
This compilation does **not** have a single permissive license:
|
data/test-00000-of-00001.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:378f35e5094853d3276bc2cac013f5cc5fd5a578b44feab3754e836b03d6cdb8
|
| 3 |
+
size 3010553
|
data/train-00000-of-00001.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f3e43cb95833ff04930fb8c53f02c921938b395cd006f3dd7673c43035803971
|
| 3 |
+
size 22749897
|
metadata/build_summary.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
{
|
| 2 |
-
"build_version": "
|
| 3 |
"split_seed": "hatespeechmix-v1",
|
| 4 |
"num_rows": 187315,
|
| 5 |
-
"num_text_rows":
|
| 6 |
-
"num_metadata_only_rows":
|
| 7 |
"split_counts": {
|
| 8 |
"train": 166282,
|
| 9 |
"test": 21033
|
|
@@ -32,8 +32,9 @@
|
|
| 32 |
}
|
| 33 |
},
|
| 34 |
"label_counts": {
|
| 35 |
-
"
|
| 36 |
-
"
|
|
|
|
| 37 |
},
|
| 38 |
"source_counts": {
|
| 39 |
"amharic_ranlp2023": 15000,
|
|
@@ -44,7 +45,7 @@
|
|
| 44 |
"cross_split_text_leakage": {
|
| 45 |
"policy": "remove train rows with exact source/text match in test",
|
| 46 |
"removed_train_rows": 56,
|
| 47 |
-
"
|
| 48 |
"removed_by_source": {
|
| 49 |
"amharic_ranlp2023": 1,
|
| 50 |
"cold_zh": 54,
|
|
@@ -59,5 +60,14 @@
|
|
| 59 |
"within_split_duplicate_text_counts": {
|
| 60 |
"train": 102,
|
| 61 |
"test": 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"build_version": "2.0.0",
|
| 3 |
"split_seed": "hatespeechmix-v1",
|
| 4 |
"num_rows": 187315,
|
| 5 |
+
"num_text_rows": 187315,
|
| 6 |
+
"num_metadata_only_rows": 0,
|
| 7 |
"split_counts": {
|
| 8 |
"train": 166282,
|
| 9 |
"test": 21033
|
|
|
|
| 32 |
}
|
| 33 |
},
|
| 34 |
"label_counts": {
|
| 35 |
+
"hate": 21847,
|
| 36 |
+
"neutral": 114595,
|
| 37 |
+
"offensive": 50873
|
| 38 |
},
|
| 39 |
"source_counts": {
|
| 40 |
"amharic_ranlp2023": 15000,
|
|
|
|
| 45 |
"cross_split_text_leakage": {
|
| 46 |
"policy": "remove train rows with exact source/text match in test",
|
| 47 |
"removed_train_rows": 56,
|
| 48 |
+
"removed_rows_with_label_conflict": 9,
|
| 49 |
"removed_by_source": {
|
| 50 |
"amharic_ranlp2023": 1,
|
| 51 |
"cold_zh": 54,
|
|
|
|
| 60 |
"within_split_duplicate_text_counts": {
|
| 61 |
"train": 102,
|
| 62 |
"test": 3
|
| 63 |
+
},
|
| 64 |
+
"source_quality": {
|
| 65 |
+
"toraman2022_en_tr_v2": {
|
| 66 |
+
"source_rows": 128907,
|
| 67 |
+
"retained_rows": 128907,
|
| 68 |
+
"dropped_rows": 0,
|
| 69 |
+
"dropped_reasons": {},
|
| 70 |
+
"normalized_bare_carriage_returns": 22
|
| 71 |
+
}
|
| 72 |
}
|
| 73 |
}
|
metadata/source_manifest.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
{
|
| 2 |
-
"build_version": "
|
| 3 |
"sources": [
|
| 4 |
{
|
| 5 |
"name": "HASOC 2019 Hindi",
|
|
@@ -11,11 +11,11 @@
|
|
| 11 |
},
|
| 12 |
{
|
| 13 |
"name": "Large-Scale Hate Speech Dataset v2",
|
| 14 |
-
"source": "https://github.com/
|
| 15 |
-
"revision": "
|
| 16 |
-
"sha256": "
|
| 17 |
"license": "CC BY-NC-SA 4.0",
|
| 18 |
-
"redistribution_note": "
|
| 19 |
},
|
| 20 |
{
|
| 21 |
"name": "COLDataset",
|
|
@@ -37,4 +37,3 @@
|
|
| 37 |
}
|
| 38 |
]
|
| 39 |
}
|
| 40 |
-
|
|
|
|
| 1 |
{
|
| 2 |
+
"build_version": "2.0.0",
|
| 3 |
"sources": [
|
| 4 |
{
|
| 5 |
"name": "HASOC 2019 Hindi",
|
|
|
|
| 11 |
},
|
| 12 |
{
|
| 13 |
"name": "Large-Scale Hate Speech Dataset v2",
|
| 14 |
+
"source": "https://github.com/metunlp/hate-speech/blob/f27c847b34e68c40ac654219b5824f8750b284d8/dataset_v2/Toraman22_hate_speech_v2.tsv",
|
| 15 |
+
"revision": "f27c847b34e68c40ac654219b5824f8750b284d8",
|
| 16 |
+
"sha256": "c6c110de8e9656577b5781c8173d7974268c8e716adf5d804f98a50b2f4c0a95",
|
| 17 |
"license": "CC BY-NC-SA 4.0",
|
| 18 |
+
"redistribution_note": "Official 2024 full-text release; only tweet ID, text, language, topic, and final label are retained"
|
| 19 |
},
|
| 20 |
{
|
| 21 |
"name": "COLDataset",
|
|
|
|
| 37 |
}
|
| 38 |
]
|
| 39 |
}
|
|
|
scripts/build_dataset.py
CHANGED
|
@@ -3,11 +3,10 @@
|
|
| 3 |
|
| 4 |
The conversion is intentionally conservative:
|
| 5 |
|
| 6 |
-
* ``label``
|
| 7 |
-
offensive content.
|
| 8 |
* ``fine_label`` and ``source_label`` retain the available source semantics.
|
| 9 |
-
*
|
| 10 |
-
|
| 11 |
* Official test splits are preserved. Upstream dev splits are folded into
|
| 12 |
train. The unsplit English/Turkish data receives a deterministic,
|
| 13 |
stratified 90/10 split.
|
|
@@ -30,7 +29,7 @@ from typing import Iterable, Iterator
|
|
| 30 |
from datasets import ClassLabel, Dataset, DatasetDict, Features, Value
|
| 31 |
|
| 32 |
|
| 33 |
-
BUILD_VERSION = "
|
| 34 |
SPLIT_SEED = "hatespeechmix-v1"
|
| 35 |
|
| 36 |
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
@@ -39,12 +38,13 @@ WORKSPACE_ROOT = REPO_ROOT.parent
|
|
| 39 |
SOURCE_SPECS = {
|
| 40 |
"toraman_en_tr": {
|
| 41 |
"url": (
|
| 42 |
-
"https://
|
| 43 |
-
"
|
|
|
|
| 44 |
),
|
| 45 |
-
"sha256": "
|
| 46 |
-
"filename": "
|
| 47 |
-
"revision": "
|
| 48 |
},
|
| 49 |
"cold_zh": {
|
| 50 |
"url": (
|
|
@@ -85,11 +85,12 @@ SOURCE_SPECS = {
|
|
| 85 |
}
|
| 86 |
|
| 87 |
HINDI_SHA256 = "d419780fe825f9946e3a03da4cf3fdf41699b188932d3662ca304693829994ad"
|
|
|
|
| 88 |
|
| 89 |
FEATURES = Features(
|
| 90 |
{
|
| 91 |
"text": Value("string"),
|
| 92 |
-
"label": ClassLabel(names=
|
| 93 |
"label_text": Value("string"),
|
| 94 |
"fine_label": Value("string"),
|
| 95 |
"language": Value("string"),
|
|
@@ -187,11 +188,11 @@ def record(
|
|
| 187 |
topic: str | None = None,
|
| 188 |
target: str | None = None,
|
| 189 |
) -> dict[str, object]:
|
| 190 |
-
if label_text not in
|
| 191 |
-
raise ValueError(f"Invalid
|
| 192 |
return {
|
| 193 |
"text": text,
|
| 194 |
-
"label":
|
| 195 |
"label_text": label_text,
|
| 196 |
"fine_label": fine_label,
|
| 197 |
"language": language,
|
|
@@ -207,7 +208,8 @@ def record(
|
|
| 207 |
|
| 208 |
def load_hindi(hindi_zip: Path) -> dict[str, list[dict[str, object]]]:
|
| 209 |
verify_file(hindi_zip, HINDI_SHA256)
|
| 210 |
-
fine_labels = {"NONE": "
|
|
|
|
| 211 |
targets = {"NONE": None, "TIN": "targeted", "UNT": "untargeted"}
|
| 212 |
members = {
|
| 213 |
"train": "hindi_dataset/hindi_dataset.tsv",
|
|
@@ -217,7 +219,7 @@ def load_hindi(hindi_zip: Path) -> dict[str, list[dict[str, object]]]:
|
|
| 217 |
for split, member in members.items():
|
| 218 |
for row in dict_rows_from_zip(hindi_zip, member, delimiter="\t"):
|
| 219 |
task_1 = row["task_1"]
|
| 220 |
-
label_text = "
|
| 221 |
splits[split].append(
|
| 222 |
record(
|
| 223 |
text=row["text"],
|
|
@@ -270,38 +272,86 @@ def deterministic_stratified_split(
|
|
| 270 |
return splits
|
| 271 |
|
| 272 |
|
| 273 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
lang = {"0": "tr", "1": "en"}
|
| 275 |
topics = {"0": "religion", "1": "gender", "2": "race", "3": "politics", "4": "sports"}
|
| 276 |
-
|
| 277 |
rows: list[dict[str, object]] = []
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
rows.append(
|
| 283 |
record(
|
| 284 |
-
text=
|
| 285 |
-
label_text=
|
| 286 |
-
fine_label=
|
| 287 |
-
language=lang[row["
|
| 288 |
source="toraman2022_en_tr_v2",
|
| 289 |
-
source_id=row["
|
| 290 |
-
source_label=row["
|
| 291 |
original_split="unsplit",
|
| 292 |
-
topic=topics[row["
|
| 293 |
)
|
| 294 |
)
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
)
|
|
|
|
| 300 |
|
| 301 |
|
| 302 |
def load_chinese(archive_path: Path) -> dict[str, list[dict[str, object]]]:
|
| 303 |
splits: dict[str, list[dict[str, object]]] = {"train": [], "test": []}
|
| 304 |
-
fine_test = {"0": "
|
| 305 |
target_test = {"0": None, "1": "individual", "2": "group", "3": None}
|
| 306 |
for upstream_split in ("train", "dev", "test"):
|
| 307 |
output_split = "test" if upstream_split == "test" else "train"
|
|
@@ -311,7 +361,7 @@ def load_chinese(archive_path: Path) -> dict[str, list[dict[str, object]]]:
|
|
| 311 |
source_binary = row["label"]
|
| 312 |
source_fine = row.get("fine-grained-label")
|
| 313 |
if source_fine is None:
|
| 314 |
-
fine_label = "
|
| 315 |
target = None
|
| 316 |
source_label = source_binary
|
| 317 |
else:
|
|
@@ -321,7 +371,7 @@ def load_chinese(archive_path: Path) -> dict[str, list[dict[str, object]]]:
|
|
| 321 |
splits[output_split].append(
|
| 322 |
record(
|
| 323 |
text=row["TEXT"],
|
| 324 |
-
label_text="
|
| 325 |
fine_label=fine_label,
|
| 326 |
language="zh",
|
| 327 |
source="cold_zh",
|
|
@@ -347,16 +397,19 @@ def load_amharic(paths: dict[str, Path]) -> dict[str, list[dict[str, object]]]:
|
|
| 347 |
raise ValueError(
|
| 348 |
f"Expected two Amharic TSV columns in {upstream_split} row {index}, got {len(row)}"
|
| 349 |
)
|
| 350 |
-
|
|
|
|
|
|
|
|
|
|
| 351 |
splits[output_split].append(
|
| 352 |
record(
|
| 353 |
text=text,
|
| 354 |
-
label_text=
|
| 355 |
-
fine_label=
|
| 356 |
language="am",
|
| 357 |
source="amharic_ranlp2023",
|
| 358 |
source_id=f"{upstream_split}:{index:05d}",
|
| 359 |
-
source_label=
|
| 360 |
original_split=upstream_split,
|
| 361 |
)
|
| 362 |
)
|
|
@@ -378,8 +431,8 @@ def remove_cross_split_text_leakage(
|
|
| 378 |
) -> tuple[dict[str, list[dict[str, object]]], dict[str, object]]:
|
| 379 |
"""Remove train rows whose exact source/text pair occurs in test.
|
| 380 |
|
| 381 |
-
Test rows are authoritative.
|
| 382 |
-
|
| 383 |
"""
|
| 384 |
raw_language_counts = Counter(
|
| 385 |
str(row["language"]) for rows in splits.values() for row in rows
|
|
@@ -396,14 +449,14 @@ def remove_cross_split_text_leakage(
|
|
| 396 |
|
| 397 |
kept_train: list[dict[str, object]] = []
|
| 398 |
removed: list[dict[str, object]] = []
|
| 399 |
-
|
| 400 |
for row in splits["train"]:
|
| 401 |
key = (str(row["source"]), str(row["text"]))
|
| 402 |
matching_test_rows = test_by_key.get(key, []) if row["text_available"] else []
|
| 403 |
if matching_test_rows:
|
| 404 |
removed.append(row)
|
| 405 |
if any(test_row["label"] != row["label"] for test_row in matching_test_rows):
|
| 406 |
-
|
| 407 |
else:
|
| 408 |
kept_train.append(row)
|
| 409 |
|
|
@@ -411,7 +464,7 @@ def remove_cross_split_text_leakage(
|
|
| 411 |
report = {
|
| 412 |
"policy": "remove train rows with exact source/text match in test",
|
| 413 |
"removed_train_rows": len(removed),
|
| 414 |
-
"
|
| 415 |
"removed_by_source": dict(
|
| 416 |
sorted(Counter(str(row["source"]) for row in removed).items())
|
| 417 |
),
|
|
@@ -446,7 +499,7 @@ def validate_records(
|
|
| 446 |
row["text"] is not None and row["text"] != ""
|
| 447 |
):
|
| 448 |
raise ValueError("text_available disagrees with text")
|
| 449 |
-
expected_label =
|
| 450 |
if row["label"] != expected_label:
|
| 451 |
raise ValueError("label disagrees with label_text")
|
| 452 |
|
|
@@ -552,16 +605,22 @@ def parse_args() -> argparse.Namespace:
|
|
| 552 |
def main() -> None:
|
| 553 |
args = parse_args()
|
| 554 |
source_paths = download_sources(args.cache_dir)
|
|
|
|
|
|
|
|
|
|
| 555 |
splits = merge_sources(
|
| 556 |
[
|
| 557 |
load_hindi(args.hindi_zip),
|
| 558 |
-
|
| 559 |
load_chinese(source_paths["cold_zh"]),
|
| 560 |
load_amharic(source_paths),
|
| 561 |
]
|
| 562 |
)
|
| 563 |
splits, leakage_report = remove_cross_split_text_leakage(splits)
|
| 564 |
summary = validate_records(splits, leakage_report)
|
|
|
|
|
|
|
|
|
|
| 565 |
dataset = to_dataset_dict(splits)
|
| 566 |
write_outputs(dataset, summary, args.output_dir)
|
| 567 |
print(json.dumps(summary, indent=2, ensure_ascii=False))
|
|
|
|
| 3 |
|
| 4 |
The conversion is intentionally conservative:
|
| 5 |
|
| 6 |
+
* ``label`` uses the shared three-way space: neutral, offensive, and hate.
|
|
|
|
| 7 |
* ``fine_label`` and ``source_label`` retain the available source semantics.
|
| 8 |
+
* Chinese positives map to offensive because its main labels do not
|
| 9 |
+
distinguish offensive content from hate.
|
| 10 |
* Official test splits are preserved. Upstream dev splits are folded into
|
| 11 |
train. The unsplit English/Turkish data receives a deterministic,
|
| 12 |
stratified 90/10 split.
|
|
|
|
| 29 |
from datasets import ClassLabel, Dataset, DatasetDict, Features, Value
|
| 30 |
|
| 31 |
|
| 32 |
+
BUILD_VERSION = "2.0.0"
|
| 33 |
SPLIT_SEED = "hatespeechmix-v1"
|
| 34 |
|
| 35 |
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
| 38 |
SOURCE_SPECS = {
|
| 39 |
"toraman_en_tr": {
|
| 40 |
"url": (
|
| 41 |
+
"https://raw.githubusercontent.com/metunlp/hate-speech/"
|
| 42 |
+
"f27c847b34e68c40ac654219b5824f8750b284d8/"
|
| 43 |
+
"dataset_v2/Toraman22_hate_speech_v2.tsv"
|
| 44 |
),
|
| 45 |
+
"sha256": "c6c110de8e9656577b5781c8173d7974268c8e716adf5d804f98a50b2f4c0a95",
|
| 46 |
+
"filename": "toraman22_hate_speech_v2.tsv",
|
| 47 |
+
"revision": "f27c847b34e68c40ac654219b5824f8750b284d8",
|
| 48 |
},
|
| 49 |
"cold_zh": {
|
| 50 |
"url": (
|
|
|
|
| 85 |
}
|
| 86 |
|
| 87 |
HINDI_SHA256 = "d419780fe825f9946e3a03da4cf3fdf41699b188932d3662ca304693829994ad"
|
| 88 |
+
LABEL_NAMES = ["neutral", "offensive", "hate"]
|
| 89 |
|
| 90 |
FEATURES = Features(
|
| 91 |
{
|
| 92 |
"text": Value("string"),
|
| 93 |
+
"label": ClassLabel(names=LABEL_NAMES),
|
| 94 |
"label_text": Value("string"),
|
| 95 |
"fine_label": Value("string"),
|
| 96 |
"language": Value("string"),
|
|
|
|
| 188 |
topic: str | None = None,
|
| 189 |
target: str | None = None,
|
| 190 |
) -> dict[str, object]:
|
| 191 |
+
if label_text not in LABEL_NAMES:
|
| 192 |
+
raise ValueError(f"Invalid normalized label: {label_text}")
|
| 193 |
return {
|
| 194 |
"text": text,
|
| 195 |
+
"label": LABEL_NAMES.index(label_text),
|
| 196 |
"label_text": label_text,
|
| 197 |
"fine_label": fine_label,
|
| 198 |
"language": language,
|
|
|
|
| 208 |
|
| 209 |
def load_hindi(hindi_zip: Path) -> dict[str, list[dict[str, object]]]:
|
| 210 |
verify_file(hindi_zip, HINDI_SHA256)
|
| 211 |
+
fine_labels = {"NONE": "neutral", "OFFN": "offensive", "HATE": "hate", "PRFN": "profane"}
|
| 212 |
+
labels = {"NONE": "neutral", "OFFN": "offensive", "HATE": "hate", "PRFN": "offensive"}
|
| 213 |
targets = {"NONE": None, "TIN": "targeted", "UNT": "untargeted"}
|
| 214 |
members = {
|
| 215 |
"train": "hindi_dataset/hindi_dataset.tsv",
|
|
|
|
| 219 |
for split, member in members.items():
|
| 220 |
for row in dict_rows_from_zip(hindi_zip, member, delimiter="\t"):
|
| 221 |
task_1 = row["task_1"]
|
| 222 |
+
label_text = labels[row["task_2"]]
|
| 223 |
splits[split].append(
|
| 224 |
record(
|
| 225 |
text=row["text"],
|
|
|
|
| 272 |
return splits
|
| 273 |
|
| 274 |
|
| 275 |
+
def normalized_tsv_rows(path: Path) -> tuple[list[dict[str, str]], int]:
|
| 276 |
+
"""Read the official TSV after repairing bare carriage returns in text."""
|
| 277 |
+
with path.open(encoding="utf-8-sig", newline="") as handle:
|
| 278 |
+
raw = handle.read()
|
| 279 |
+
crlf_count = raw.count("\r\n")
|
| 280 |
+
bare_cr_count = raw.count("\r") - crlf_count
|
| 281 |
+
normalized = raw.replace("\r\n", "\n").replace("\r", " ")
|
| 282 |
+
rows = list(csv.DictReader(io.StringIO(normalized, newline=""), delimiter="\t"))
|
| 283 |
+
return rows, bare_cr_count
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
def load_english_turkish(
|
| 287 |
+
tsv_path: Path,
|
| 288 |
+
) -> tuple[dict[str, list[dict[str, object]]], dict[str, object]]:
|
| 289 |
lang = {"0": "tr", "1": "en"}
|
| 290 |
topics = {"0": "religion", "1": "gender", "2": "race", "3": "politics", "4": "sports"}
|
| 291 |
+
labels = {"0": "neutral", "1": "offensive", "2": "hate"}
|
| 292 |
rows: list[dict[str, object]] = []
|
| 293 |
+
dropped_reasons: Counter[str] = Counter()
|
| 294 |
+
source_rows, normalized_bare_cr = normalized_tsv_rows(tsv_path)
|
| 295 |
+
seen_ids: set[str] = set()
|
| 296 |
+
required_columns = {"tweet_id", "topic", "language", "text", "label"}
|
| 297 |
+
|
| 298 |
+
for row in source_rows:
|
| 299 |
+
if not required_columns.issubset(row) or any(
|
| 300 |
+
row.get(column) is None for column in required_columns
|
| 301 |
+
):
|
| 302 |
+
dropped_reasons["malformed_columns"] += 1
|
| 303 |
+
continue
|
| 304 |
+
if not row["tweet_id"].isdigit():
|
| 305 |
+
dropped_reasons["invalid_tweet_id"] += 1
|
| 306 |
+
continue
|
| 307 |
+
if row["tweet_id"] in seen_ids:
|
| 308 |
+
dropped_reasons["duplicate_tweet_id"] += 1
|
| 309 |
+
continue
|
| 310 |
+
if row["language"] not in lang:
|
| 311 |
+
dropped_reasons["invalid_language"] += 1
|
| 312 |
+
continue
|
| 313 |
+
if row["topic"] not in topics:
|
| 314 |
+
dropped_reasons["invalid_topic"] += 1
|
| 315 |
+
continue
|
| 316 |
+
if row["label"] not in labels:
|
| 317 |
+
dropped_reasons["invalid_label"] += 1
|
| 318 |
+
continue
|
| 319 |
+
if not row["text"].strip():
|
| 320 |
+
dropped_reasons["empty_text"] += 1
|
| 321 |
+
continue
|
| 322 |
+
|
| 323 |
+
seen_ids.add(row["tweet_id"])
|
| 324 |
+
label_text = labels[row["label"]]
|
| 325 |
rows.append(
|
| 326 |
record(
|
| 327 |
+
text=row["text"],
|
| 328 |
+
label_text=label_text,
|
| 329 |
+
fine_label=label_text,
|
| 330 |
+
language=lang[row["language"]],
|
| 331 |
source="toraman2022_en_tr_v2",
|
| 332 |
+
source_id=row["tweet_id"],
|
| 333 |
+
source_label=row["label"],
|
| 334 |
original_split="unsplit",
|
| 335 |
+
topic=topics[row["topic"]],
|
| 336 |
)
|
| 337 |
)
|
| 338 |
+
|
| 339 |
+
quality_report = {
|
| 340 |
+
"source_rows": len(source_rows),
|
| 341 |
+
"retained_rows": len(rows),
|
| 342 |
+
"dropped_rows": sum(dropped_reasons.values()),
|
| 343 |
+
"dropped_reasons": dict(sorted(dropped_reasons.items())),
|
| 344 |
+
"normalized_bare_carriage_returns": normalized_bare_cr,
|
| 345 |
+
}
|
| 346 |
+
splits = deterministic_stratified_split(
|
| 347 |
+
rows, strata=("language", "topic", "fine_label"), test_fraction=0.1
|
| 348 |
)
|
| 349 |
+
return splits, quality_report
|
| 350 |
|
| 351 |
|
| 352 |
def load_chinese(archive_path: Path) -> dict[str, list[dict[str, object]]]:
|
| 353 |
splits: dict[str, list[dict[str, object]]] = {"train": [], "test": []}
|
| 354 |
+
fine_test = {"0": "neutral", "1": "offensive", "2": "offensive", "3": "neutral"}
|
| 355 |
target_test = {"0": None, "1": "individual", "2": "group", "3": None}
|
| 356 |
for upstream_split in ("train", "dev", "test"):
|
| 357 |
output_split = "test" if upstream_split == "test" else "train"
|
|
|
|
| 361 |
source_binary = row["label"]
|
| 362 |
source_fine = row.get("fine-grained-label")
|
| 363 |
if source_fine is None:
|
| 364 |
+
fine_label = "neutral" if source_binary == "0" else "offensive"
|
| 365 |
target = None
|
| 366 |
source_label = source_binary
|
| 367 |
else:
|
|
|
|
| 371 |
splits[output_split].append(
|
| 372 |
record(
|
| 373 |
text=row["TEXT"],
|
| 374 |
+
label_text="neutral" if source_binary == "0" else "offensive",
|
| 375 |
fine_label=fine_label,
|
| 376 |
language="zh",
|
| 377 |
source="cold_zh",
|
|
|
|
| 397 |
raise ValueError(
|
| 398 |
f"Expected two Amharic TSV columns in {upstream_split} row {index}, got {len(row)}"
|
| 399 |
)
|
| 400 |
+
source_fine_label, text = row
|
| 401 |
+
label_text = (
|
| 402 |
+
"neutral" if source_fine_label == "normal" else source_fine_label
|
| 403 |
+
)
|
| 404 |
splits[output_split].append(
|
| 405 |
record(
|
| 406 |
text=text,
|
| 407 |
+
label_text=label_text,
|
| 408 |
+
fine_label=label_text,
|
| 409 |
language="am",
|
| 410 |
source="amharic_ranlp2023",
|
| 411 |
source_id=f"{upstream_split}:{index:05d}",
|
| 412 |
+
source_label=source_fine_label,
|
| 413 |
original_split=upstream_split,
|
| 414 |
)
|
| 415 |
)
|
|
|
|
| 431 |
) -> tuple[dict[str, list[dict[str, object]]], dict[str, object]]:
|
| 432 |
"""Remove train rows whose exact source/text pair occurs in test.
|
| 433 |
|
| 434 |
+
Test rows are authoritative. The `text_available` guard keeps this routine
|
| 435 |
+
safe if a future source introduces metadata-only rows.
|
| 436 |
"""
|
| 437 |
raw_language_counts = Counter(
|
| 438 |
str(row["language"]) for rows in splits.values() for row in rows
|
|
|
|
| 449 |
|
| 450 |
kept_train: list[dict[str, object]] = []
|
| 451 |
removed: list[dict[str, object]] = []
|
| 452 |
+
conflicting_labels = 0
|
| 453 |
for row in splits["train"]:
|
| 454 |
key = (str(row["source"]), str(row["text"]))
|
| 455 |
matching_test_rows = test_by_key.get(key, []) if row["text_available"] else []
|
| 456 |
if matching_test_rows:
|
| 457 |
removed.append(row)
|
| 458 |
if any(test_row["label"] != row["label"] for test_row in matching_test_rows):
|
| 459 |
+
conflicting_labels += 1
|
| 460 |
else:
|
| 461 |
kept_train.append(row)
|
| 462 |
|
|
|
|
| 464 |
report = {
|
| 465 |
"policy": "remove train rows with exact source/text match in test",
|
| 466 |
"removed_train_rows": len(removed),
|
| 467 |
+
"removed_rows_with_label_conflict": conflicting_labels,
|
| 468 |
"removed_by_source": dict(
|
| 469 |
sorted(Counter(str(row["source"]) for row in removed).items())
|
| 470 |
),
|
|
|
|
| 499 |
row["text"] is not None and row["text"] != ""
|
| 500 |
):
|
| 501 |
raise ValueError("text_available disagrees with text")
|
| 502 |
+
expected_label = LABEL_NAMES.index(str(row["label_text"]))
|
| 503 |
if row["label"] != expected_label:
|
| 504 |
raise ValueError("label disagrees with label_text")
|
| 505 |
|
|
|
|
| 605 |
def main() -> None:
|
| 606 |
args = parse_args()
|
| 607 |
source_paths = download_sources(args.cache_dir)
|
| 608 |
+
english_turkish, english_turkish_quality = load_english_turkish(
|
| 609 |
+
source_paths["toraman_en_tr"]
|
| 610 |
+
)
|
| 611 |
splits = merge_sources(
|
| 612 |
[
|
| 613 |
load_hindi(args.hindi_zip),
|
| 614 |
+
english_turkish,
|
| 615 |
load_chinese(source_paths["cold_zh"]),
|
| 616 |
load_amharic(source_paths),
|
| 617 |
]
|
| 618 |
)
|
| 619 |
splits, leakage_report = remove_cross_split_text_leakage(splits)
|
| 620 |
summary = validate_records(splits, leakage_report)
|
| 621 |
+
summary["source_quality"] = {
|
| 622 |
+
"toraman2022_en_tr_v2": english_turkish_quality,
|
| 623 |
+
}
|
| 624 |
dataset = to_dataset_dict(splits)
|
| 625 |
write_outputs(dataset, summary, args.output_dir)
|
| 626 |
print(json.dumps(summary, indent=2, ensure_ascii=False))
|
tests/test_build_dataset.py
CHANGED
|
@@ -15,12 +15,12 @@ build_dataset = importlib.util.module_from_spec(SPEC)
|
|
| 15 |
SPEC.loader.exec_module(build_dataset)
|
| 16 |
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
with pytest.raises(ValueError, match="Invalid
|
| 20 |
build_dataset.record(
|
| 21 |
text="sample",
|
| 22 |
-
label_text="
|
| 23 |
-
fine_label="
|
| 24 |
language="en",
|
| 25 |
source="fixture",
|
| 26 |
source_id="1",
|
|
@@ -32,9 +32,9 @@ def test_binary_mapping_rejects_unknown_label() -> None:
|
|
| 32 |
def test_stratified_split_is_deterministic_and_disjoint() -> None:
|
| 33 |
rows = [
|
| 34 |
build_dataset.record(
|
| 35 |
-
text=
|
| 36 |
-
label_text="
|
| 37 |
-
fine_label="
|
| 38 |
language="en",
|
| 39 |
source="fixture",
|
| 40 |
source_id=str(index),
|
|
@@ -67,8 +67,34 @@ def test_built_dataset_schema_and_counts() -> None:
|
|
| 67 |
assert len(dataset["train"]) + len(dataset["test"]) == 187_315
|
| 68 |
label = dataset["train"].features["label"]
|
| 69 |
assert isinstance(label, ClassLabel)
|
| 70 |
-
assert label.names == ["
|
| 71 |
-
assert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
def test_no_exact_source_text_leakage() -> None:
|
|
|
|
| 15 |
SPEC.loader.exec_module(build_dataset)
|
| 16 |
|
| 17 |
|
| 18 |
+
def test_three_way_mapping_rejects_unknown_label() -> None:
|
| 19 |
+
with pytest.raises(ValueError, match="Invalid normalized label"):
|
| 20 |
build_dataset.record(
|
| 21 |
text="sample",
|
| 22 |
+
label_text="abusive",
|
| 23 |
+
fine_label="abusive",
|
| 24 |
language="en",
|
| 25 |
source="fixture",
|
| 26 |
source_id="1",
|
|
|
|
| 32 |
def test_stratified_split_is_deterministic_and_disjoint() -> None:
|
| 33 |
rows = [
|
| 34 |
build_dataset.record(
|
| 35 |
+
text=f"sample {index}",
|
| 36 |
+
label_text="neutral" if index % 2 == 0 else "offensive",
|
| 37 |
+
fine_label="neutral" if index % 2 == 0 else "offensive",
|
| 38 |
language="en",
|
| 39 |
source="fixture",
|
| 40 |
source_id=str(index),
|
|
|
|
| 67 |
assert len(dataset["train"]) + len(dataset["test"]) == 187_315
|
| 68 |
label = dataset["train"].features["label"]
|
| 69 |
assert isinstance(label, ClassLabel)
|
| 70 |
+
assert label.names == ["neutral", "offensive", "hate"]
|
| 71 |
+
assert all(all(dataset[split]["text_available"]) for split in dataset)
|
| 72 |
+
assert sum(
|
| 73 |
+
source == "toraman2022_en_tr_v2"
|
| 74 |
+
for split in dataset.values()
|
| 75 |
+
for source in split["source"]
|
| 76 |
+
) == 128_907
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_source_specific_label_mapping() -> None:
|
| 80 |
+
local_dataset = ROOT / "local_dataset"
|
| 81 |
+
if not local_dataset.exists():
|
| 82 |
+
pytest.skip("Run scripts/build_dataset.py before the integration test")
|
| 83 |
+
|
| 84 |
+
dataset = load_from_disk(local_dataset)
|
| 85 |
+
rows = (row for split in dataset.values() for row in split)
|
| 86 |
+
chinese_labels = set()
|
| 87 |
+
profanity_rows = 0
|
| 88 |
+
for row in rows:
|
| 89 |
+
if row["language"] == "zh":
|
| 90 |
+
chinese_labels.add(row["label_text"])
|
| 91 |
+
if "|PRFN|" in row["source_label"]:
|
| 92 |
+
profanity_rows += 1
|
| 93 |
+
assert row["label_text"] == "offensive"
|
| 94 |
+
assert row["fine_label"] == "profane"
|
| 95 |
+
|
| 96 |
+
assert chinese_labels == {"neutral", "offensive"}
|
| 97 |
+
assert profanity_rows > 0
|
| 98 |
|
| 99 |
|
| 100 |
def test_no_exact_source_text_leakage() -> None:
|