Datasets:
Matthew Franglen
commited on
Commit
•
cfa80ae
1
Parent(s):
67b8661
Get the sem-eval converter working
Browse files- src/alignment.py +16 -4
- src/main.py +24 -3
src/alignment.py
CHANGED
@@ -14,10 +14,11 @@ def find_closest_text(
|
|
14 |
) -> pd.Series:
|
15 |
# Returns a series of the replacement values aligned to the original values
|
16 |
no_space_replacements = {text.replace(" ", ""): text for text in replacement}
|
17 |
-
|
|
|
18 |
non_perfect_matches = result.isna().sum()
|
19 |
|
20 |
-
assert non_perfect_matches / len(original) <= 0.
|
21 |
"Poor alignment with replacement text. "
|
22 |
f"{non_perfect_matches:,} of {len(original),} rows did not match well"
|
23 |
)
|
@@ -28,7 +29,8 @@ def find_closest_text(
|
|
28 |
)
|
29 |
return replacement.iloc[distances.argmin()]
|
30 |
|
31 |
-
result.loc[result.isna()] =
|
|
|
32 |
return result
|
33 |
|
34 |
|
@@ -63,6 +65,16 @@ def to_character_indices(
|
|
63 |
)
|
64 |
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
def to_aligned_character_indices(
|
67 |
*,
|
68 |
original: str,
|
@@ -147,7 +159,7 @@ def _aligned_start_index(
|
|
147 |
def _aligned_end_index(
|
148 |
text: str, original_index: int, indices: list[Optional[int]]
|
149 |
) -> int:
|
150 |
-
closest_before =
|
151 |
index for index in indices if index is not None and index <= original_index
|
152 |
)
|
153 |
index = indices.index(closest_before)
|
|
|
14 |
) -> pd.Series:
|
15 |
# Returns a series of the replacement values aligned to the original values
|
16 |
no_space_replacements = {text.replace(" ", ""): text for text in replacement}
|
17 |
+
original_text = original.str.replace(" ", "")
|
18 |
+
result = original_text.map(no_space_replacements)
|
19 |
non_perfect_matches = result.isna().sum()
|
20 |
|
21 |
+
assert non_perfect_matches / len(original) <= 0.10, (
|
22 |
"Poor alignment with replacement text. "
|
23 |
f"{non_perfect_matches:,} of {len(original),} rows did not match well"
|
24 |
)
|
|
|
29 |
)
|
30 |
return replacement.iloc[distances.argmin()]
|
31 |
|
32 |
+
result.loc[result.isna()] = original_text[result.isna()].apply(closest)
|
33 |
+
result = result.str.strip()
|
34 |
return result
|
35 |
|
36 |
|
|
|
65 |
)
|
66 |
|
67 |
|
68 |
+
def to_aligned_character_indices_series(row: pd.Series) -> pd.Series:
|
69 |
+
indices = to_character_indices(triplet=row.triples, text=row.original)
|
70 |
+
result = to_aligned_character_indices(
|
71 |
+
original=row.original,
|
72 |
+
replacement=row.text,
|
73 |
+
original_indices=indices,
|
74 |
+
)
|
75 |
+
return pd.Series(asdict(result))
|
76 |
+
|
77 |
+
|
78 |
def to_aligned_character_indices(
|
79 |
*,
|
80 |
original: str,
|
|
|
159 |
def _aligned_end_index(
|
160 |
text: str, original_index: int, indices: list[Optional[int]]
|
161 |
) -> int:
|
162 |
+
closest_before = max(
|
163 |
index for index in indices if index is not None and index <= original_index
|
164 |
)
|
165 |
index = indices.index(closest_before)
|
src/main.py
CHANGED
@@ -3,8 +3,12 @@ from typing import Annotated
|
|
3 |
|
4 |
import typer
|
5 |
|
6 |
-
from .alignment import
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
from .sentiment import to_nice_sentiment
|
9 |
|
10 |
app = typer.Typer()
|
@@ -37,7 +41,24 @@ def sem_eval(
|
|
37 |
sem_eval_file: Annotated[Path, typer.Option()],
|
38 |
output_file: Annotated[Path, typer.Option()],
|
39 |
) -> None:
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
if __name__ == "__main__":
|
|
|
3 |
|
4 |
import typer
|
5 |
|
6 |
+
from .alignment import (
|
7 |
+
find_closest_text,
|
8 |
+
to_aligned_character_indices_series,
|
9 |
+
to_character_indices_series,
|
10 |
+
)
|
11 |
+
from .data import read_aste_file, read_sem_eval_file
|
12 |
from .sentiment import to_nice_sentiment
|
13 |
|
14 |
app = typer.Typer()
|
|
|
41 |
sem_eval_file: Annotated[Path, typer.Option()],
|
42 |
output_file: Annotated[Path, typer.Option()],
|
43 |
) -> None:
|
44 |
+
df = read_aste_file(aste_file)
|
45 |
+
sem_eval_df = read_sem_eval_file(sem_eval_file)
|
46 |
+
|
47 |
+
df["original"] = df.text
|
48 |
+
df["text"] = find_closest_text(original=df.original, replacement=sem_eval_df.text)
|
49 |
+
df = df.explode("triples")
|
50 |
+
df = df.reset_index(drop=False)
|
51 |
+
df = df.merge(
|
52 |
+
df.apply(to_aligned_character_indices_series, axis="columns"),
|
53 |
+
left_index=True,
|
54 |
+
right_index=True,
|
55 |
+
)
|
56 |
+
df["sentiment"] = df.triples.apply(lambda triple: to_nice_sentiment(triple[2]))
|
57 |
+
df = df.drop(columns=["original", "triples"])
|
58 |
+
|
59 |
+
print(df.sample(3))
|
60 |
+
|
61 |
+
df.to_parquet(output_file, compression="gzip")
|
62 |
|
63 |
|
64 |
if __name__ == "__main__":
|