epub2md/tools/qa/worddiff.py

37 lines
1.3 KiB
Python
Raw Permalink Normal View History

"""Vergleicht die Wörter zweier epub2md-Ausgaben (Datenverlust-Check nach einer Codeänderung).
Aufruf: python tools/qa/worddiff.py ALT_AUSGABE NEU_AUSGABE
Gezählt werden die Wörter aller chapters/*.md ohne Frontmatter, Markup, Link- und Bildziele. Gibt die Wörter
aus, die nur in einer der beiden Ausgaben vorkommen (Multimengen-Differenz).
"""
from __future__ import annotations
import collections
import glob
import re
import sys
def words(out_dir: str) -> collections.Counter[str]:
counter: collections.Counter[str] = collections.Counter()
for path in sorted(glob.glob(out_dir + "/chapters/*.md")):
with open(path, encoding="utf-8") as fh:
text = fh.read()
text = re.sub(r"\A---\n.*?\n---\n", "", text, flags=re.S)
text = re.sub(r"\]\([^)]*\)|<[^>]+>|[*_`#>|\\\[\]↪➥]", " ", text)
counter.update(re.findall(r"\w+", text))
return counter
def main() -> None:
old, new = words(sys.argv[1]), words(sys.argv[2])
lost, gained = old - new, new - old
print(f"Wörter alt {sum(old.values())}, neu {sum(new.values())}; "
f"nur alt {sum(lost.values())}, nur neu {sum(gained.values())}")
print("nur alt:", lost.most_common(25))
print("nur neu:", gained.most_common(25))
if __name__ == "__main__":
main()