"""Body lines mentioning BKLNW / Buthe / Johnston-Yang / FKS(2) and their tables or key constants. Usage: python -I grep_ctx2.py pdfs ID [ID...]""" import sys, os, re sys.stdout.reconfigure(encoding="utf-8") PAT = re.compile(r'Broadbent|BKLNW|Bro\+|BKL\+|B.?.?uthe|Buthe|Johnston.{0,3}Yang|JY2|\[JY|Fiori|FKS|Swidinsky|Table 8\b|Table 15\b|1\.93378|0\.024334|57\.184|9\.39|1\.570|9\.2211|121\.1|0\.4298|2\.9 ?· ?10|1\.2 ?· ?10') for pid in sys.argv[2:]: lines = open(os.path.join(sys.argv[1], pid + '.mu.txt'), encoding='utf-8', errors='replace').read().split('\n') end = len(lines) for i in range(len(lines) - 1, 0, -1): if re.match(r'\s*(References|REFERENCES|Bibliography|BIBLIOGRAPHY)\s*$', lines[i]): end = i; break print('#####', pid, '(body lines', end, ')') last = -10 for i in range(end): if PAT.search(lines[i]) and i > last + 1: ctx = ' '.join(x.strip() for x in lines[max(0, i - 2):i + 3]) print(f' l{i}: {ctx[:400]}') last = i