#!/usr/bin/env python3
"""Keyword scan of the v3 LaTeX source (main.tex, Sections, Tables, in the order they are \\input). Case-insensitive,
word-boundary-free substring and whole-word counts. Prints file:line and a 110 char window around the hit."""
import os, re, sys

root = sys.argv[1]
KW = ['prompt', 'length', 'words', 'characters', 'format', 'JSON', 'system', 'temperature', 'top_p', 'top_k', 'max_tokens',
      'sampling', 'self-consistency', 'majority', 'mean', 'vote', 'normalize', 'scale', 'average', 'AVG', 'filter',
      'exclude', 'retry', 'fail', 'translation', 'version', 'date', 'seed']
paths = ['main.tex']
for d in ('Sections', 'Tables'):
    for f in sorted(os.listdir(os.path.join(root, d))):
        if f.endswith('.tex'):
            paths.append(os.path.join(d, f))
SKIP_CASE = re.compile(r'^Tables/case_')
for kw in KW:
    pat = re.compile(re.escape(kw), re.I)
    total = 0
    per = {}
    hits = []
    for p in paths:
        with open(os.path.join(root, p), encoding='utf-8', errors='replace') as f:
            for n, line in enumerate(f, 1):
                for m in pat.finditer(line):
                    total += 1
                    per[p] = per.get(p, 0) + 1
                    s = max(0, m.start() - 50)
                    hits.append((p, n, re.sub(r'\s+', ' ', line[s:m.end() + 60]).strip()))
    print(f'\n#### {kw}: {total} hits; per file {per}')
    for p, n, w in hits:
        # copyrighted scene tables only counted, not excerpted
        if SKIP_CASE.match(p):
            continue
        print(f'{p}:{n}: {w}')
