""" Coverage report for a Kashmiri text corpus, plus a frequency-ranked review sheet. python tools/coverage.py metadata.jsonl --field asr_text --top 5000 --out review.tsv Prints: running words, distinct words, characters the reader has no rule for, and how much of the running text the top-N words cover. The review sheet lists the top-N words with their IPA and Devanagari so a Kashmiri speaker can mark wrong readings; corrections then go into an exceptions list. """ import argparse import collections import json import sys import unicodedata try: # optional: a Devanagari column in the review sheet helps reviewers from ks_devanagari import transliterate_word except ImportError: def transliterate_word(word): return '' from ks_g2p._reader import normalize_word, tokenize from ks_g2p import word_to_ipa from ks_g2p.ipa import CONSONANTS # Characters the reader has a rule for (after normalize_word) KNOWN = set(CONSONANTS) | set('اٲٳۄۆےؠعءئھ') | set( 'ًٌٍَُِّْٕٓٔ' 'ٰٖٟٗ٘ٚٛ̂‌' ) def main(argv=None): p = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) p.add_argument('input', help='JSONL (with --field) or plain text, one sentence per line') p.add_argument('--field', help='JSONL text field') p.add_argument('--top', type=int, default=5000, help='words in the review sheet') p.add_argument('--out', help='review sheet (TSV)') a = p.parse_args(argv) sys.stdout.reconfigure(encoding='utf-8') freq = collections.Counter() with open(a.input, encoding='utf-8') as f: for line in f: if not line.strip(): continue text = json.loads(line).get(a.field, '') if a.field else line for tok, is_word in tokenize(text): if is_word: freq.update(w for w in tok.split('‌') if w) total = sum(freq.values()) unknown = collections.Counter() unknown_words = 0 for word, n in freq.items(): bad = {ch for ch in normalize_word(word) if ch not in KNOWN} if bad: unknown_words += n for ch in bad: unknown[ch] += n print(f'running words {total:,}') print(f'distinct words {len(freq):,}') print(f'words with a character the reader has no rule for: {unknown_words:,} ({100 * unknown_words / max(total, 1):.3f}%)') for ch, n in unknown.most_common(20): print(f' U+{ord(ch):04X} {unicodedata.name(ch, "?"):45s} {n:,}') ranked = freq.most_common() for k in (100, 1000, 2000, 5000, 10000): if k <= len(ranked): cov = sum(n for _, n in ranked[:k]) print(f'top {k:>6,} words cover {100 * cov / total:5.1f}% of running text') if a.out: cum = 0 with open(a.out, 'w', encoding='utf-8', newline='\n') as out: out.write('rank\tcount\tcum_pct\tword\tipa\tdevanagari\tcorrect_ipa\n') for i, (word, n) in enumerate(ranked[:a.top], 1): cum += n out.write(f'{i}\t{n}\t{100 * cum / total:.2f}\t{word}\t{word_to_ipa(word)}\t' f'{transliterate_word(word)}\t\n') print(f'review sheet: {a.out}') if __name__ == '__main__': main()