-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_config.py
302 lines (252 loc) · 13.1 KB
/
check_config.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
#!/usr/bin/env python3
# parses the config, check if the tools are present
from tomlkit import parse, dumps
import os
import sys
import re
import xml.etree.ElementTree as ET
from itertools import dropwhile
# urls of the required tools and data
corpora_url = "https://wiki.apertium.org/wiki/Corpora"
# lex_tools_url = "https://wiki.apertium.org/wiki/Install_Apertium_core_by_compiling"
fast_align_url = "https://github.com/clab/fast_align"
langs_url = "https://wiki.apertium.org/wiki/List_of_language_pairs"
apertium_url = "https://wiki.apertium.org/wiki/Installation"
yasmet_url = "https://wiki.apertium.org/wiki/Using_weights_for_ambiguous_rules"
irstlm_url = "https://wiki.apertium.org/wiki/IRSTLM"
def irstlm_path():
"""Fallback to default Debian installation path if not in environ"""
if 'IRSTLM' in os.environ:
return os.environ['IRSTLM']
else:
return '/usr/lib/irstlm'
def get_modes(lang_data):
modesfile = os.path.join(lang_data, 'modes.xml')
if not os.path.isfile(modesfile):
print(f"'{modesfile}' doesn't exist, check that LANG_DATA points to an apertium language pair checkout.")
return None
return ET.parse(modesfile)
def get_autobil(modes, lang_data, pair):
found = [n for x in modes.findall(f'.//mode[@name="{pair}"]//file')
for n in [x.attrib['name']]
if re.search('autobil[.]bin$', n)]
if found:
return os.path.join(lang_data, found[0])
else:
print(f"Couldn't find bidix bin in mode with name='{pair}' in modes.xml of '{lang_data}' (LANG_DATA), "
+ f"provide a valid directory or to install, follow {langs_url}")
def get_transferfiles(modes, lang_data, pair):
found = [n for x in modes.findall(f'.//mode[@name="{pair}"]//file')
for n in [x.attrib['name']]
if re.search('[.](t[0-9]x([.]bin)?|autop?gen[.]bin)$', n)]
if found:
return [os.path.join(lang_data, f) for f in found]
else:
print(f"Couldn't find transfer files in mode with name='{pair}' in modes.xml of '{lang_data}' (LANG_DATA), "
+ f"provide a valid directory or to install, follow {langs_url}")
def get_mode_after_biltrans(modes, lang_data, pair):
"""Chop the pipeline on biltrans/lexsel, return what's after those steps.
The return value is a list of pairs of lists, one for the cmd + opts,
one for the file arguments, e.g. (["lt-proc", "-b"], ["nob-nno.autobil.bin"])
"""
def is_biltrans(program):
return any(re.search('auto(bil|lex)[.]bin$', f.attrib['name'])
for f in program.findall('./file'))
pipeline = modes.findall(f'.//mode[@name="{pair}"]/pipeline/program')
after_biltrans = dropwhile(is_biltrans, dropwhile(lambda p: not is_biltrans(p), pipeline))
def to_cmd(program):
cmd_opts = program.attrib['name'].replace('$1', '-g').replace('$2', '-z').split()
file_args = [f.attrib['name'] for f in program.findall('./file')]
return (cmd_opts, file_args)
return [to_cmd(p) for p in after_biltrans]
def check_config(config_filename):
misconfigured = False
lex_tools_paths = ['/opt/local/share/apertium-lex-tools',
'/usr/local/share/apertium-lex-tools', '/usr/share/apertium-lex-tools']
with open(config_filename) as config_file:
config_toml = config_file.read()
config = parse(config_toml)
# gives error if not parsed well
assert config_toml == dumps(config)
# changing the paths to absolute
for key in ['CORPUS_SL', 'CORPUS_TL', 'LANG_DATA']:
if not os.path.isabs(config[key]):
config[key] = os.path.join(os.path.abspath('.'), config[key])
if not os.path.isfile(config['CORPUS_SL']):
print(
f"'{config['CORPUS_SL']}'(CORPUS_SL) is not a file, provide a valid file or \nto download, look {corpora_url}\n")
misconfigured = True
if 'TL_MODEL' not in config:
if not os.path.isfile(config['CORPUS_TL']):
print(
f"'{config['CORPUS_TL']}'(CORPUS_TL) is not a file, provide a valid file or \nto download, look {corpora_url}\n")
misconfigured = True
modes = None
if not os.path.isdir(config['LANG_DATA']):
print(
f"'{config['LANG_DATA']}'(LANG_DATA) is not a directory, provide a valid directory or \nto install, follow {langs_url}\n")
misconfigured = True
else:
modes = get_modes(config['LANG_DATA'])
if not modes:
misconfigured = True
else:
sl_tl_autobil = get_autobil(modes, config['LANG_DATA'], config['PAIR'])
if not sl_tl_autobil:
misconfigured = True
if sl_tl_autobil and not os.path.exists(sl_tl_autobil):
print(f"'{sl_tl_autobil}' is not in '{config['LANG_DATA']}' (LANG_DATA), "
+ f"provide a valid directory or to install, follow {langs_url}")
misconfigured = True
apertium_present = False
for path in os.environ["PATH"].split(os.pathsep):
if os.path.isfile(os.path.join(path, 'apertium')):
apertium_present = True
break
if not apertium_present:
print(
f"apertium is either not installed or not added to path, see {apertium_url}\n")
misconfigured = True
if not isinstance(config['TRAINING_LINES'], int):
print(
f"'{config['TRAINING_LINES']}'(TRAINING_LINES) is not an integer. pass an integer \n")
misconfigured = True
if not isinstance(config['MAX_RULES'], int):
print(
f"'{config['MAX_RULES']}'(MAX_RULES) is not an integer. pass an integer \n")
misconfigured = True
if not (isinstance(config['CRISPHOLD'], int) or isinstance(config['CRISPHOLD'], float)):
print(
f"'{config['CRISPHOLD']}'(CRISPHOLD) is not an integer. pass an integer \n")
misconfigured = True
if not isinstance(config['IS_PARALLEL'], bool):
print(
f"'{config['IS_PARALLEL']}'(IS_PARALLEL) is not an boolean. pass true or false \n")
misconfigured = True
else:
if config['IS_PARALLEL']:
if not os.path.isabs(config['FAST_ALIGN']):
config['FAST_ALIGN'] = os.path.join(
os.path.abspath('.'), config['FAST_ALIGN'])
if not os.path.isfile(config['FAST_ALIGN']):
print(
f"'{config['FAST_ALIGN']}'(FAST_ALIGN) is not a file, provide a valid executable or \nto install, follow {fast_align_url}\n")
misconfigured = True
if modes:
tl_sl_autobil = get_autobil(modes, config['LANG_DATA'], config['REVERSE_PAIR'])
if not tl_sl_autobil:
misconfigured = True
if tl_sl_autobil and not os.path.exists(tl_sl_autobil):
print(f"'{tl_sl_autobil}' is not in '{config['LANG_DATA']}' (LANG_DATA), "
+ f"provide a valid directory or to install, follow {langs_url}")
misconfigured = True
yasmet_present = False
for path in os.environ["PATH"].split(os.pathsep):
if os.path.isfile(os.path.join(path, 'yasmet')):
yasmet_present = True
break
if not yasmet_present:
print(
f"yasmet is either not installed or not added to path, install yasmet and add to the path, \
{yasmet_url} or \nre-install apertium-lex-tools with yasmet, {apertium_url}\n")
misconfigured = True
process_tagger_output_present = False
for path in os.environ["PATH"].split(os.pathsep):
if os.path.isfile(os.path.join(path, 'process-tagger-output')):
process_tagger_output_present = True
break
if not process_tagger_output_present:
print(
f"process-tagger-output is not installed, re-install apertium-lex-tools {apertium_url}\n")
misconfigured = True
# else:
# if 'fast_align' not in os.listdir(config['FAST_ALIGN']):
# print("fast_align is not present in", "'"+config['FAST_ALIGN']+"'(FAST_ALIGN),", \
# "provide a valid directory or \nto install, follow", fast_align_url, '\n')
# misconfigured = True
is_lex_tools_present = False
for lex_tools in lex_tools_paths:
if os.path.isdir(lex_tools):
scripts = ['extract-sentences.py', 'extract-freq-lexicon.py',
'ngram-count-patterns-maxent2.py', 'merge-ngrams-lambdas.py', 'lambdas-to-rules.py',
'ngrams-to-rules-me.py', 'common.py']
for script in scripts:
if not os.path.isfile(os.path.join(lex_tools, script)):
print(
f"'{script}' is not present in '{lex_tools}', re-install apertium-lex-tools {apertium_url}\n")
misconfigured = True
is_lex_tools_present = True
if not is_lex_tools_present:
print(
f"apertium_lex_tools scripts are not installed, re-install apertium-lex-tools {apertium_url}\n")
misconfigured = True
else: # non-parallel
if modes and os.path.isdir(config['LANG_DATA']):
mode_after_biltrans = get_mode_after_biltrans(modes, config['LANG_DATA'], config['PAIR'])
filepaths = (e for m in mode_after_biltrans for e in m[1])
for fp in filepaths:
if fp not in os.listdir(config['LANG_DATA']):
print(f"'{fp}' is not in '{config['LANG_DATA']}' (LANG_DATA)"
+ " – is the language pair compiled? Please provide a "
+ "valid directory or to install, follow {langs_url}")
misconfigured = True
if not os.path.isfile(os.path.join(irstlm_path(), 'bin/build-lm.sh')):
if 'IRSTLM' not in os.environ:
print(
"IRSTLM doesn't seem to be installed to /usr/lib/irstlm,"
+ " couldn't find /usr/lib/irstlm/bin/build-lm.sh (if you installed"
+ f" it elsewhere, set the environment variable IRSTLM), see {irstlm_url}\n")
misconfigured = True
else:
print(
f"'bin/build-lm.sh' is not present in $IRSTLM ('{os.environ['IRSTLM']}'), see {irstlm_url}\n")
misconfigured = True
multitrans_present = False
for path in os.environ["PATH"].split(os.pathsep):
if os.path.isfile(os.path.join(path, 'multitrans')):
multitrans_present = True
break
if not multitrans_present:
print(
f"multitrans is not installed, re-install apertium-lex-tools {apertium_url}\n")
misconfigured = True
ranker_present = False
for path in os.environ["PATH"].split(os.pathsep):
if os.path.isfile(os.path.join(path, 'irstlm-ranker')):
ranker_present = True
break
if not ranker_present:
print(
f"irstlm-ranker is not installed, re-install apertium-lex-tools with irstlm {apertium_url}\n")
misconfigured = True
is_lex_tools_present = False
for lex_tools in lex_tools_paths:
if os.path.isdir(lex_tools):
scripts = ['biltrans-extract-frac-freq.py', 'extract-alig-lrx.py',
'biltrans-count-patterns-ngrams.py', 'ngram-pruning-frac.py', 'ngrams-to-rules.py',
'biltrans_count_common.py', 'common.py']
for script in scripts:
if not os.path.isfile(os.path.join(lex_tools, script)):
print(
f"'{script}' is not present in '{lex_tools}', re-install apertium-lex-tools {apertium_url}\n")
misconfigured = True
is_lex_tools_present = True
if not is_lex_tools_present:
print(
f"apertium_lex_tools scripts are not installed, re-install apertium-lex-tools {apertium_url}\n")
misconfigured = True
if 'TL_MODEL' in config:
if not os.path.isfile(config['TL_MODEL']):
print(
f"'{config['TL_MODEL']}'(TL_MODEL) is not a file, provide a valid file or \nto build, see {irstlm_url}\n")
misconfigured = True
if misconfigured:
exit(1)
else:
print("Prerequisites are properly installed.")
return config
if __name__ == '__main__':
config_file = 'config.toml'
if(len(sys.argv) == 2):
config_file = sys.argv[1]
check_config(config_file)