Download test_temporal_keys.py from jkorstad/temporal-context-gap-e1: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/datasets/jkorstad/temporal-context-gap-e1/resolve/main/test_temporal_keys.py
- Command line
-
hf download hf://datasets/jkorstad/temporal-context-gap-e1/test_temporal_keys.py
-
curl -L -o test_temporal_keys.py https://huggingface.co/datasets/jkorstad/temporal-context-gap-e1/resolve/main/test_temporal_keys.py
3.7 kB
| """Unit checks for temporal_keys. Plain asserts - runs with `python test_temporal_keys.py`.""" | |
| import random | |
| from temporal_keys import ( | |
| DriftAnnotation, TemporalKey, annotate_drift, load_lsc_drift_data, | |
| make_key, selfanchor_density, | |
| ) | |
| def test_round_trip_full(): | |
| k = TemporalKey( | |
| time="1997-11", fetch="2014-11", src="news.wire", region="uk", | |
| register="journalistic", | |
| drift=[DriftAnnotation("cell", 1850, 1970, "prison-room"), | |
| DriftAnnotation("cell", 1971, None, "biological-unit")], | |
| selfanchor=0.31, | |
| ) | |
| k2 = TemporalKey.parse(k.serialize()) | |
| assert k2 == k, (k.serialize(), k2) | |
| print("ok full round-trip:", k.serialize()) | |
| def test_paper_example(): | |
| s = '<KEY time=1997-11 fetch=2014-11 src=news.wire region=uk register=journalistic drift="cell:1850-1970:prison-room|cell:1971-:biological-unit" selfanchor=0.31 />' | |
| k = TemporalKey.parse(s) | |
| assert k.time == "1997-11" and k.src == "news.wire" | |
| assert len(k.drift) == 2 and k.drift[0].term == "cell" and k.drift[0].year_start == 1850 | |
| assert k.selfanchor == 0.31 | |
| assert TemporalKey.parse(k.serialize()) == k | |
| print("ok paper-example parses and round-trips") | |
| def test_minimal_and_partial(): | |
| k = TemporalKey(time="1839-06") | |
| assert TemporalKey.parse(k.serialize()) == k | |
| k2 = TemporalKey.parse('<KEY src=web.blog />') | |
| assert k2.src == "web.blog" and k2.time is None and k2.drift == [] | |
| print("ok minimal / partial keys") | |
| def test_mask(): | |
| rng = random.Random(0) | |
| k = TemporalKey(time="2001-05", fetch="2014-01", src="news.wire") | |
| for _ in range(50): | |
| m = k.mask(rng, p=0.9) | |
| TemporalKey.parse(m.serialize()) # masked keys still serialize | |
| print("ok mask drops fields and stays serializable") | |
| def test_annotate_drift(): | |
| lsc = [{"term": "cell", "senses": [ | |
| {"year_start": 1850, "year_end": 1970, "sense": "prison-room"}, | |
| {"year_start": 1971, "year_end": None, "sense": "biological-unit"}]}] | |
| text = "He spent the night in a monastic cell." | |
| assert [a.sense for a in annotate_drift(text, lsc, 1900)] == ["prison-room"] | |
| assert [a.sense for a in annotate_drift(text, lsc, 2020)] == ["biological-unit"] | |
| assert len(annotate_drift(text, lsc, None)) == 2 | |
| assert annotate_drift("nothing relevant here", lsc, 1900) == [] | |
| print("ok era-conditional drift annotation") | |
| def test_selfanchor(): | |
| plain = "The dog ran across the yard and barked at the mail carrier." | |
| dated = "As of 2020 the treaty had lapsed; in March 1943 the convoy sailed." | |
| assert selfanchor_density(plain) == 0.0 | |
| assert selfanchor_density(dated) > 0.0 | |
| assert selfanchor_density("") == 0.0 | |
| print("ok selfanchor density") | |
| def test_make_key(): | |
| k = make_key("Written in 1912, the letter mentions a gay ball.", time="1912", src="letters") | |
| assert k.time == "1912" and k.selfanchor > 0 | |
| print("ok make_key convenience constructor:", k.serialize()) | |
| def test_lsc_loader(): | |
| import json, tempfile, os | |
| data = [{"term": "awful", "senses": [ | |
| {"year_start": None, "year_end": 1900, "sense": "awe-inspiring"}, | |
| {"year_start": 1901, "year_end": None, "sense": "very-bad"}]}] | |
| path = os.path.join(tempfile.mkdtemp(), "lsc.json") | |
| with open(path, "w") as f: | |
| json.dump(data, f) | |
| recs = load_lsc_drift_data(path) | |
| assert recs[0]["term"] == "awful" | |
| print("ok LSC loader") | |
| if __name__ == "__main__": | |
| test_round_trip_full() | |
| test_paper_example() | |
| test_minimal_and_partial() | |
| test_mask() | |
| test_annotate_drift() | |
| test_selfanchor() | |
| test_make_key() | |
| test_lsc_loader() | |
| print("ALL TEMPORAL-KEYS TESTS PASSED") |