forked from machinetranslate/machinetranslate.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
303 lines (241 loc) · 7.64 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
import yaml
from os.path import exists
SCRIPTS = None
LANGUAGES = None
ENGINES = None
LANGUAGE_FAMILIES = None
### Read scripts
with open('_data/scripts.yml', 'r') as stream:
SCRIPTS = yaml.safe_load(stream)
### Read languages
with open('_data/languages.yml', 'r') as stream:
LANGUAGES = yaml.safe_load(stream)
### Read language families
with open('_data/language-families.yml', 'r') as stream:
LANGUAGE_FAMILIES = yaml.safe_load(stream)
### Read engines
with open('_data/engines.yml', 'r') as stream:
ENGINES = yaml.safe_load(stream)
### Read engine-language conversions
with open('_data/engine-language.yml', 'r') as stream:
ENGINE_LANGUAGE = yaml.safe_load(stream)
def base_language_code(locale_code):
locale_code = locale_code.replace('_', '-')
return locale_code.split('-')[0]
def normalize_locale_casing(locale_code):
return '-'.join([ part.capitalize() if len(part) == 4 else part.lower() for part in locale_code.split('-') ])
def _normalize_language_code(locale_code, engine_id):
if engine_id not in ENGINE_LANGUAGE:
return None
if locale_code not in ENGINE_LANGUAGE[engine_id]:
return None
return ENGINE_LANGUAGE[engine_id][locale_code]
def normalize_language_code(locale_code, engine_id):
locale_code = locale_code.replace('_', '-')
locale_code = normalize_locale_casing(locale_code)
return _normalize_language_code(base_language_code(locale_code), engine_id) \
or _normalize_language_code(locale_code, '*') \
or _normalize_language_code(base_language_code(locale_code), '*') \
or locale_code
def get_language_variant_name(locale_code):
parts = normalize_locale_casing(locale_code.replace('_', '-')).split('-')
names = []
if parts == 'lzh':
names.append('Literary')
for part in parts[1:]:
if part in SCRIPTS:
names.append(SCRIPTS[part])
# TODO: if part in country
print(locale_code, names)
if not names:
return None
return ' - '.join(names)
def slugify(name):
# Should work *exactly* like in Liquid!
return name.lower().replace(' ', '-')
def flatten(l):
_ = []
for item in l:
if type(item) is list:
_ += flatten(item)
else:
_.append(item)
return _
def read_content(filepth):
content = ''
if not exists(filepath):
return ''
with open(filepath, 'r') as f:
page = f.read()
i = page.find('\n---\n', 3)
i += len('\n---\n')
return page[i:].strip()
SUPPORTED_LANGUAGE_BASE_CODES = {}
for engine in ENGINES:
engine_id = engine['id']
codes = flatten(engine['languages'])
def normalize(code):
return normalize_language_code(code, engine_id)
codes = map(normalize, codes)
codes = map(base_language_code, codes)
SUPPORTED_LANGUAGE_BASE_CODES[engine_id] = list(set(codes))
### Write language families
for code in LANGUAGE_FAMILIES:
name = LANGUAGE_FAMILIES[code]
slug = slugify(name)
filepath = f'languages/{ slug }.md'
# TODO: check that it won't be overwritten by a language
content = read_content(filepath)
# "Join"
languages = []
for language in LANGUAGES:
if code in language['family']:
language_name = language['names'][0]
languages.append({
'slug': slugify(language_name),
'name': language_name
})
languages.sort(key=lambda language: language['name'])
frontmatter = {
'nav_exclude': True,
'parent': 'Language families',
'layout': 'language_family',
'title': name,
'description': f'Machine translation for the { name } language family',
'code': code,
'languages': languages
}
with open(filepath, 'w', encoding='utf8') as f:
f.write(f'''\
---
{ yaml.dump(frontmatter, sort_keys=False) }
---
{ content }
''')
### Write languages
for language in LANGUAGES:
code = language['codes'][0]
if type(language['codes']) is not list:
raise Exception(language)
name = language['names'][0]
if type(language['names']) is not list:
raise Exception(language)
family = []
for language_family_code in language['family']:
language_family_name = LANGUAGE_FAMILIES[language_family_code]
family.append({
'slug': slugify(language_family_name),
'name': language_family_name
})
# "Join"
supported_engines = []
for engine in ENGINES:
codes = SUPPORTED_LANGUAGE_BASE_CODES[engine['id']]
if code in codes:
supported_engines.append({
'id': engine['id'],
'name': engine['name'],
'supported_language_count': len(codes)
})
supported_engines.sort(key=lambda engine: engine['supported_language_count'])
frontmatter = {
'nav_order': 1000 - len(supported_engines),
'parent': 'Languages',
'layout': 'language',
'title': name,
'description': f'Machine translation for { name }',
'code': code,
'family': family,
'supported_engines': supported_engines
}
slug = slugify(name)
filepath = f'languages/{ slug }.md'
content = read_content(filepath)
with open(filepath, 'w', encoding='utf8') as f:
f.write(f'''\
---
{ yaml.dump(frontmatter, sort_keys=False) }
---
{ content }
''')
UNLISTED_LANGUAGES = {}
### Generate engines
for engine in ENGINES:
name = engine['name']
if type(name) is not str:
raise Exception(name)
engine_id = engine['id']
if type(engine_id) is not str:
raise Exception(engine_id)
languages = engine['languages']
if type(languages) is not list:
raise Exception(languages)
urls = engine['urls']
self_serve = engine.get('self-serve', True)
customization = []
if engine.get('adaptive', False):
customization.append('Adaptive')
if engine.get('glossary', False):
customization.append('Glossary')
if engine.get('formality', False):
customization.append('Formality')
# "Join"
# TODO: use language/engine mapping
supported_language_codes = list(set(flatten(languages)))
supported_language_codes.sort()
# TODO: language *pairs*
supported_languages = []
for code in supported_language_codes:
language_name = None
language_slug = None
for language in LANGUAGES:
normalized_code = normalize_language_code(code, engine_id)
base_code = base_language_code(normalized_code)
if base_code in language['codes']:
language_name = language['names'][0]
language_slug = slugify(language_name)
break
variant_name = get_language_variant_name(code)
supported_languages.append({
'slug': language_slug,
'code': code,
'normalized_code': normalized_code,
'base_code': base_code,
'name': language_name,
'variant_name': variant_name
})
if not language_slug:
if code in UNLISTED_LANGUAGES:
UNLISTED_LANGUAGES[code] += 1
else:
UNLISTED_LANGUAGES[code] = 1
frontmatter = {
'layout': 'engine',
'title': name,
'description': f'The { name } machine translation API',
'id': engine_id,
'parent': 'Engines',
'urls': urls,
'self_serve': self_serve,
'customization': customization,
'supported_languages': supported_languages,
'nav_order': 1000 - len(supported_languages)
}
content = read_content(filepath)
filepath = f'engines/{ engine_id }.md'
with open(filepath, 'w', encoding='utf8') as f:
f.write(f'''\
---
{ yaml.dump(frontmatter, sort_keys=False) }
---
{ content }
''')
print('Codes to add to languages.md')
for code, count in sorted(UNLISTED_LANGUAGES.items(), key=lambda x: x[1] * 10 - len(x[0]), reverse=True):
text = code + ': ' + str(count)
if count > 1 or len(code) == 2:
text = '**' + text + '**'
base_code = code.split('-')[0].lower()
link = f'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes#{ base_code }' if len(base_code) == 2 else f'https://en.wikipedia.org/wiki/ISO_639:{ base_code }'
print(f'[{ text }]({ link })')