File size: 6,760 Bytes
b0f8699 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #!/usr/bin/env python3
"""
Run this script as ./conversion_script.py to convert SemEval-2020 Task 5
("Modelling Causal Reasoning in Language: Detecting Counterfactuals")
into HF-compatible parquet files.
Citation / original source
---------------------------
Yang, X., Obadinma, S., Zhao, H., Zhang, Q., Matwin, S., & Zhu, X. (2020).
"SemEval-2020 Task 5: Counterfactual Recognition." Proceedings of the
14th Workshop on Semantic Evaluation (SemEval-2020), pages 322-335.
https://aclanthology.org/2020.semeval-1.40/
Repo (verified live, public, no login, real rows fetched and offsets
independently re-verified before trusting it):
github.com/arielsho/SemEval-2020-Task-5. No LICENSE file (verified: 404)
-- the README only asks for a citation, so usage here is citation-gated/
academic-use-implied rather than under an explicit open license; flagged,
not resolved. This repo is a POST-competition release with real gold
labels for both splits (verified: `subtask1_test.csv`'s ``gold_label``
column and `subtask2_test.csv`'s span columns are both populated with
real values, not blind/withheld) -- unlike the original CodaLab
competition, which would have hidden test labels during the shared task.
Subtask 1 (``Subtask-1/subtask1_{train,test}.csv``, columns
``sentenceID,gold_label,sentence``) is used for causality-detection:
13000 train + 7000 test = 20000 sentences, 1454+738 = 2192 causal
(gold_label=1) / 17808 noncausal -- verified directly, an EXACT match to
Hagen et al. 2026 (arXiv:2510.08224) Table 2's citation of this dataset
(2192 causal + 17808 noncausal), confirming train+test combined (not
train alone, which is only 13000) is the intended full dataset size.
Subtask 2 (``Subtask-2/subtask2_{train,test}.csv``, columns
``sentenceID,sentence,antecedent,consequent,antecedent_startid,
antecedent_endid,consequent_startid,consequent_endid``) is used for
causal-candidate-extraction/causality-identification: 3551 train + 1950
test = 5501 sentences. This is a DIFFERENT, disjoint sentence set from
Subtask 1 (sentenceIDs 200000+ vs. 100000+) -- it only covers the
causal/counterfactual sentences re-collected for span annotation, not
all 20000 Subtask-1 sentences; same situation as several other datasets
here (e.g. FinCausal 2020) where detection and extraction/identification
draw from different upstream tables.
``antecedent``/``consequent`` are the hypothetical condition and
hypothetical result of a counterfactual statement respectively -- mapped
onto this project's Cause/Effect roles (antecedent=cause,
consequent=effect), matching the paper's own "antecedents and consequent
[are connected] with causal relations" framing.
Offsets: ``antecedent_startid``/``antecedent_endid`` are INCLUSIVE on
both ends (need ``sentence[start:end+1]``) -- verified: 0/5501 mismatches
against the ``antecedent`` column with this convention. ``consequent``
offsets use the SAME inclusive convention, EXCEPT 788/5501 (14.3%) rows
have ``consequent == "{}"`` with sentinel offsets ``(-1, -1)`` -- a
literal "no consequent span annotated" marker (verified: every "{}"
row has exactly (-1,-1), and every non-"{}" row's offsets are exact) --
not a parsing bug, a genuine share of counterfactuals with only an
antecedent span and no separate consequent span. Such rows contribute
only the antecedent entity (no relation) to causality-identification --
matching this project's "still include the entity, just no relation"
convention used for BioCause's Effect-only events.
"""
from pathlib import Path
import pandas as pd
from causalatee.data.constants import ClassLabel, Relation, Task
from causalatee.data.utils import insert_entity_markers, verify_dataset
_BASE_URL = "https://raw.githubusercontent.com/arielsho/SemEval-2020-Task-5/master"
_DETECTION_FILES = {"train": "Subtask-1/subtask1_train.csv", "test": "Subtask-1/subtask1_test.csv"}
_SPAN_FILES = {"train": "Subtask-2/subtask2_train.csv", "test": "Subtask-2/subtask2_test.csv"}
def convert_for_causality_detection(split: str) -> None:
df = pd.read_csv(f"{_BASE_URL}/{_DETECTION_FILES[split]}")
rows = [
{"index": f"semeval2020t5_{split}_{r.sentenceID}", "text": r.sentence,
"label": ClassLabel.Causal if r.gold_label else ClassLabel.Uncausal}
for r in df.itertuples()
]
df = pd.DataFrame(rows).set_index("index")
for error in verify_dataset(df, Task.CausalityDetection):
print(f"WARNING [SemEval2020T5 causality detection/{split}]: {error}")
df.to_parquet(f"./causality-detection/{split}.parquet", engine="pyarrow")
def _spans(df_row) -> tuple[tuple[int, int], tuple[int, int] | None]:
"""(antecedent_span, consequent_span_or_None), inclusive-end offsets resolved."""
ante = (int(df_row.antecedent_startid), int(df_row.antecedent_endid) + 1)
if df_row.consequent == "{}":
return ante, None
conseq = (int(df_row.consequent_startid), int(df_row.consequent_endid) + 1)
return ante, conseq
def convert_for_causal_candidate_extraction(split: str) -> None:
df = pd.read_csv(f"{_BASE_URL}/{_SPAN_FILES[split]}")
out = []
for r in df.itertuples():
ante, conseq = _spans(r)
entity = sorted({ante, conseq} if conseq else {ante})
out.append({"index": f"semeval2020t5_{split}_{r.sentenceID}", "text": r.sentence,
"entity": [list(s) for s in entity]})
df = pd.DataFrame(out).set_index("index")
for error in verify_dataset(df, Task.CausalCandidateExtraction):
print(f"WARNING [SemEval2020T5 causal candidate extraction/{split}]: {error}")
df.to_parquet(
f"./causal-candidate-extraction/{split}.parquet", engine="pyarrow"
)
def convert_for_causality_identification(split: str) -> None:
df = pd.read_csv(f"{_BASE_URL}/{_SPAN_FILES[split]}")
out = []
for r in df.itertuples():
ante, conseq = _spans(r)
segments = {"e1": [ante]}
relations = []
if conseq:
segments["e2"] = [conseq]
relations.append({"relationship": Relation.Procausal, "first": "e1", "second": "e2"})
marked_text = insert_entity_markers(r.sentence, segments)
out.append({"index": f"semeval2020t5_{split}_{r.sentenceID}", "text": marked_text, "relations": relations})
df = pd.DataFrame(out).set_index("index")
for error in verify_dataset(df, Task.CausalityIdentification):
print(f"WARNING [SemEval2020T5 causality identification/{split}]: {error}")
df.to_parquet(
f"./causality-identification/{split}.parquet", engine="pyarrow"
)
if __name__ == "__main__":
for split in ["train", "test"]:
convert_for_causality_detection(split)
convert_for_causal_candidate_extraction(split)
convert_for_causality_identification(split)
|