#!/usr/bin/env python3 """Tabulate which sources contain the rarely used MathML elements. Usage: element_coverage.py DIR SOURCE [SOURCE ...] > table.md For every element in the rare list and for the Content MathML elements, the table gives the number of distinct formulas containing it per source, and a second table lists the encodings of `annotation` and `annotation-xml` elements per source. """ import collections import json import os import re import sys import corpus_common as cc RARE = ["mfenced", "mmultiscripts", "mprescripts", "menclose", "mpadded", "mphantom", "maction", "mlabeledtr", "ms", "merror", "semantics", "annotation", "annotation-xml", "mglyph", "maligngroup", "malignmark", "mstack", "mlongdiv", "mscarries", "mstyle", "mtable", "none"] CONTENT = ["apply", "ci", "cn", "csymbol", "bvar", "lambda", "piecewise", "piece", "otherwise", "plus", "minus", "times", "divide", "power", "root", "eq", "gt", "lt", "geq", "leq", "and", "or", "not", "exp", "ln", "sin", "cos", "diff", "int", "sum", "partialdiff", "factorial", "floor", "ceiling", "abs", "set", "vector", "matrix", "interval", "limit", "forall"] ENC = re.compile(r"<(?:\w+:)?annotation(?:-xml)?\b[^>]*\bencoding\s*=\s*[\"']([^\"']*)[\"']", re.I) def main(): directory, sources = sys.argv[1], sys.argv[2:] cnt = {s: collections.Counter() for s in sources} enc = {s: collections.Counter() for s in sources} size = {} for s in sources: n = 0 for line in open(os.path.join(directory, s + ".jsonl"), encoding="utf-8"): n += 1 m = json.loads(line)["mathml"] cnt[s].update(cc.element_names(m)) enc[s].update(ENC.findall(m)) size[s] = n for title, names in (("Rare presentation elements", RARE), ("Content MathML elements", CONTENT)): print("\n### %s (distinct formulas containing the element)\n" % title) print("| element | " + " | ".join(sources) + " |") print("|---|" + "---:|" * len(sources)) for e in names: row = [cnt[s].get(e, 0) for s in sources] if any(row): print("| `%s` | " % e + " | ".join(str(v) if v else "" for v in row) + " |") print("\n### Annotation encodings\n") for s in sources: if enc[s]: print("- %s: %s" % (s, ", ".join("`%s` %d" % kv for kv in enc[s].most_common(8)))) if __name__ == "__main__": main()