out.redirect(tr) assert(False) except: # libhfst.StreamIsClosedException: assert(libhfst.hfst_get_exception() == "StreamIsClosedException") pass # Transducer is cyclic. # --------------------- print("TransducerIsCyclicException") for type in types: transducer = libhfst.HfstTransducer("a", "b", type) transducer.repeat_star() try: results = libhfst.detokenize_paths(libhfst.extract_paths(transducer)) print("The transducer has {0} paths".format(len(results))) assert(False) except: # libhfst.TransducerIsCyclicException: print("The transducer is cyclic and has an infinite number of paths.") # The stream does not contain transducers. # ---------------------------------------- print("NotTransducerStreamException") foofile = open('foofile', 'wb') foofile.write('This is a text file.\n'.encode('ascii')) foofile.write('Here is another line.\n'.encode('ascii')) foofile.write('The file ends here.'.encode('ascii')) foofile.close()
out.close() out.redirect(tr) assert (False) except: # libhfst.StreamIsClosedException: assert (libhfst.hfst_get_exception() == "StreamIsClosedException") pass # Transducer is cyclic. # --------------------- print("TransducerIsCyclicException") for type in types: transducer = libhfst.HfstTransducer("a", "b", type) transducer.repeat_star() try: results = libhfst.detokenize_paths(libhfst.extract_paths(transducer)) print("The transducer has {0} paths".format(len(results))) assert (False) except: # libhfst.TransducerIsCyclicException: print("The transducer is cyclic and has an infinite number of paths.") # The stream does not contain transducers. # ---------------------------------------- print("NotTransducerStreamException") foofile = open('foofile', 'wb') foofile.write('This is a text file.\n'.encode('ascii')) foofile.write('Here is another line.\n'.encode('ascii')) foofile.write('The file ends here.'.encode('ascii')) foofile.close() try:
import libhfst import io tr = libhfst.HfstTransducer('a', 'b', libhfst.TROPICAL_OPENFST_TYPE) paths = libhfst.extract_paths(tr) for path in libhfst.detokenize_paths(paths): print("{0}:{1} {2}".format(path.input, path.output, path.weight)) tr = libhfst.HfstTransducer('a', 'b', libhfst.TROPICAL_OPENFST_TYPE) tr.convert(libhfst.HFST_OLW_TYPE) for path in libhfst.detokenize_paths(tr.lookup("a")): print("{0} {1}".format(path.output, path.weight))
# Create a transducer that contains animals tok = libhfst.HfstTokenizer() cat = libhfst.HfstTransducer("cat", "cats", tok, type) cat.set_final_weights(3) dog = libhfst.HfstTransducer("dog", "dogs", tok, type) dog.set_final_weights(2.5) mouse = libhfst.HfstTransducer("mouse", "mice", tok, type) mouse.set_final_weights(1.7) animals = libhfst.HfstTransducer(type) animals.disjunct(cat) animals.disjunct(dog) animals.disjunct(mouse) animals.minimize() results = libhfst.extract_paths(animals, 3, 0) #print results # Add an animal with two possible plurals hippopotamus1 = libhfst.HfstTransducer("hippopotamus", "hippopotami", tok, type) hippopotamus1.set_final_weights(1.2) hippopotamus2 = libhfst.HfstTransducer("hippopotamus", "hippopotamuses", tok, type) hippopotamus2.set_final_weights(1.4) animals.disjunct(hippopotamus1) animals.disjunct(hippopotamus2) animals.minimize() results = libhfst.extract_paths(animals, 5, 0) #print results # Convert into optimized lookup format