File size: 2,385 Bytes
115d1ae
 
 
 
e3ca08b
 
 
 
 
 
 
115d1ae
 
 
 
8fdc74e
115d1ae
8fdc74e
115d1ae
 
 
e3ca08b
 
 
 
 
115d1ae
8fdc74e
 
115d1ae
 
 
 
 
e3ca08b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3

"""
Run this script as ./conversion_script.py to convert the UniCausal CTB files to HF-compatible parquet files.

No causal-candidate-extraction table: UniCausal's CTB CSVs don't carry the
span data this converter would need to build one directly. Derived instead
from the causality-identification table written just above, via
causalatee.data.utils.identification_batch_to_extraction -- keeps the two
tables consistent by construction (extraction is exactly "identification's
entities, restricted to the ones backing an actual relation").
"""

# 1) Install dependencies:
#    pip install git+https://github.com/TheMrSheldon/causality-toolkit.git
# 2) Source files (fetched automatically via pandas):
#  - https://raw.githubusercontent.com/tanfiona/UniCausal/refs/heads/main/data/splits/ctb_train.csv
#  - https://raw.githubusercontent.com/tanfiona/UniCausal/refs/heads/main/data/splits/ctb_test.csv

from pathlib import Path

import pandas as pd

from causalatee.data.constants import Task
from causalatee.data.conversion import UniCausal2HF
from causalatee.data.utils import identification_batch_to_extraction

converter = UniCausal2HF({"train": "https://raw.githubusercontent.com/tanfiona/UniCausal/refs/heads/main/data/splits/ctb_train.csv",
                         "test": "https://raw.githubusercontent.com/tanfiona/UniCausal/refs/heads/main/data/splits/ctb_test.csv"}, Path.cwd(), grouped=False)

converter.convert(Task.CausalityDetection, "train")
converter.convert(Task.CausalityDetection, "test")
converter.convert(Task.CausalityIdentification, "train")
converter.convert(Task.CausalityIdentification, "test")


def _convert_extraction_from_identification(split: str) -> None:
    identification = pd.read_parquet(f"./causality-identification/{split}.parquet")
    batch = {"text": identification["text"].tolist(), "relations": identification["relations"].tolist()}
    out = identification_batch_to_extraction(batch)
    df = pd.DataFrame({
        "index": [f"ctb_{split}_{i}" for i in range(len(out["text"]))],
        "text": out["text"],
        "entity": out["entity"],
    }).set_index("index")
    Path("./causal-candidate-extraction").mkdir(exist_ok=True)
    df.to_parquet(f"./causal-candidate-extraction/{split}.parquet", engine="pyarrow")


_convert_extraction_from_identification("train")
_convert_extraction_from_identification("test")