polish-dynaword / src /count_vocab.py
kacperwikiel's picture
Squash: collapse LFS history to reclaim private storage
cfe406c
Raw
History Blame Contribute Delete
1.19 kB
"""Full-corpus unique word count. Streams parquet batches into a set.
ponytail: lowercased surface forms (Polish is heavily inflected, so this is
'distinct word forms', not lemmas). Counts total words too.
"""
import re
import pyarrow.parquet as pq
SOURCES = ["eurlex", "parliamentary", "wikisource", "wikipedia",
"dziennik_ustaw", "wolne_lektury"]
WORD = re.compile(r"[^\W\d_]+", re.UNICODE) # unicode letters, no digits/punct
total_vocab = set()
total_words = 0
print(f"{'source':<16} {'words':>14} {'uniq forms':>13}")
for s in SOURCES:
pf = pq.ParquetFile(f"data/{s}/{s}.parquet")
src_vocab = set()
src_words = 0
for batch in pf.iter_batches(columns=["text"], batch_size=2000):
for t in batch.column("text").to_pylist():
if not t:
continue
w = WORD.findall(t.lower())
src_words += len(w)
src_vocab.update(w)
total_words += src_words
total_vocab |= src_vocab
print(f"{s:<16} {src_words:>14,} {len(src_vocab):>13,}", flush=True)
print(f"\n{'TOTAL (deduped)':<16} {total_words:>14,} {len(total_vocab):>13,}")
print(f"type/token ratio: {len(total_vocab)/total_words:.5f}")