Download ner/add_system.py from iapp/OpenThai2.0-NLP-Core-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 3.63 kB
-
https://huggingface.co/datasets/iapp/OpenThai2.0-NLP-Core-Benchmark/resolve/main/ner/add_system.py
- Command line
-
hf download hf://datasets/iapp/OpenThai2.0-NLP-Core-Benchmark/ner/add_system.py
-
curl -L -o add_system.py https://huggingface.co/datasets/iapp/OpenThai2.0-NLP-Core-Benchmark/resolve/main/ner/add_system.py
3.63 kB
| """Add a system to the table of README.md. | |
| python add_system.py NAME --from FILE --display "Name in the table" [--input "how it was called"] | |
| [--kind rival|reference] [--learnt-test] [--replace] | |
| FILE holds one line per test text, {"id": ..., "entities": [[start, end, type], ...]}, with no text (README.md, Data | |
| format). It is checked, copied to predictions/NAME/thainer22-test.jsonl and scored with the same scorer. Its row goes | |
| to results/expected.json with the interval of our model minus it in every view, and the table of README.md is | |
| written again. --learnt-test marks a system that learnt the test texts: its scores go in parentheses. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import re | |
| import sys | |
| from pathlib import Path | |
| HERE = Path(__file__).resolve().parent | |
| sys.path.insert(0, str(HERE)) | |
| from benchmark import check_entities, write # noqa: E402 | |
| from build_testsets import prepare # noqa: E402 | |
| from score import EXPECTED, OURS, SET, VIEWS, read_outputs, score_system, table_md # noqa: E402 | |
| from scoring.ner import bootstrap, read # noqa: E402 | |
| def main() -> None: | |
| ap = argparse.ArgumentParser(description=__doc__.split("\n")[0]) | |
| ap.add_argument("name") | |
| ap.add_argument("--from", dest="src", required=True) | |
| ap.add_argument("--display", required=True) | |
| ap.add_argument("--input", default="text") | |
| ap.add_argument("--kind", choices=("rival", "reference"), default="rival") | |
| ap.add_argument("--learnt-test", action="store_true") | |
| ap.add_argument("--replace", action="store_true") | |
| a = ap.parse_args() | |
| if not re.fullmatch(r"[a-z0-9][a-z0-9._-]*", a.name): | |
| raise SystemExit("NAME: lower-case letters, digits, '.', '_' and '-'") | |
| if a.name == OURS: | |
| raise SystemExit(f"{OURS} is our row") | |
| gold = read(prepare()) | |
| pred = read_outputs(Path(a.src), gold) | |
| for k, r in pred.items(): | |
| if set(r) != {"id", "entities"}: | |
| raise SystemExit(f"{k}: a line holds only id and entities (no text): {sorted(r)}") | |
| r["entities"] = check_entities(k, gold[k]["text"], [tuple(e) for e in r["entities"]]) | |
| exp = json.loads(EXPECTED.read_text(encoding="utf-8")) | |
| if a.name in exp["systems"] and not a.replace: | |
| raise SystemExit(f"{a.name} is in the table already (--replace to score it again)") | |
| write(pred, HERE / "predictions" / a.name / f"{SET}.jsonl") | |
| res, cs = score_system(gold, pred) | |
| _, ours = score_system(gold, read_outputs(HERE / "predictions" / OURS / f"{SET}.jsonl", gold)) | |
| exp["systems"][a.name] = { | |
| "display": a.display, "input": a.input, "kind": a.kind, "learnt_test": a.learnt_test, "model": None, | |
| "revision": None, "packages": {}, "f1": {v: round(res[v]["f1"], 2) for v in VIEWS}, | |
| "shown": {v: round(res[v]["f1"], 1) for v in VIEWS}, | |
| "p": {v: round(res[v]["p"], 2) for v in VIEWS}, "r": {v: round(res[v]["r"], 2) for v in VIEWS}} | |
| exp["compared"][a.name] = {v: bootstrap(ours[v], cs[v]) for v in VIEWS} | |
| EXPECTED.write_text(json.dumps(exp, ensure_ascii=False, indent=1) + "\n", encoding="utf-8") | |
| card = (HERE / "README.md").read_text(encoding="utf-8") | |
| head, rest = card.split("<!-- table:start -->\n", 1) | |
| _, tail = rest.split("\n<!-- table:end -->", 1) | |
| (HERE / "README.md").write_text(head + "<!-- table:start -->\n" + table_md(exp) + "\n<!-- table:end -->" + tail, | |
| encoding="utf-8") | |
| print(f"{a.name}: " + ", ".join(f"{v} {res[v]['f1']:.2f}" for v in VIEWS) + "; added to results/expected.json " | |
| "and README.md") | |
| if __name__ == "__main__": | |
| main() | |