-
Notifications
You must be signed in to change notification settings - Fork 9
/
kaikki-breakdown.py
152 lines (125 loc) · 5.3 KB
/
kaikki-breakdown.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import json
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import sys
import os
recalculate = False
if len(sys.argv) > 1:
first_arg = sys.argv[1]
if first_arg == "R":
recalculate = True
for file in ["heatmap_data.json", "source_languages.json", "target_languages.json"]:
if not os.path.isfile(file):
recalculate = True
if recalculate:
counter = {}
for target_iso in ['de', 'es', 'ru', 'zh', 'fr']:
print(f"Processing {target_iso}...")
counter[target_iso] = {}
with open(f'../data/kaikki/{target_iso}-extract.json', "r", encoding="utf-8") as f:
line_count = 0
print_interval = 1000
for line in f:
line_count += 1
if line_count % print_interval == 0:
print(f"Processed {line_count} lines...", end='\r')
try:
obj = json.loads(line.strip())
except json.JSONDecodeError:
print(f"Error decoding JSON in line {line_count}. Skipping...")
continue
if "lang_code" in obj:
counter[target_iso][obj["lang_code"]] = counter[target_iso].get(obj["lang_code"], 0) + 1
else:
if "redirect" in obj:
counter[target_iso]["redirect"] = counter[target_iso].get("redirect", 0) + 1
else:
counter[target_iso]["error"] = counter[target_iso].get("error", 0) + 1
print(json.dumps(counter[target_iso], indent=4))
print(f"Processing en...")
counter["en"] = {}
for file in os.listdir("../data/kaikki"):
if file.startswith("kaikki"):
print(f"Processing {file}...")
with open(f"../data/kaikki/{file}", "r", encoding="utf-8") as f:
line_count = 0
print_interval = 1000
for line in f:
line_count += 1
if line_count % print_interval == 0:
print(f"Processed {line_count} lines...", end='\r')
try:
obj = json.loads(line.strip())
except json.JSONDecodeError:
print(f"Error decoding JSON in line {line_count}. Skipping...")
continue
if "lang_code" in obj:
counter["en"][obj["lang_code"]] = counter["en"].get(obj["lang_code"], 0) + 1
else:
if "redirect" in obj:
counter["en"]["redirect"] = counter["en"].get("redirect", 0) + 1
else:
counter["en"]["error"] = counter["en"].get("error", 0) + 1
for target_iso in counter:
for target_iso2 in counter:
for source_iso in counter[target_iso]:
if source_iso not in counter[target_iso2]:
counter[target_iso2][source_iso] = 0
for target_iso in counter:
if "error" in counter[target_iso]:
del counter[target_iso]["error"]
if "redirect" in counter[target_iso]:
del counter[target_iso]["redirect"]
counter[target_iso] = {k: v for k, v in sorted(counter[target_iso].items(), key=lambda item: item[0])}
heatmap_data = [[counter[key1].get(key2, 0) for key2 in counter[key1]] for key1 in counter]
source_languages = list(counter.keys())
target_languages = list(counter["de"].keys())
with open('heatmap_data.json', 'w') as f:
json.dump(heatmap_data, f)
with open('source_languages.json', 'w') as f:
json.dump(source_languages, f)
with open('target_languages.json', 'w') as f:
json.dump(target_languages, f)
with open('heatmap_data.json', 'r') as f:
heatmap_data = json.load(f)
with open('source_languages.json', 'r') as f:
source_languages = json.load(f)
with open('target_languages.json', 'r') as f:
target_languages = json.load(f)
annotations = []
for row in heatmap_data:
new_row = []
for cell in row:
if cell < 1000:
new_row.append(str(cell))
else:
rounded_value = int(round(cell / 1000, 0))
new_row.append(f"{rounded_value}k")
annotations.append(new_row)
size = 25
df = pd.DataFrame(heatmap_data, index=source_languages, columns=target_languages)
df = df.loc[df.sum(axis=1).sort_values(ascending=False).head(size).index]
df = df[df.sum().sort_values(ascending=False).head(size).index]
annotations = pd.DataFrame(annotations, index=source_languages, columns=target_languages)
annotations = annotations.loc[df.sum(axis=1).sort_values(ascending=False).head(size).index]
annotations = annotations[df.sum().sort_values(ascending=False).head(size).index]
# Set a larger figure size
plt.figure(figsize=(15, 4))
# Create a heatmap using seaborn
sns.heatmap(
df,
annot=annotations,
cmap="YlGnBu",
annot_kws={"size": 7},
fmt="s",
vmax=150000,
cbar_kws={'label': 'number of words'},
linewidths=.5
)
# Add labels and title
plt.xlabel("Source Language (headwords in this language)", fontsize=8)
plt.ylabel("Target Language (glosses in this language)", fontsize=8)
plt.title("kaikki-to-yomitan", fontsize=12)
# Save the plot with a higher resolution
plt.savefig("heatmap.png", dpi=300)