kacperwikiel commited on
Commit
0ea96e2
·
1 Parent(s): 0538a97

Add phrase frequency stats and HF card snippet for Polish DynaWord

Browse files
README.md CHANGED
@@ -124,3 +124,46 @@ CHANGELOG.
124
  python3 src/build_dynaword.py --all --speakleash-dir <speakleash_zst_dir> --out .
125
  python3 src/make_docs.py
126
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  python3 src/build_dynaword.py --all --speakleash-dir <speakleash_zst_dir> --out .
125
  python3 src/make_docs.py
126
  ```
127
+
128
+ ## Corpus phrase frequency (normalized by tokens)
129
+
130
+ To show how frequent legal and discourse markers are across the corpus, we can report counts normalized by token count per source and globally. Raw counts + percentages are generated from the current parquet data and source token counts:
131
+
132
+ ```bash
133
+ python3 src/pattern_frequency_report.py --data-root . \
134
+ --out-md pattern_frequency_report.md \
135
+ --out-hf artifacts/pattern_frequency_hf_snippet.md \
136
+ --out-png artifacts/pattern_frequency.png
137
+ ```
138
+
139
+ `pattern_frequency_report.md` contains full source-by-source breakdown.
140
+ `artifacts/pattern_frequency_hf_snippet.md` is the exact block for Hugging Face model card.
141
+
142
+ | pattern | count | share of all corpus tokens |
143
+ |---|---:|---:|
144
+ | `w roku` | 434,882 | 0.0070% |
145
+ | `klasyfikacji` | 129,963 | 0.0021% |
146
+ | `ustawa` | 586,803 | 0.0094% |
147
+ | `artykuł` | 2,035,630 | 0.0327% |
148
+ | `parlament` | 1,201,401 | 0.0193% |
149
+ | `rozporządzenie` | 1,490,399 | 0.0240% |
150
+ | `w pobliżu` | 77,561 | 0.0012% |
151
+ | `mieszkańców` | 240,332 | 0.0039% |
152
+ | `Dz.U.` | 939,966 | 0.0151% |
153
+
154
+ ![Overall pattern counts](artifacts/pattern_frequency_overall.png)
155
+
156
+ Per-source normalized shares:
157
+ - [w roku](artifacts/pattern_frequency_w_roku.png)
158
+ - [klasyfikacji](artifacts/pattern_frequency_klasyfikacji.png)
159
+ - [ustawa](artifacts/pattern_frequency_ustawa.png)
160
+ - [artykuł](artifacts/pattern_frequency_artykul.png)
161
+ - [parlament](artifacts/pattern_frequency_parlament.png)
162
+ - [rozporządzenie](artifacts/pattern_frequency_rozporządzenie.png)
163
+ - [w pobliżu](artifacts/pattern_frequency_w_pobliżu.png)
164
+ - [mieszkańców](artifacts/pattern_frequency_mieszkańców.png)
165
+ - [Dz.U.](artifacts/pattern_frequency_dzu.png)
166
+
167
+ ### Hugging Face Model Card block
168
+
169
+ Wklej dokładnie `artifacts/pattern_frequency_hf_snippet.md` do sekcji **Results** w model card (`README.md` repozytorium na HF).
artifacts/pattern_frequency_artykul.png ADDED

Git LFS Details

  • SHA256: 5b6b5da297f277b5a925ad8a5ad0f6a645eacd9ba2ccb966e2fc0c9e3e937463
  • Pointer size: 130 Bytes
  • Size of remote file: 50.2 kB
artifacts/pattern_frequency_dzu.png ADDED

Git LFS Details

  • SHA256: d9782c79e15c16e98bf2ead64f2c2a5165c7d2897c0833912f8a632edaad58a7
  • Pointer size: 130 Bytes
  • Size of remote file: 52.7 kB
artifacts/pattern_frequency_hf_snippet.md ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Phrase frequency (token-normalized)
2
+
3
+ ### Global corpus totals
4
+
5
+ Token counts are computed with the same tiktoken proxy used in source stats (`cl100k_base`).
6
+
7
+ | Pattern | Count | Share of total tokens |
8
+ |---|---:|---:|
9
+ | `w roku` | 434,882 | 0.0070% |
10
+ | `klasyfikacji` | 129,963 | 0.0021% |
11
+ | `ustawa` | 586,803 | 0.0094% |
12
+ | `artykuł` | 2,035,630 | 0.0327% |
13
+ | `parlament` | 1,201,401 | 0.0193% |
14
+ | `rozporządzenie` | 1,490,399 | 0.0240% |
15
+ | `w pobliżu` | 77,561 | 0.0012% |
16
+ | `mieszkańców` | 240,332 | 0.0039% |
17
+ | `Dz.U.` | 939,966 | 0.0151% |
18
+
19
+ ![overall-pattern-frequency](artifacts/pattern_frequency_overall.png)
20
+
21
+ ### Per-source token-normalized shares
22
+
23
+ Plots:
24
+ - [w roku](artifacts/pattern_frequency_w_roku.png)
25
+ - [klasyfikacji](artifacts/pattern_frequency_klasyfikacji.png)
26
+ - [ustawa](artifacts/pattern_frequency_ustawa.png)
27
+ - [artykuł](artifacts/pattern_frequency_artykul.png)
28
+ - [parlament](artifacts/pattern_frequency_parlament.png)
29
+ - [rozporządzenie](artifacts/pattern_frequency_rozporządzenie.png)
30
+ - [w pobliżu](artifacts/pattern_frequency_w_pobliżu.png)
31
+ - [mieszkańców](artifacts/pattern_frequency_mieszkańców.png)
32
+ - [Dz.U.](artifacts/pattern_frequency_dzu.png)
33
+
34
+ ### Source-level full table
35
+
36
+ | source | pattern | count | share of source tokens |
37
+ |---|---|---:|---:|
38
+ | dziennik_ustaw | `w roku` | 33,096 | 0.00681% |
39
+ | dziennik_ustaw | `klasyfikacji` | 10,896 | 0.00224% |
40
+ | dziennik_ustaw | `ustawa` | 77,376 | 0.01592% |
41
+ | dziennik_ustaw | `artykuł` | 27,773 | 0.00571% |
42
+ | dziennik_ustaw | `parlament` | 42,338 | 0.00871% |
43
+ | dziennik_ustaw | `rozporządzenie` | 166,383 | 0.03423% |
44
+ | dziennik_ustaw | `w pobliżu` | 1,087 | 0.00022% |
45
+ | dziennik_ustaw | `mieszkańców` | 8,025 | 0.00165% |
46
+ | dziennik_ustaw | `Dz.U.` | 158 | 0.00003% |
47
+ | eltec_pol | `w roku` | 108 | 0.00050% |
48
+ | eltec_pol | `klasyfikacji` | 1 | 0.00000% |
49
+ | eltec_pol | `ustawa` | 153 | 0.00071% |
50
+ | eltec_pol | `artykuł` | 173 | 0.00081% |
51
+ | eltec_pol | `parlament` | 95 | 0.00044% |
52
+ | eltec_pol | `rozporządzenie` | 35 | 0.00016% |
53
+ | eltec_pol | `w pobliżu` | 246 | 0.00114% |
54
+ | eltec_pol | `mieszkańców` | 214 | 0.00100% |
55
+ | eltec_pol | `Dz.U.` | 0 | 0.00000% |
56
+ | eurlex | `w roku` | 40,009 | 0.00168% |
57
+ | eurlex | `klasyfikacji` | 59,428 | 0.00250% |
58
+ | eurlex | `ustawa` | 30,368 | 0.00128% |
59
+ | eurlex | `artykuł` | 1,774,958 | 0.07464% |
60
+ | eurlex | `parlament` | 780,286 | 0.03281% |
61
+ | eurlex | `rozporządzenie` | 1,202,658 | 0.05057% |
62
+ | eurlex | `w pobliżu` | 6,088 | 0.00026% |
63
+ | eurlex | `mieszkańców` | 9,441 | 0.00040% |
64
+ | eurlex | `Dz.U.` | 915,707 | 0.03851% |
65
+ | parliamentary | `w roku` | 198,192 | 0.01203% |
66
+ | parliamentary | `klasyfikacji` | 12,637 | 0.00077% |
67
+ | parliamentary | `ustawa` | 459,179 | 0.02788% |
68
+ | parliamentary | `artykuł` | 182,038 | 0.01105% |
69
+ | parliamentary | `parlament` | 309,695 | 0.01881% |
70
+ | parliamentary | `rozporządzenie` | 113,547 | 0.00689% |
71
+ | parliamentary | `w pobliżu` | 4,964 | 0.00030% |
72
+ | parliamentary | `mieszkańców` | 78,048 | 0.00474% |
73
+ | parliamentary | `Dz.U.` | 23,809 | 0.00145% |
74
+ | wikibooks | `w roku` | 319 | 0.00205% |
75
+ | wikibooks | `klasyfikacji` | 37 | 0.00024% |
76
+ | wikibooks | `ustawa` | 165 | 0.00106% |
77
+ | wikibooks | `artykuł` | 732 | 0.00470% |
78
+ | wikibooks | `parlament` | 283 | 0.00182% |
79
+ | wikibooks | `rozporządzenie` | 131 | 0.00084% |
80
+ | wikibooks | `w pobliżu` | 125 | 0.00080% |
81
+ | wikibooks | `mieszkańców` | 204 | 0.00131% |
82
+ | wikibooks | `Dz.U.` | 16 | 0.00010% |
83
+ | wikinews | `w roku` | 449 | 0.00370% |
84
+ | wikinews | `klasyfikacji` | 639 | 0.00526% |
85
+ | wikinews | `ustawa` | 407 | 0.00335% |
86
+ | wikinews | `artykuł` | 2,474 | 0.02038% |
87
+ | wikinews | `parlament` | 2,530 | 0.02084% |
88
+ | wikinews | `rozporządzenie` | 168 | 0.00138% |
89
+ | wikinews | `w pobliżu` | 455 | 0.00375% |
90
+ | wikinews | `mieszkańców` | 1,014 | 0.00835% |
91
+ | wikinews | `Dz.U.` | 27 | 0.00022% |
92
+ | wikipedia | `w roku` | 143,023 | 0.02022% |
93
+ | wikipedia | `klasyfikacji` | 46,043 | 0.00651% |
94
+ | wikipedia | `ustawa` | 9,536 | 0.00135% |
95
+ | wikipedia | `artykuł` | 28,165 | 0.00398% |
96
+ | wikipedia | `parlament` | 57,863 | 0.00818% |
97
+ | wikipedia | `rozporządzenie` | 5,637 | 0.00080% |
98
+ | wikipedia | `w pobliżu` | 41,915 | 0.00593% |
99
+ | wikipedia | `mieszkańców` | 122,766 | 0.01736% |
100
+ | wikipedia | `Dz.U.` | 232 | 0.00003% |
101
+ | wikiquote | `w roku` | 608 | 0.00191% |
102
+ | wikiquote | `klasyfikacji` | 15 | 0.00005% |
103
+ | wikiquote | `ustawa` | 357 | 0.00112% |
104
+ | wikiquote | `artykuł` | 606 | 0.00190% |
105
+ | wikiquote | `parlament` | 1,271 | 0.00398% |
106
+ | wikiquote | `rozporządzenie` | 27 | 0.00008% |
107
+ | wikiquote | `w pobliżu` | 207 | 0.00065% |
108
+ | wikiquote | `mieszkańców` | 527 | 0.00165% |
109
+ | wikiquote | `Dz.U.` | 6 | 0.00002% |
110
+ | wikisource | `w roku` | 16,571 | 0.00207% |
111
+ | wikisource | `klasyfikacji` | 166 | 0.00002% |
112
+ | wikisource | `ustawa` | 8,156 | 0.00102% |
113
+ | wikisource | `artykuł` | 16,230 | 0.00202% |
114
+ | wikisource | `parlament` | 6,119 | 0.00076% |
115
+ | wikisource | `rozporządzenie` | 1,651 | 0.00021% |
116
+ | wikisource | `w pobliżu` | 13,921 | 0.00174% |
117
+ | wikisource | `mieszkańców` | 14,335 | 0.00179% |
118
+ | wikisource | `Dz.U.` | 5 | 0.00000% |
119
+ | wikivoyage | `w roku` | 609 | 0.00356% |
120
+ | wikivoyage | `klasyfikacji` | 21 | 0.00012% |
121
+ | wikivoyage | `ustawa` | 46 | 0.00027% |
122
+ | wikivoyage | `artykuł` | 657 | 0.00384% |
123
+ | wikivoyage | `parlament` | 249 | 0.00145% |
124
+ | wikivoyage | `rozporządzenie` | 34 | 0.00020% |
125
+ | wikivoyage | `w pobliżu` | 6,480 | 0.03783% |
126
+ | wikivoyage | `mieszkańców` | 4,100 | 0.02394% |
127
+ | wikivoyage | `Dz.U.` | 1 | 0.00001% |
128
+ | wolne_lektury | `w roku` | 1,898 | 0.00184% |
129
+ | wolne_lektury | `klasyfikacji` | 80 | 0.00008% |
130
+ | wolne_lektury | `ustawa` | 1,060 | 0.00103% |
131
+ | wolne_lektury | `artykuł` | 1,824 | 0.00177% |
132
+ | wolne_lektury | `parlament` | 672 | 0.00065% |
133
+ | wolne_lektury | `rozporządzenie` | 128 | 0.00012% |
134
+ | wolne_lektury | `w pobliżu` | 2,073 | 0.00201% |
135
+ | wolne_lektury | `mieszkańców` | 1,658 | 0.00161% |
136
+ | wolne_lektury | `Dz.U.` | 5 | 0.00000% |
artifacts/pattern_frequency_klasyfikacji.png ADDED

Git LFS Details

  • SHA256: d7a05145cf47112cd3cb6363b2157de0f649b51675163c24d2e81e262a5b6dc8
  • Pointer size: 130 Bytes
  • Size of remote file: 51.3 kB
artifacts/pattern_frequency_mieszkańców.png ADDED

Git LFS Details

  • SHA256: 3b6cf08c1d5b916ee147e436442c9e267c39e537b95d1dc5834961dd2e18acd6
  • Pointer size: 130 Bytes
  • Size of remote file: 49.9 kB
artifacts/pattern_frequency_overall.png ADDED

Git LFS Details

  • SHA256: cbc7e134d6fba9b0af85db0919a7c80ddf69813e8255695164cd5f5b79d5f46e
  • Pointer size: 130 Bytes
  • Size of remote file: 42.2 kB
artifacts/pattern_frequency_parlament.png ADDED

Git LFS Details

  • SHA256: 4aace1d3daacca7604fb1d989e47124746807c6d476b8fe5cd6899a3c7a25bea
  • Pointer size: 130 Bytes
  • Size of remote file: 50.8 kB
artifacts/pattern_frequency_rozporządzenie.png ADDED

Git LFS Details

  • SHA256: dac48564eb391ee615fae000cee2d8aea0e5b5a81abc6e84db871251ce855292
  • Pointer size: 130 Bytes
  • Size of remote file: 48.8 kB
artifacts/pattern_frequency_ustawa.png ADDED

Git LFS Details

  • SHA256: 994033b901cf698a55d4cb6a5733323cfe5654c6ffb83e56434e453f98274a88
  • Pointer size: 130 Bytes
  • Size of remote file: 49.5 kB
artifacts/pattern_frequency_w_pobliżu.png ADDED

Git LFS Details

  • SHA256: 3e5d346fcb45bffec8e8d11b362275f20222bef4708d10b62de92cdfadb9c12d
  • Pointer size: 130 Bytes
  • Size of remote file: 52 kB
artifacts/pattern_frequency_w_roku.png ADDED

Git LFS Details

  • SHA256: 04fa0ed3684f8d57fa7356c013fe3ebe75a56ddfae5c029d05c3e6385ccf2ad3
  • Pointer size: 130 Bytes
  • Size of remote file: 54.2 kB
src/pattern_frequency_report.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Generate pattern-frequency report as percentage of source token counts.
3
+
4
+ Outputs:
5
+ - summary for whole corpus (counts + share of total tokens)
6
+ - per-source counts + share within source
7
+ - optional markdown snippet and optional bar chart
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import json
14
+ from pathlib import Path
15
+
16
+ import matplotlib.pyplot as plt
17
+ import pyarrow.compute as pc
18
+ import pyarrow.parquet as pq
19
+
20
+ PATTERNS = [
21
+ ("w roku", "w roku"),
22
+ ("klasyfikacji", "klasyfikacji"),
23
+ ("ustawa", "ustawa"),
24
+ ("artykuł", "artykuł"),
25
+ ("parlament", "parlament"),
26
+ ("rozporządzenie", "rozporządzenie"),
27
+ ("w pobliżu", "w pobliżu"),
28
+ ("mieszkańców", "mieszkańców"),
29
+ ("Dz.U.", "dz\\.u\\."),
30
+ ]
31
+
32
+
33
+ def load_tokens_by_source(root: Path) -> dict[str, int]:
34
+ by_source = {}
35
+ for stats_file in sorted((root / "data").glob("*/*.stats.json")):
36
+ src = stats_file.parent.name
37
+ payload = json.loads(stats_file.read_text(encoding="utf-8"))
38
+ by_source[src] = int(payload["tokens"])
39
+ return by_source
40
+
41
+
42
+ def count_patterns_for_source(parquet_path: Path) -> dict[str, int]:
43
+ counts = {name: 0 for name, _ in PATTERNS}
44
+ pf = pq.ParquetFile(parquet_path)
45
+
46
+ for rg in range(pf.num_row_groups):
47
+ table = pf.read_row_group(rg, columns=["text"])
48
+ text = table["text"]
49
+ text = pc.utf8_lower(text)
50
+ text = pc.replace_substring_regex(text, pattern="\\r?\\n", replacement=" ")
51
+
52
+ for label, pattern in PATTERNS:
53
+ if label == "Dz.U.":
54
+ cnt = pc.count_substring_regex(text, pattern)
55
+ else:
56
+ cnt = pc.count_substring(text, pattern)
57
+ counts[label] += int(pc.sum(cnt).as_py())
58
+
59
+ return counts
60
+
61
+
62
+ def compute_counts(data_root: Path) -> tuple[dict[str, int], dict[str, dict[str, int]]]:
63
+ tokens = load_tokens_by_source(data_root)
64
+ source_counts = {}
65
+ total_counts = {label: 0 for label, _ in PATTERNS}
66
+
67
+ for parquet_path in sorted((data_root / "data").glob("*/*.parquet")):
68
+ source = parquet_path.parent.name
69
+ counts = count_patterns_for_source(parquet_path)
70
+ source_counts[source] = counts
71
+ for label, cnt in counts.items():
72
+ total_counts[label] += cnt
73
+
74
+ return total_counts, source_counts, tokens
75
+
76
+
77
+ def write_markdown(total_counts, source_counts, tokens, out_md: Path) -> None:
78
+ total_tokens = sum(tokens.values())
79
+ lines = []
80
+ lines.append("## Pattern frequency on corpus\n")
81
+ lines.append(f"- total tokens (tiktoken proxy): `{total_tokens:,}`\n")
82
+ lines.append("| pattern | count | share of all tokens |")
83
+ lines.append("|---|---:|---:|")
84
+ for label, _ in PATTERNS:
85
+ c = total_counts[label]
86
+ lines.append(f"| `{label}` | {c:,} | {c/total_tokens*100:.4f}% |")
87
+ lines.append("")
88
+ lines.append("| source | pattern | count | per-token share |")
89
+ lines.append("|---|---|---:|---:|")
90
+ for source in sorted(source_counts):
91
+ src_tokens = tokens[source]
92
+ for label, _ in PATTERNS:
93
+ c = source_counts[source][label]
94
+ lines.append(f"| {source} | `{label}` | {c:,} | {c/src_tokens*100:.5f}% |")
95
+ out_md.write_text("\n".join(lines) + "\n", encoding="utf-8")
96
+
97
+
98
+ def write_hf_snippet(total_counts, source_counts, tokens, total_tokens: int, out_md: Path) -> None:
99
+ patterns = [label for label, _ in PATTERNS]
100
+ lines = []
101
+ lines.append("## Phrase frequency in corpus (token-normalized)")
102
+ lines.append("")
103
+ lines.append(f"- Total token count (tiktoken proxy): **{total_tokens:,}**")
104
+ lines.append("")
105
+ lines.append("| Pattern | Count | Share of all tokens |")
106
+ lines.append("|---|---:|---:|")
107
+ for label in patterns:
108
+ c = total_counts[label]
109
+ lines.append(f"| `{label}` | {c:,} | {c / total_tokens * 100:.4f}% |")
110
+ lines.append("")
111
+ lines.append("### Per-source shares")
112
+ lines.append("")
113
+ lines.append("| source | pattern | count | share of source tokens |")
114
+ lines.append("|---|---|---:|---:|")
115
+ ordered_sources = sorted(source_counts)
116
+ for source in ordered_sources:
117
+ src_tok = tokens[source]
118
+ for label in patterns:
119
+ c = source_counts[source][label]
120
+ lines.append(f"| `{source}` | `{label}` | {c:,} | {c / src_tok * 100:.5f}% |")
121
+
122
+ lines.append("")
123
+ lines.append("![Overall pattern counts](artifacts/pattern_frequency_overall.png)")
124
+ lines.append("")
125
+ lines.append("![w roku by source](artifacts/pattern_frequency_w_roku.png)")
126
+ lines.append("![klasyfikacji by source](artifacts/pattern_frequency_klasyfikacji.png)")
127
+ lines.append("![ustawa by source](artifacts/pattern_frequency_ustawa.png)")
128
+ lines.append("![artykuł by source](artifacts/pattern_frequency_artykul.png)")
129
+ lines.append("![parlament by source](artifacts/pattern_frequency_parlament.png)")
130
+ lines.append("![rozporządzenie by source](artifacts/pattern_frequency_rozporządzenie.png)")
131
+ lines.append("![w pobliżu by source](artifacts/pattern_frequency_w_pobliżu.png)")
132
+ lines.append("![mieszkańców by source](artifacts/pattern_frequency_mieszkańców.png)")
133
+ lines.append("![Dz.U. by source](artifacts/pattern_frequency_dzu.png)")
134
+
135
+ out_md.write_text("\n".join(lines) + "\n", encoding="utf-8")
136
+
137
+
138
+ def plot(total_counts, source_counts, tokens, out_png: Path) -> None:
139
+ out_png.parent.mkdir(parents=True, exist_ok=True)
140
+ patterns = [label for label, _ in PATTERNS]
141
+ totals = [total_counts[p] for p in patterns]
142
+
143
+ # overall share chart
144
+ plt.figure(figsize=(10, 4))
145
+ plt.bar(patterns, totals, color="#2b8cbe")
146
+ plt.title("Pattern count in full corpus")
147
+ plt.ylabel("count")
148
+ plt.xlabel("pattern")
149
+ plt.xticks(rotation=25, ha="right")
150
+ plt.tight_layout()
151
+ total_png = out_png.with_name(out_png.stem + "_overall" + out_png.suffix)
152
+ plt.savefig(total_png, dpi=140)
153
+ plt.close()
154
+
155
+ # per-source percentage heatmap-like bars
156
+ ordered_sources = sorted(source_counts)
157
+ for pattern in patterns:
158
+ vals = [source_counts[src][pattern] / tokens[src] * 100 for src in ordered_sources]
159
+ plt.figure(figsize=(10, 4))
160
+ plt.bar(ordered_sources, vals)
161
+ plt.title(f"{pattern} share per source (% of source tokens)")
162
+ plt.ylabel("% of tokens")
163
+ plt.xticks(rotation=30, ha="right")
164
+ plt.tight_layout()
165
+ safe = pattern.replace(" ", "_").replace("ł", "l").replace(".", "").lower()
166
+ plt.savefig(out_png.parent / f"{out_png.stem}_{safe}.png", dpi=140)
167
+ plt.close()
168
+
169
+
170
+ def parse_args():
171
+ ap = argparse.ArgumentParser()
172
+ ap.add_argument("--data-root", type=Path, default=Path("."), help="repo root")
173
+ ap.add_argument("--out-md", type=Path, default=Path("pattern_frequency_report.md"))
174
+ ap.add_argument("--out-png", type=Path, default=Path("artifacts/pattern_frequency.png"))
175
+ ap.add_argument(
176
+ "--out-hf",
177
+ type=Path,
178
+ default=Path("artifacts/pattern_frequency_hf_snippet.md"),
179
+ help="HF model card snippet to paste into README.md on Hugging Face",
180
+ )
181
+ return ap.parse_args()
182
+
183
+
184
+ def main():
185
+ args = parse_args()
186
+ total_counts, source_counts, tokens = compute_counts(args.data_root)
187
+ total_tokens = sum(tokens.values())
188
+ args.out_md.parent.mkdir(parents=True, exist_ok=True)
189
+ write_markdown(total_counts, source_counts, tokens, args.out_md)
190
+ write_hf_snippet(total_counts, source_counts, tokens, total_tokens, args.out_hf)
191
+ plot(total_counts, source_counts, tokens, args.out_png)
192
+
193
+ print(f"wrote: {args.out_md}")
194
+ print(f"wrote: {args.out_hf}")
195
+ print(f"wrote: {args.out_png.with_name(args.out_png.stem + '_overall' + args.out_png.suffix)}")
196
+ for label, _ in PATTERNS:
197
+ safe = label.replace(' ', '_').replace('ł', 'l').replace('.', '').lower()
198
+ print(f"wrote: {args.out_png.parent / f'{args.out_png.stem}_{safe}.png'}")
199
+
200
+
201
+ if __name__ == "__main__":
202
+ main()