commit beabaefdf2c4ab5fba0c450e40201d989ee4f97a Author: vaydheesh Date: Sat Aug 17 17:54:32 2019 +0530 added: apertium-destxt diff --git a/apertium/utils.py b/apertium/utils.py index 3c2c52c..8f67100 100644 --- a/apertium/utils.py +++ b/apertium/utils.py @@ -52,56 +52,63 @@ def execute_pipeline(inp: str, commands: List[List[str]]) -> str: # delete=False and manually delete the file. used_wrapper = True if wrappers_available: - input_file = tempfile.NamedTemporaryFile(delete=False, mode='w') - output_file = tempfile.NamedTemporaryFile(delete=False) - input_file_name, output_file_name = input_file.name, output_file.name - - arg = command[1][1] if len(command) >= 3 else '' - text = end.decode() - input_file.write(text) - input_file.close() - - if 'lt-proc' == command[0]: - dictionary_path = command[-1] - lttoolbox.LtLocale.tryToSetLocale() - fst = lttoolbox.FST(dictionary_path) - if not fst.valid(): - raise ValueError('FST Invalid') - fst.lt_proc(arg, input_file_name, output_file_name) - elif 'lrx-proc' == command[0]: - dictionary_path = command[-1] - lextools.LtLocale.tryToSetLocale() - lrx = lextools.LRXProc(dictionary_path) - lrx.lrx_proc(arg, input_file.name, output_file.name) - elif 'apertium-transfer' == command[0]: - transfer = apertium_core.ApertiumTransfer(command[2], command[3]) - transfer.transfer_text(arg, input_file.name, output_file.name) - elif 'apertium-interchunk' == command[0]: - interchunk = apertium_core.ApertiumInterchunk(command[1], command[2]) - interchunk.interchunk_text(arg, input_file.name, output_file.name) - elif 'apertium-postchunk' == command[0]: - postchunk = apertium_core.ApertiumPostchunk(command[1], command[2]) - postchunk.postchunk_text(arg, input_file.name, output_file.name) - elif 'apertium-pretransfer' == command[0]: - apertium_core.pretransfer(arg, input_file.name, output_file.name) - elif 'apertium-tagger' == command[0]: - command += [input_file.name, output_file.name] - apertium_core.ApertiumTagger(len(command), command) - elif 'cg-proc' == command[0]: - dictionary_path = command[-1] - cg = constraint_grammar.CGProc(dictionary_path) - cg_proc_command = command[:-1] - cg.cg_proc(len(cg_proc_command), cg_proc_command, input_file.name, output_file.name) + if 'apertium-destxt' == command[0]: + used_wrapper = True + _special_chars_map = {i: '\\' + chr(i) for i in b'[]{}?^$@\\'} + text = end.decode() + output = '{}.[][\n]'.format(text.translate(_special_chars_map)) + end = output.encode() else: - used_wrapper = False - - if used_wrapper: - output_file.seek(0) - end = output_file.read() - output_file.close() - - os.remove(input_file_name) - os.remove(output_file_name) + input_file = tempfile.NamedTemporaryFile(delete=False, mode='w') + output_file = tempfile.NamedTemporaryFile(delete=False) + input_file_name, output_file_name = input_file.name, output_file.name + + arg = command[1][1] if len(command) >= 3 else '' + text = end.decode() + input_file.write(text) + input_file.close() + + if 'lt-proc' == command[0]: + dictionary_path = command[-1] + lttoolbox.LtLocale.tryToSetLocale() + fst = lttoolbox.FST(dictionary_path) + if not fst.valid(): + raise ValueError('FST Invalid') + fst.lt_proc(arg, input_file_name, output_file_name) + elif 'lrx-proc' == command[0]: + dictionary_path = command[-1] + lextools.LtLocale.tryToSetLocale() + lrx = lextools.LRXProc(dictionary_path) + lrx.lrx_proc(arg, input_file.name, output_file.name) + elif 'apertium-transfer' == command[0]: + transfer = apertium_core.ApertiumTransfer(command[2], command[3]) + transfer.transfer_text(arg, input_file.name, output_file.name) + elif 'apertium-interchunk' == command[0]: + interchunk = apertium_core.ApertiumInterchunk(command[1], command[2]) + interchunk.interchunk_text(arg, input_file.name, output_file.name) + elif 'apertium-postchunk' == command[0]: + postchunk = apertium_core.ApertiumPostchunk(command[1], command[2]) + postchunk.postchunk_text(arg, input_file.name, output_file.name) + elif 'apertium-pretransfer' == command[0]: + apertium_core.pretransfer(arg, input_file.name, output_file.name) + elif 'apertium-tagger' == command[0]: + command += [input_file.name, output_file.name] + apertium_core.ApertiumTagger(len(command), command) + elif 'cg-proc' == command[0]: + dictionary_path = command[-1] + cg = constraint_grammar.CGProc(dictionary_path) + cg_proc_command = command[:-1] + cg.cg_proc(len(cg_proc_command), cg_proc_command, input_file.name, output_file.name) + else: + used_wrapper = False + + if used_wrapper: + output_file.seek(0) + end = output_file.read() + output_file.close() + + os.remove(input_file_name) + os.remove(output_file_name) if not wrappers_available or not used_wrapper: apertium.logger.warning('Calling subprocess %s', command[0]) proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)