hfst.set_default_fst_type(hfst.ImplementationType.SFST_TYPE) elif sys.argv[1] == 'foma': if not hfst.HfstTransducer.is_implementation_type_available( hfst.ImplementationType.FOMA_TYPE): sys.exit(77) hfst.set_default_fst_type(hfst.ImplementationType.FOMA_TYPE) elif sys.argv[1] == 'openfst': if not hfst.HfstTransducer.is_implementation_type_available( hfst.ImplementationType.TROPICAL_OPENFST_TYPE): sys.exit(77) hfst.set_default_fst_type(hfst.ImplementationType.TROPICAL_OPENFST_TYPE) else: raise RuntimeError('implementation format not recognized') transducers = [] try: while (True): transducers.append(hfst.read_att_transducer(sys.stdin)) except hfst.exceptions.EndOfStreamException: pass if not len(transducers) == 3: raise RuntimeError('Wrong number of transducers read.') i = 0 for re in ['föö:bär', '0', '0-0']: if not transducers[i].compare(hfst.regex(re)): raise RuntimeError('Transducers are not equivalent.') i += 1
import hfst for type in [hfst.ImplementationType.SFST_TYPE, hfst.ImplementationType.TROPICAL_OPENFST_TYPE, hfst.ImplementationType.FOMA_TYPE]: if hfst.HfstTransducer.is_implementation_type_available(type): transducers = [] ifile = open('testfile.att', 'r') try: while (True): t = hfst.read_att_transducer(ifile, '<eps>') transducers.append(t) except hfst.exceptions.NotValidAttFormatException as e: print("Error reading transducer: not valid AT&T format.") except hfst.exceptions.EndOfStreamException as e: pass ifile.close() assert(len(transducers) == 4)
# reading and writing in text format f = open('testfile_.att', 'w') f.write('0 1 foo bar 0.5\n\ 0 1 fo ba 0.2\n\ 0 1 f b 0\n\ 1 2 baz baz\n\ 2 0.1\n\ --\n\n') f.close() numtr = 0 f = open('testfile_.att', 'r') try: while True: TR = hfst.read_att_transducer(f) numtr += 1 except hfst.exceptions.EndOfStreamException: pass f.close() if numtr != 2: raise RuntimeError(get_linenumber()) f = open('foo_att_prolog', 'w') f.write('-- in ATT format --\n') TR.write_att(f) f.write('-- in prolog format --\n') TR.write_prolog(f) f.close() fsm = hfst.read_att_string(' 0\t 1 a b\n\
f.write('This is an ordinary text file.\n') f.close() try: instr = hfst.HfstInputStream('foofile') tr = instr.read() print(tr) instr.close() except hfst.exceptions.NotTransducerStreamException: print("Could not print transducer: the file does not contain binary transducers.") f = open('testfile1.att', 'w') f.write('0 1 a b\n1 2 c\n2\n') f.close() f = open('testfile1.att', 'r') try: tr = hfst.read_att_transducer(f) except hfst.exceptions.NotValidAttFormatException: print('Could not read file: it is not in valid ATT format.') f.close() # StateIsNotFinalException tr = hfst.HfstBasicTransducer() tr.add_state(1) # An exception is thrown as state number 1 is not final try: w = tr.get_final_weight(1) except hfst.exceptions.StateIsNotFinalException: print("State is not final.") # ContextTransducersAreNotAutomataException # try:
if not hfst.HfstTransducer.is_implementation_type_available(hfst.ImplementationType.SFST_TYPE): sys.exit(77) hfst.set_default_fst_type(hfst.ImplementationType.SFST_TYPE) elif sys.argv[1] == 'foma': if not hfst.HfstTransducer.is_implementation_type_available(hfst.ImplementationType.FOMA_TYPE): sys.exit(77) hfst.set_default_fst_type(hfst.ImplementationType.FOMA_TYPE) elif sys.argv[1] == 'openfst': if not hfst.HfstTransducer.is_implementation_type_available(hfst.ImplementationType.TROPICAL_OPENFST_TYPE): sys.exit(77) hfst.set_default_fst_type(hfst.ImplementationType.TROPICAL_OPENFST_TYPE) else: raise RuntimeError('implementation format not recognized') transducers = [] try: while (True): transducers.append(hfst.read_att_transducer(sys.stdin)) except hfst.exceptions.EndOfStreamException: pass if not len(transducers) == 3: raise RuntimeError('Wrong number of transducers read.') i = 0 for re in ['föö:bär','0','0-0']: if not transducers[i].compare(hfst.regex(re)): raise RuntimeError('Transducers are not equivalent.') i += 1