thagen commited on
Commit
49fc347
·
1 Parent(s): 86f195e

fix: causal-candidate-extraction uses text+char-span format

Browse files

Replace the old tokens-as-segments / integer-entity-id format with the
standard text (plain str) + entity ([[start, end], ...] char offsets) format
used by all other datasets in the project.

Spans are derived from the causality-identification tagged text so the
conversion no longer requires the original XML source files.

causal-candidate-extraction/test.parquet CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:ad5e0271e92b904d5411453ae3f5643540eabf721accdc7ec4e78c91b4757da2
3
- size 49728
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e3b46dbdcebd10ba40e05be7f15e45f3eea2531f4b1e8b9b64c8e7e764fc84eb
3
+ size 62472
causal-candidate-extraction/train.parquet CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:36140219a24ab363966ed55377daea38fc4ce1d9e18bb8bd7787f698ec7facc5
3
- size 252771
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:48071c919f99226bb86c00319c44e816ec185f026ec38532933edb9b3689679c
3
+ size 335896
conversion_script.py CHANGED
@@ -22,23 +22,54 @@ def convert_for_causality_detection(split: Split) -> None:
22
  df.to_parquet(f"./causality-detection/{split}.parquet", engine="pyarrow")
23
 
24
  def convert_for_causal_candidate_extraction(split: Split) -> None:
25
- def map_to_tokens(text: str):
26
- splits: list[str] = []
27
- tags: list[set[int]] = []
28
- curtags: set[int] = set()
29
- for match in re.finditer(r"(.*?)<(/?)e(\d+)>", text):
30
- splits.append(match[1])
31
- tags.append(list(curtags))
32
- if match[2] == "":
33
- curtags.add(int(match[3]))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  else:
35
- curtags.remove(int(match[3]))
36
- return pd.Series((splits, tags))
37
- df = pd.read_xml(f"{split}-corpus.xml")
38
- df[["tokens", "entity"]] = df["sentence"].apply(map_to_tokens)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  df["index"] = df["id"].apply(lambda x: f"scite_{split}_{x}")
40
- df = df[["index", "tokens", "entity"]].set_index("index")
41
- print(df)
42
  df.to_parquet(f"./causal-candidate-extraction/{split}.parquet", engine="pyarrow")
43
 
44
  def convert_for_causality_identification(split: Split) -> None:
 
22
  df.to_parquet(f"./causality-detection/{split}.parquet", engine="pyarrow")
23
 
24
  def convert_for_causal_candidate_extraction(split: Split) -> None:
25
+ def extract_entity_spans(tagged_text: str, relations: list) -> tuple[str, list]:
26
+ """Return (plain_text, [[start, end], ...]) for entities in causal relations."""
27
+ causal_eids: set[str] = set()
28
+ for rel in relations:
29
+ if rel["relationship"] == 1:
30
+ causal_eids.add(rel["first"])
31
+ causal_eids.add(rel["second"])
32
+
33
+ # Single pass: track plain-text offset while consuming tags
34
+ plain_chars: list[str] = []
35
+ open_at: dict[str, int] = {}
36
+ spans: list[list[int]] = []
37
+ i = 0
38
+ while i < len(tagged_text):
39
+ m = re.match(r'<(/?)(e\d+)>', tagged_text[i:])
40
+ if m:
41
+ eid = m.group(2)
42
+ if m.group(1) == "": # opening tag
43
+ if eid in causal_eids:
44
+ open_at[eid] = len(plain_chars)
45
+ else: # closing tag
46
+ if eid in open_at:
47
+ spans.append([open_at.pop(eid), len(plain_chars)])
48
+ i += m.end()
49
  else:
50
+ plain_chars.append(tagged_text[i])
51
+ i += 1
52
+
53
+ return "".join(plain_chars), spans
54
+
55
+ df = pd.read_xml(f"{split}-corpus.xml", dtype_backend="pyarrow")
56
+ # Reuse the relation parsing from convert_for_causality_identification
57
+ def map_label(label: str) -> list:
58
+ if label == "Non-Causal":
59
+ return []
60
+ relations = []
61
+ for t in label[len("Cause-Effect("):-1].split("),("):
62
+ left, right = t.strip("()").split(",")
63
+ relations.append({"relationship": 1, "first": left, "second": right})
64
+ return relations
65
+
66
+ df["relations"] = df["label"].apply(map_label)
67
+ df[["text", "entity"]] = df.apply(
68
+ lambda row: pd.Series(extract_entity_spans(row["sentence"], row["relations"])),
69
+ axis=1,
70
+ )
71
  df["index"] = df["id"].apply(lambda x: f"scite_{split}_{x}")
72
+ df = df[["index", "text", "entity"]].set_index("index")
 
73
  df.to_parquet(f"./causal-candidate-extraction/{split}.parquet", engine="pyarrow")
74
 
75
  def convert_for_causality_identification(split: Split) -> None: