File size: 5,120 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""R5: font table of generator 0.3.0. For every registry font and every language: does the font draw ALL letters of the language's alphabet
(glyph gate, fonts.FontFace.good_chars), which letters are missing, what is its weight; per script and per language: number of fully covering
fonts, expected share of the most frequent font under equal weights (train pool = without the held-out fonts), against the 12 percent cap.

usage: python fonts_report.py --out-tsv F --out-json F [--fonts-dir DIR] [--heldout-cyrl a,b --heldout-latn c,d]
"""
import argparse
import hashlib
import json
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
ap = argparse.ArgumentParser()
ap.add_argument("--out-tsv", required=True)
ap.add_argument("--out-json", required=True)
ap.add_argument("--fonts-dir", default=None)
ap.add_argument("--heldout-cyrl", default="playpen,shantell")
ap.add_argument("--heldout-latn", default="mynerve,mali")
a = ap.parse_args()
if a.fonts_dir:
    os.environ["PAGESYNTH_FONTS"] = a.fonts_dir
from pagesynth import fonts as F  # noqa: E402
from pagesynth import text as T  # noqa: E402

LANGS = ["kk", "ky", "uz_cyrl", "tt", "ba", "sah", "uz_latn", "tr", "az", "tk"]
held = set(a.heldout_cyrl.split(",") + a.heldout_latn.split(","))


def letters(lang):
    return sorted(c for c in T.alphabet(lang) if c.isalpha())


def missing(face, lang):
    good = face.good_chars()
    cm = face.cmap
    ls = letters(lang)
    if lang == "uz_latn":
        m = [c for c in ls if c not in (T.UZ_TURN, T.UZ_APOS) and c not in good]
        for mark, alts in ((T.UZ_TURN, (T.UZ_TURN, "‘", "'")), (T.UZ_APOS, (T.UZ_APOS, "’", "'"))):
            if not any(x in good for x in alts):
                m.append(mark)
        return m
    return [c for c in ls if c not in good]


rows = []
cover = {}
for spec in F.REGISTRY:
    face = F.get_face(spec["id"])
    h = hashlib.sha256(open(face.path, "rb").read()).hexdigest()
    r = dict(font_id=spec["id"], family=spec["family"], kind=spec["kind"], licence_class=spec["licence_class"], file=spec["file"], sha256=h, connected=int(spec["connected"]),
             axes=",".join(sorted(face.axis_tags)), heldout=int(spec["id"] in held), weight_cyrl=spec["w"].get("Cyrl", 0), weight_latn=spec["w"].get("Latn", 0))
    for l in LANGS:
        m = missing(face, l)
        r["covers_" + l] = "yes" if not m else "no"
        r["missing_" + l] = "".join(m)
        cover[(spec["id"], l)] = not m
    r["missing_digits_punct"] = "".join(sorted(c for c in (T.DIGITS + T.PUNCT) if c not in face.good_chars()))
    rows.append(r)
cols = list(rows[0].keys())
with open(a.out_tsv, "w", encoding="utf-8") as f:
    f.write("\t".join(cols) + "\n")
    for r in rows:
        f.write("\t".join(str(r[c]) for c in cols) + "\n")
summ = dict(per_language={}, per_script={})
for l in LANGS:
    script = T.PROFILES[l]["script"]
    hand = [s for s in F.REGISTRY if s["kind"] == "hand" and s["w"].get(script, 0) > 0 and cover[(s["id"], l)]]
    open_hand = [s for s in hand if s["licence_class"] != "personal_use_only"]
    train = [s for s in hand if s["id"] not in held]
    summ["per_language"][l] = dict(script=script, hand_fonts_full_cover=len(hand), open_fonts_full_cover=len(open_hand), personal_use_fonts_full_cover=len(hand) - len(open_hand),
                                   train_pool_fonts=len(train), max_share_equal_weights_all=round(1.0 / max(1, len(hand)), 4), max_share_equal_weights_train=round(1.0 / max(1, len(train)), 4),
                                   meets_12_percent_cap_train=bool(1.0 / max(1, len(train)) <= 0.12), font_ids=[s["id"] for s in hand],
                                   heldout_full_cover=[s["id"] for s in hand if s["id"] in held],
                                   print_fonts_full_cover=len([s for s in F.REGISTRY if s["kind"] == "print" and s["w"].get(script, 0) > 0 and cover[(s["id"], l)]]))
for script in ("Cyrl", "Latn"):
    hand = [s for s in F.REGISTRY if s["kind"] == "hand" and s["w"].get(script, 0) > 0]
    fully_any = [s for s in hand if any(cover[(s["id"], l)] for l in LANGS if T.PROFILES[l]["script"] == script)]
    summ["per_script"][script] = dict(registry_hand_fonts=len(hand), fully_covers_at_least_one_language=len(fully_any),
                                      open=len([s for s in fully_any if s["licence_class"] != "personal_use_only"]), personal_use=len([s for s in fully_any if s["licence_class"] == "personal_use_only"]))
summ["heldout"] = sorted(held)
with open(a.out_json, "w", encoding="utf-8") as f:
    json.dump(summ, f, indent=1, ensure_ascii=False)
for l in LANGS:
    s = summ["per_language"][l]
    print(l, s["script"], "full-cover hand fonts", s["hand_fonts_full_cover"], "(open %d, personal %d)" % (s["open_fonts_full_cover"], s["personal_use_fonts_full_cover"]), "train pool", s["train_pool_fonts"],
          "max share train %.3f" % s["max_share_equal_weights_train"], "cap ok" if s["meets_12_percent_cap_train"] else "CAP NOT MET", "heldout covering:", s["heldout_full_cover"], "print", s["print_fonts_full_cover"])
print(summ["per_script"])