-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
274 lines (230 loc) · 10.5 KB
/
generate.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import sys
import re
import os
import traceback
import pyarrow.csv as csv
from jinja2 import Environment, FileSystemLoader, select_autoescape
def normalize_filename(fname):
'''turn a language name into a pleasant-looking filename'''
fname = fname.lower().replace(' ', '_').replace('(', '_').replace(')', '_')
fname = fname.replace("'", '_').replace('"', '_')
fname = re.sub(r'_+', '_', fname)
return fname
def skip_comments(row):
if row.text.startswith('#'):
return 'skip'
def read_tsv_pa(fname, column_names, usecols=None):
parse_options = csv.ParseOptions(
delimiter='\t',
invalid_row_handler=skip_comments,
)
convert_options = csv.ConvertOptions(
include_columns=usecols,
)
read_options = csv.ReadOptions(
column_names=column_names,
skip_rows=1,
)
table = csv.read_csv(
fname,
parse_options=parse_options,
convert_options=convert_options,
read_options=read_options,
)
return table
env = Environment(
loader=FileSystemLoader('./templates'),
autoescape=select_autoescape(['html'])
)
language_type_map = { # note the lower case
#'A': 'ancient', # seems to have been deprecated a while ago
'C': 'constructed',
'E': 'extinct',
'H': 'historical',
'L': 'living',
'S': 'special',
}
extras = {
# macrolanguages: if these are > 50mm speakers, label them big
'ara': {'Id': 'ara', 'Names': [], 'noedit': True, 'big': True, 'comment': 'See Modern Standard Arabic, and many others'},
'fas': {'Id': 'fas', 'Names': ['Farsi'], 'noedit': True, 'big': True, 'comment': 'See Iranian Persian, Dari, and Tajik'},
'msa': {'Id': 'msa', 'Names': [], 'noedit': True, 'big': True, 'comment': 'See Indonesian, and 34 other languages with Malay in their name'},
'zho': {'Id': 'zho', 'Names': [], 'noedit': True, 'big': True, 'comment': 'See Mandarin Chinese and the many other Chinese languages'},
'lah': {'Id': 'lah', 'Names': [], 'noedit': True, 'big': True, 'comment': 'Macrolanguage. See Western Panjabi and [the list here](https://en.wikipedia.org/wiki/Lahnda)'},
# macro not big
'pus': {'Id': 'pus', 'Names': ['Pashto'], 'noedit': True, 'comment': 'See Northern, Southern, and Central Pashto'}, # 40mm speakers
# not macro, still no edit
'eng': {'Id': '', 'Names': [], 'noedit': True, 'comment': ''},
'rus': {'Id': '', 'Names': [], 'noedit': True, 'comment': ''},
'deu': {'Id': '', 'Names': [], 'noedit': True, 'comment': ''},
'spa': {'Id': 'spa', 'Names': [], 'noedit': True, 'comment': 'We may eventually make a geographic breakdown'},
'fra': {'Id': '', 'Names': [], 'noedit': True, 'comment': ''},
# other
'pnb': {'Id': 'pnb', 'Names': ['Lahndi'], 'comment': 'See also [the list here](https://en.wikipedia.org/wiki/Lahnda)'},
'por': {'Id': 'por', 'Names': [], 'comment': 'Includes Brazil, we may break that out later'},
#'': {'Id': '', 'Names': [], 'noedit': True, 'comment': ''},
}
basedir = '../web-languages'
def add_names(entry, names):
Names = [entry['Ref_Name']] + entry.get('Extra_Names', [])
for name in names:
if not Names.count(name):
if 'Extra_Names' not in entry:
entry['Extra_Names'] = []
entry['Extra_Names'].append(name)
def main():
column_names = ['Id', 'Part2b', 'Part2t', 'Part1', 'Scope', 'Language_Type', 'Ref_Name', 'Comment']
usecols = None # list of str
table = read_tsv_pa('iso-639-3_Code_Tables_20240415/iso-639-3.tab', column_names=column_names, usecols=usecols)
print('ISO-639-3 rows', table.num_rows)
types = set(table['Language_Type'].to_pylist())
column_names = ['Name', 'Code', 'Family', 'Script', 'doc_count', 'image_count', 'token_count']
usecols = ['Name', 'Code', 'Script', 'doc_count']
mOSCAR_table = read_tsv_pa('mOSCAR_table.tsv', column_names=column_names, usecols=usecols)
print('mOSCAR rows', mOSCAR_table.num_rows)
column_names = ['Id', 'Name', 'speakers']
usecols = None
wikipedia_size_table = read_tsv_pa('wikipedia_size.tsv', column_names=column_names, usecols=usecols)
print('wikipedia_size rows', wikipedia_size_table.num_rows)
column_names = ['wiki_code', 'iso_code', 'iso_name', 'wiki_name', 'wiki_local_name']
usecols = None
wikipedia_languages_table = read_tsv_pa('wikipedia_languages_all.tsv', column_names=column_names, usecols=usecols)
print('wikipedia_language rows', wikipedia_languages_table.num_rows)
# these are small so let's do it in python
table_dicts = table.to_pylist() # list of dictionaries
mOSCAR_dicts = mOSCAR_table.to_pylist()
wikipedia_size_dicts = wikipedia_size_table.to_pylist()
wikipedia_languages_dicts = wikipedia_languages_table.to_pylist()
assert len(table_dicts) == len(set(x['Id'] for x in table_dicts)), 'check that ISO 639 Ids are unique'
assert len(table_dicts) == len(set(x['Ref_Name'] for x in table_dicts)), 'check that ISO 639 Ref_Names are unique'
# split Code into a useful Id and ISO standard script name
# scrpt https://en.wikipedia.org/wiki/ISO_15924
for d in mOSCAR_dicts:
d['Id'], d['scrpt'] = d['Code'].split('_')
# start the main table
ids = {}
for d in table_dicts:
Id = d['Id']
ids[Id] = d
# add in info from mOSCAR
for d in mOSCAR_dicts:
if d['Id'] == 'ajp':
# special fixup (2023): ajp (South) -> apc (formerly North) and the ref_name is Levantine Arabic
# the ref_name for ajp is alreay set to Levantine Arabic at this point
# by NOT changing the 'Name', both North and South will be Extra_Names
d['Id'] = 'apc'
Id = d['Id']
if Id not in ids:
print(f'warning: mOSCAR Id {Id} not in table, skipping')
continue
entry = ids[Id]
for name in ('Extra_Names', 'Scripts', 'scrpts'):
if name not in entry:
entry[name] = []
if entry['Ref_Name'] != d['Name']:
entry['Extra_Names'].append(d['Name'])
for name in ('Script', 'scrpt'):
entry[name+'s'].append(d[name])
if len(entry[name+'s']) > 1:
# zho really is 2; apc is 2 Arabics; eventually belarusian will have 2
entry[name+'s'] = list(set(entry[name+'s']))
if 'mOSCAR_doc_count' not in entry:
entry['mOSCAR_doc_count'] = 0
entry['mOSCAR_doc_count'] += d['doc_count']
# wikipedia_size_table Id Name speakers -- these are all big
for d in wikipedia_size_dicts:
Id = d['Id']
if Id not in ids:
print(f'warning: wikipedia_size Id {Id} not in table, skipping')
continue
entry = ids[Id]
entry['big'] = True
if d['Name']:
add_names(entry, [d['Name']])
# wikipedia_languages wiki_code and maybe names
# 'wiki_code', 'iso_code', 'iso_name', 'wiki_name', 'wiki_local_name'
for d in wikipedia_languages_dicts:
Id = d['iso_code']
if Id not in ids:
print(f'warning: wikipedia_languages Id {Id} not in table, skipping')
continue
entry = ids[Id]
entry['wiki_code'] = d['wiki_code'] # XXX only allows one. Belarusian has 2.
names = [x for x in [d['wiki_name'], d['wiki_local_name']] if x]
if names:
add_names(entry, names)
# add in extras
for Id, d in extras.items():
if Id not in ids:
# eventually we will use this to add new entries, but not yet (e.g. Taiwanese)
print(f'warning: extras Id {Id} not in table')
continue
entry = ids[Id]
if d['Names']:
add_names(entry, d['Names'])
for field in ('noedit', 'big', 'comment'):
if field in d:
entry[field] = d[field]
# zip the scripts
for k, v in ids.items():
if 'Scripts' in v: # means scrpts should also be present
v['script_zip'] = zip(v['Scripts'], v['scrpts'])
for type_ in types:
os.makedirs(basedir.rstrip('/') + '/' + language_type_map[type_], exist_ok=True)
for k, v in ids.items():
fname = normalize_filename(v['Ref_Name']) + '.md'
v['fname'] = fname
fname = basedir.rstrip('/') + '/' + language_type_map[v['Language_Type']] + '/' + fname
template = env.get_template('ref_name.template')
with open(fname, 'w') as f:
try:
f.write(template.render(**v)+'\n')
except Exception as e:
print('got exception {} processing {}, skipping'.format(str(e), d), file=sys.stderr)
print(traceback.format_exc())
for type_ in types:
print('type_', type_)
type_list_big = []
type_list = []
for v in ids.values():
if v['Language_Type'] == type_:
if v.get('big'):
type_list_big.append(v)
else:
type_list.append(v)
if type_list_big:
type_list_big = sorted(type_list_big, key=lambda k: k['Ref_Name'])
type_list = sorted(type_list, key=lambda k: k['Ref_Name'])
type_name = language_type_map[type_]
template = env.get_template('type.template')
fname = basedir.rstrip('/') + '/' + type_name + '/README.md'
top = False
subdir = type_name + '/'
with open(fname, 'w') as f:
try:
f.write(template.render(
type_name=type_name, top=top, subdir=subdir, type_list_big=type_list_big, type_list=type_list
)+'\n')
except Exception as e:
print('got exception {} processing {}, skipping'.format(str(e), d), file=sys.stderr)
print(traceback.format_exc())
if os.path.getsize(fname) > 500 * 1024:
raise ValueError(f'{fname} is too big for github to display')
if type_ != 'L':
continue
# top level README
fname = basedir.rstrip('/') + '/' + '/README.md'
top = True
subdir = type_name + '/'
with open(fname, 'w') as f:
try:
f.write(template.render(
type_name=type_name, top=top, subdir=subdir, type_list_big=type_list_big, type_list=type_list
)+'\n')
except Exception as e:
print('got exception {} processing {}, skipping'.format(str(e), d), file=sys.stderr)
print(traceback.format_exc())
if os.path.getsize(fname) > 500 * 1024:
raise ValueError(f'{fname} is too big for github to display')
if __name__ == '__main__':
main()