import hfst import os.path assert os.path.isfile('streets.txt') # pmatch transducers are always in ol format, so this has actually no effect... for type in [ hfst.ImplementationType.SFST_TYPE, hfst.ImplementationType.TROPICAL_OPENFST_TYPE, hfst.ImplementationType.FOMA_TYPE ]: if hfst.HfstTransducer.is_implementation_type_available(type): hfst.set_default_fst_type(type) # (1) compile the file directly defs = hfst.compile_pmatch_file('streets.txt') cont = hfst.PmatchContainer(defs) assert cont.match( "Je marche seul dans l'avenue des Ternes." ) == "Je marche seul dans l'<FrenchStreetName>avenue des Ternes</FrenchStreetName>." # (2) compile the contents of file with open('streets.txt', 'r') as myfile: data = myfile.read() myfile.close() defs = hfst.compile_pmatch_expression(data) cont = hfst.PmatchContainer(defs) assert cont.match( "Je marche seul dans l'avenue des Ternes." ) == "Je marche seul dans l'<FrenchStreetName>avenue des Ternes</FrenchStreetName>."
import hfst import os.path assert os.path.isfile('streets.txt') # pmatch transducers are always in ol format, so this has actually no effect... for type in [hfst.ImplementationType.SFST_TYPE, hfst.ImplementationType.TROPICAL_OPENFST_TYPE, hfst.ImplementationType.FOMA_TYPE]: if hfst.HfstTransducer.is_implementation_type_available(type): print(hfst.fst_type_to_string(type)) hfst.set_default_fst_type(type) # (1) compile the file directly defs = hfst.compile_pmatch_file('streets.txt') cont = hfst.PmatchContainer(defs) assert cont.match("Je marche seul dans l'avenue des Ternes.") == "Je marche seul dans l'<FrenchStreetName>avenue des Ternes</FrenchStreetName>." # (2) compile the contents of file with open('streets.txt', 'r') as myfile: data=myfile.read() myfile.close() defs = hfst.compile_pmatch_expression(data) cont = hfst.PmatchContainer(defs) assert cont.match("Je marche seul dans l'avenue des Ternes.") == "Je marche seul dans l'<FrenchStreetName>avenue des Ternes</FrenchStreetName>." # (3) try to compile a nonexistent file nonexistent_file = 'foofoofoofoofoofoofoofoofoofoofoofoo' assert not os.path.isfile(nonexistent_file) try: hfst.compile_pmatch_file(nonexistent_file) assert False except IOError as e:
import hfst defs = None from sys import argv if len(argv) < 2: from sys import stdin exp = str(stdin.read()) defs = hfst.compile_pmatch_expression(exp) elif len(argv) == 2: defs = hfst.compile_pmatch_file(argv[1]) else: raise RuntimeError('error: hfst-pmatch2fst.py [INFILE]') ostr = hfst.HfstOutputStream(type=hfst.ImplementationType.HFST_OLW_TYPE) for tr in defs: ostr.write(tr) ostr.close()
import hfst import hfst_commandline options = hfst_commandline.hfst_getopt('', [], 1) istr = hfst_commandline.get_one_text_input_stream(options) defs = None from sys import stdin if istr[0] == stdin: exp = str(istr[0].read()) defs = hfst.compile_pmatch_expression(exp) else: istr[0].close() defs = hfst.compile_pmatch_file(options[1][0]) # todo istr[1] # raise RuntimeError('error: hfst-pmatch2fst.py [INFILE]') ostr = hfst_commandline.get_one_hfst_output_stream( options, hfst.ImplementationType.HFST_OLW_TYPE)[0] for tr in defs: ostr.write(tr) ostr.close()
from sys import argv if len(argv) != 2: raise RuntimeError('Usage: hfst-pmatch2fst.py INFILE') infilename = argv[1] import hfst defs = hfst.compile_pmatch_file(infilename) ostr = hfst.HfstOutputStream(type=hfst.ImplementationType.HFST_OLW_TYPE) for tr in defs: ostr.write(tr) ostr.close()
import hfst import hfst_commandline options = hfst_commandline.hfst_getopt('', [], 1) istr = hfst_commandline.get_one_text_input_stream(options) defs=None from sys import stdin if istr[0] == stdin: exp = str(istr[0].read()) defs = hfst.compile_pmatch_expression(exp) else: istr[0].close() defs = hfst.compile_pmatch_file(options[1][0]) # todo istr[1] # raise RuntimeError('error: hfst-pmatch2fst.py [INFILE]') ostr = hfst_commandline.get_one_hfst_output_stream(options, hfst.ImplementationType.HFST_OLW_TYPE)[0] for tr in defs: ostr.write(tr) ostr.close()
import hfst defs=None from sys import argv if len(argv) < 2: from sys import stdin exp = str(stdin.read()) defs = hfst.compile_pmatch_expression(exp) elif len(argv) == 2: defs = hfst.compile_pmatch_file(argv[1]) else: raise RuntimeError('error: hfst-pmatch2fst.py [INFILE]') ostr = hfst.HfstOutputStream(type=hfst.ImplementationType.HFST_OLW_TYPE) for tr in defs: ostr.write(tr) ostr.close()