"""Count benchmark records that GLiNER truncates. Usage: python -I count_truncation.py DATA_DIR OUT_JSON
Rule: GLiNER multi PII reads at most 384 words per call (gliner_config.json max_len). Words are counted with
GLiNER's own default splitter (WhitespaceTokenSplitter). Records are the first 300 en / 100 de / 100 fr of the
seeded sample that run.py scores GLiNER on."""
import sys, json, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import run
from gliner.data_processing.tokenizer import WhitespaceTokenSplitter

MAX_LEN = 384
split = WhitespaceTokenSplitter()
recs = run.load()
out = {"method": "Words per record counted with GLiNER default splitter; a record is truncated when it has more than max_len_words words. Records: the 500 GLiNER scored (first 300 en, 100 de, 100 fr of the seeded sample). run.log holds 68 truncation warnings because Python prints one per distinct length.", "max_len_words": MAX_LEN, "splitter": "gliner.data_processing.tokenizer.WhitespaceTokenSplitter", "per_language": {}}
tot = over = 0
for lg, n in run.GN.items():
    lens = [sum(1 for _ in split(r["text"])) for r in recs[lg][:n]]
    k = sum(1 for x in lens if x > MAX_LEN)
    out["per_language"][lg] = {"records": n, "over_384": k, "max_length": max(lens)}
    tot += n; over += k
out["records"] = tot
out["records_over_384"] = over
json.dump(out, open(sys.argv[2], "w"), indent=1)
print(json.dumps(out, indent=1))
