tacitus-source-lab / toolkit /tests /test_toolkit.py
Giuliocc's picture
Release browser inference, models, and data v0.2.0
2e39601 verified
Raw History Blame Contribute Delete
4.29 kB
import json
import tempfile
import unittest
from pathlib import Path
from app import highlight, interface
from tacitus_nlp import Events, source_span
from tacitus_nlp.data import convert, few_nerd, maven, vitamin
class ContractTests(unittest.TestCase):
def test_unicode_offsets_and_escaped_preview(self):
text = "🗳️ <b>Río</b>"
start = text.index("Río")
span = source_span(text, start, start + 3, "Río")
self.assertIn("&lt;b&gt;<mark", highlight(text, [span]))
with self.assertRaises(ValueError):
source_span(text, start + 1, start + 4, "Río")
def test_same_type_event_mentions_have_distinct_ids_and_invert_role_offsets(self):
text = "🔔 Ada protested and Bea protested."
actor_schema = {"Participant": "Name of the person protesting"}
event = Events(None, {"repo": "test", "revision": "fixture"})
event.role_schema = {"Protest": actor_schema}
def fake_windows(source, schema):
if schema is actor_schema:
name = "Ada" if source.index("[TARGET]") < source.index("Bea") else "Bea"
start = source.index(name)
return [{"start": start, "end": start + len(name), "text": name,
"score": 1.0, "type": "Participant"}]
first, second = source.index("protested"), source.rindex("protested")
return [{"start": start, "end": start + 9, "text": "protested", "type": "Protest"}
for start in (first, second)]
event._windows = fake_windows
rows = event.extract(text)["events"]
self.assertNotEqual(rows[0]["event_mention_id"], rows[1]["event_mention_id"])
for row, name in zip(rows, ("Ada", "Bea"), strict=True):
span = row["participants"][0]
self.assertEqual(text[span["start"]:span["end"]], name)
def test_ui_has_three_prediction_and_three_download_actions(self):
self.assertEqual(len(interface().config["dependencies"]), 6)
class ConverterTests(unittest.TestCase):
def test_vitaminc_discards_canary_and_retains_case(self):
row = vitamin({"evidence": "Text", "claim": "Claim", "label": "REFUTES",
"case_id": "c1", "big_bench_canary": "never retain"})
self.assertEqual(row["label"], "contradicted")
self.assertIn("case_id:c1", row["groups"])
self.assertNotIn("never retain", json.dumps(row))
def test_few_nerd_preserves_exact_reconstructed_spans(self):
row = few_nerd({"tokens": ["🗳", "Juniper", "Town", "Council", "met"],
"fine_ner_tags": [0, 1, 1, 1, 0]},
names=["O", "organization-government/governmentagency"])
span = row["mentions"][0]
self.assertEqual(row["text"][span["start"]:span["end"]], "Juniper Town Council")
self.assertEqual(span["type"], "government_institution")
def test_maven_resolves_entity_references_to_source_spans(self):
row = maven({"id": "d", "title": "Test", "document": "Ada protested.",
"events": [{"id": "e", "type": "Protest",
"mention": [{"id": "t", "trigger_word": "protested", "offset": [4, 13]}],
"argument": {"Participant": [{"entity_id": "p"}]}}],
"entities": [{"id": "p", "mention": [{"id": "m", "mention": "Ada", "offset": [0, 3]}]}]})
span = row["events"][0]["participants"][0]
self.assertEqual(row["text"][span["start"]:span["end"]], "Ada")
self.assertEqual(span["source_mention_id"], "m")
def test_split_audit_rejects_duplicate_and_keeps_previous_output(self):
row = {"evidence": "Evidence", "claim": "a claim", "label": "SUPPORTS", "case_id": "c"}
with tempfile.TemporaryDirectory() as folder:
source, output = (Path(folder) / name for name in ("source", "out"))
source.write_text(json.dumps(row) + "\n")
output.write_text("previous")
with self.assertRaisesRegex(ValueError, "crosses partitions"):
convert("evidence", [("train", source), ("test", source)], output)
self.assertEqual(output.read_text(), "previous")
if __name__ == "__main__":
unittest.main()