for file in 'foofile', 'testfile', 'testfile.att', 'testfile.hfst': if os.path.exists(file): pass # doesn't work on MinGW.. #os.remove(file) types = [libhfst.SFST_TYPE, libhfst.TROPICAL_OPENFST_TYPE, libhfst.FOMA_TYPE] # The library required by the implementation type requested is not linked to HFST. # -------------------------------------------------------------------------------- print("ImplementationTypeNotAvailableException") for type in types: try: tr = libhfst.HfstTransducer("foo", "bar", type) except: # libhfst.ImplementationTypeNotAvailableException: assert(libhfst.hfst_get_exception() == "ImplementationTypeNotAvailableException") pass assert(libhfst.hfst_get_exception() == "") # Stream cannot be read. # ---------------------- #print "StreamNotReadableException" # #try: # instr = libhfst.HfstInputStream("foofile") #except libhfst.StreamNotReadableException: # print "ERROR: file cannot be read." # Stream cannot be written.
t.set_final_weight(s2, 1.0) assert(t.is_final_state(s2) and t.get_final_weight(s2) == 1.0) # Take a copy tc = libhfst.HfstBasicTransducer(t); print("HfstBasicTransducer exceptions") # Asking the weight of a non-final state for s in range(5): if s != s2: try: w = t.get_final_weight(s) assert(False) except: # libhfst.StateIsNotFinalException: assert(libhfst.hfst_get_exception() == "StateIsNotFinalException") pass # Reading a file in non-valid AT&T format ofile = open('test.att', 'wb') ofile.write("0\n0\t1\ta\tb\n1\t2\tb\n2\n".encode('ascii')) ofile.close() ifile = libhfst.hfst_open('test.att', 'rb') # ERROR On MinGW, using libhfst.hfst_open fails.. try: foo = libhfst.HfstBasicTransducer(ifile) ifile.close() os.remove('test.att') assert(False) except: # libhfst.NotValidAttFormatException: assert(libhfst.hfst_get_exception() == "NotValidAttFormatException")
t.set_final_weight(s2, 1.0) assert (t.is_final_state(s2) and t.get_final_weight(s2) == 1.0) # Take a copy tc = libhfst.HfstBasicTransducer(t) print("HfstBasicTransducer exceptions") # Asking the weight of a non-final state for s in range(5): if s != s2: try: w = t.get_final_weight(s) assert (False) except: # libhfst.StateIsNotFinalException: assert (libhfst.hfst_get_exception() == "StateIsNotFinalException") pass # Reading a file in non-valid AT&T format ofile = open('test.att', 'wb') ofile.write("0\n0\t1\ta\tb\n1\t2\tb\n2\n".encode('ascii')) ofile.close() ifile = libhfst.hfst_open( 'test.att', 'rb') # ERROR On MinGW, using libhfst.hfst_open fails.. try: foo = libhfst.HfstBasicTransducer(ifile) ifile.close() os.remove('test.att') assert (False) except: # libhfst.NotValidAttFormatException:
elif sys.argv[1] == 'openfst': ttype = libhfst.TROPICAL_OPENFST_TYPE elif sys.argv[1] == 'foma': ttype = libhfst.FOMA_TYPE else: print("ERROR: could not parse transducer format argument.") sys.exit(1) transducers_in_stream = int(sys.argv[2]) istr = libhfst.HfstInputStream() ostr = libhfst.HfstOutputStream(ttype) transducers_read = 0 transducers_written = 0 while True: try: tr = libhfst.HfstTransducer(istr) transducers_read += 1 ostr.redirect(tr) transducers_written += 1 except: # libhfst.EndOfStreamException: assert(libhfst.hfst_get_exception() == "EndOfStreamException") break; if transducers_read != transducers_in_stream: print("ERROR: wrong number of transducers read") sys.exit(1) istr.close() ostr.close()
for file in 'foofile', 'testfile', 'testfile.att', 'testfile.hfst': if os.path.exists(file): pass # doesn't work on MinGW.. #os.remove(file) types = [libhfst.SFST_TYPE, libhfst.TROPICAL_OPENFST_TYPE, libhfst.FOMA_TYPE] # The library required by the implementation type requested is not linked to HFST. # -------------------------------------------------------------------------------- print("ImplementationTypeNotAvailableException") for type in types: try: tr = libhfst.HfstTransducer("foo", "bar", type) except: # libhfst.ImplementationTypeNotAvailableException: assert (libhfst.hfst_get_exception() == "ImplementationTypeNotAvailableException") pass assert (libhfst.hfst_get_exception() == "") # Stream cannot be read. # ---------------------- #print "StreamNotReadableException" # #try: # instr = libhfst.HfstInputStream("foofile") #except libhfst.StreamNotReadableException: # print "ERROR: file cannot be read." # Stream cannot be written.
assert(t1.compare(t3)) print("Function shuffle") tok = libhfst.HfstTokenizer() t1 = libhfst.HfstTransducer("abc", "abc", tok, type) t2 = libhfst.HfstTransducer("cde", "cde", tok, type) t1.shuffle(t2) t3 = libhfst.HfstTransducer("abc", "abC", tok, type) try: t3.shuffle(t2) assert(False) except: # libhfst.TransducersAreNotAutomataException: assert(libhfst.hfst_get_exception() == "TransducersAreNotAutomataException") assert(True) print("Function convert") t1 = libhfst.HfstTransducer("foo", "bar", type) t2 = libhfst.HfstTransducer("foo", "bar", type) # Go through all implementation formats and get back to the original one for conv_type in types: t1.convert(conv_type) assert(compare_alphabets(t1, t2)) t1.convert(type) assert(t1.compare(t2)) assert(compare_alphabets(t1, t2))