def get_hfst_transducer(name): hfstpath = os.path.join(DIR, name + '.hfst') attpath = os.path.join(DIR, name + '.att') hfst.set_default_fst_type(hfst.ImplementationType.FOMA_TYPE) hfst.compile_xfst_file(hfstpath, output=sys.stderr) tr = get_att_transducer(attpath) tr.lookup_optimize() return tr
'Usage: compile_xfst.py [sfst|openfst|foma] FILENAME [[PYTHONPATH]]') if len(sys.argv) == 4: sys.path.insert(0, sys.argv[3]) import hfst if (sys.argv[1] == 'sfst'): if (hfst.HfstTransducer.is_implementation_type_available( hfst.ImplementationType.SFST_TYPE)): hfst.set_default_fst_type(hfst.ImplementationType.SFST_TYPE) else: raise RuntimeError('Format sfst is not available.') elif (sys.argv[1] == 'openfst' or sys.argv[1] == "openfst-tropical"): if (hfst.HfstTransducer.is_implementation_type_available( hfst.ImplementationType.TROPICAL_OPENFST_TYPE)): hfst.set_default_fst_type( hfst.ImplementationType.TROPICAL_OPENFST_TYPE) else: raise RuntimeError('Format openfst is not available.') elif (sys.argv[1] == 'foma'): if (hfst.HfstTransducer.is_implementation_type_available( hfst.ImplementationType.FOMA_TYPE)): hfst.set_default_fst_type(hfst.ImplementationType.FOMA_TYPE) else: raise RuntimeError('Format foma is not available.') else: raise RuntimeError('Transducer format "' + sys.argv[1] + '" not recognized.') if hfst.compile_xfst_file(sys.argv[2]) != 0: raise RuntimeError('Compiling file "' + sys.argv[2] + '" failed.')
import sys if len(sys.argv) < 3 or len(sys.argv) > 4: raise RuntimeError('Usage: compile_xfst.py [sfst|openfst|foma] FILENAME [[PYTHONPATH]]') if len(sys.argv) == 4: sys.path.insert(0, sys.argv[3]) import hfst if (sys.argv[1] == 'sfst'): if (hfst.HfstTransducer.is_implementation_type_available(hfst.ImplementationType.SFST_TYPE)): hfst.set_default_fst_type(hfst.ImplementationType.SFST_TYPE) else: raise RuntimeError('Format sfst is not available.') elif (sys.argv[1] == 'openfst' or sys.argv[1] == "openfst-tropical"): if (hfst.HfstTransducer.is_implementation_type_available(hfst.ImplementationType.TROPICAL_OPENFST_TYPE)): hfst.set_default_fst_type(hfst.ImplementationType.TROPICAL_OPENFST_TYPE) else: raise RuntimeError('Format openfst is not available.') elif (sys.argv[1] == 'foma'): if (hfst.HfstTransducer.is_implementation_type_available(hfst.ImplementationType.FOMA_TYPE)): hfst.set_default_fst_type(hfst.ImplementationType.FOMA_TYPE) else: raise RuntimeError('Format foma is not available.') else: raise RuntimeError('Transducer format "' + sys.argv[1] + '" not recognized.') if hfst.compile_xfst_file(sys.argv[2]) != 0 : raise RuntimeError('Compiling file "' + sys.argv[2] + '" failed.')
# Other functions (TODO: more extensixe checks) tr = hfst.regex('[foo]|[foo bar]|[f o o bar baz]') if not tr.longest_path_size() == 5: raise RuntimeError(get_linenumber()) result = tr.extract_longest_paths() if not len(result) == 1: raise RuntimeError(get_linenumber()) result = tr.extract_shortest_paths() if not len(result) == 1: raise RuntimeError(get_linenumber()) # XfstCompiler if int(version[0]) > 2: import io msg = io.StringIO() if hfst.compile_xfst_file('test_pass.xfst', verbosity=0, output=msg, error=msg) != 0: raise RuntimeError(get_linenumber()) if hfst.compile_xfst_file('test_fail.xfst', verbosity=0, output=msg, error=msg) == 0: raise RuntimeError(get_linenumber()) if hfst.compile_xfst_file('test_fail.xfst', quit_on_fail=False, verbosity=0, output=msg, error=msg) != 0: raise RuntimeError(get_linenumber()) # regex compiler import io msg = io.StringIO() msg.write(unicode('This is the error message:\n', 'utf-8')) tr = hfst.regex('foo\\', error=msg) if (tr == None): msg.write(unicode('This was the error message.\n', 'utf-8')) # print(msg.getvalue()) import sys