# 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")
    ifile.close()
    os.remove('test.att')

  
print("HfstBasicTransducer: symbol handling")

t.add_symbol_to_alphabet("foo")
Exemple #2
0
    print("file does not contain transducers.")


# The stream is not in valid AT&T format. 
# ---------------------------------------
print("NotValidAttFormatException")

testfile_att = open("testfile.att", "wb")
testfile_att.write('0 1 a b\n'.encode('ascii'))
testfile_att.write('1\n'.encode('ascii'))
testfile_att.write('c\n'.encode('ascii'))
testfile_att.close()

for type in types:
    transducers = []
    ifile = libhfst.hfst_open("testfile.att", "r")
    try:
        t = libhfst.HfstTransducer(ifile, type, "epsilon")
        transducers.append(t)
        print("read one transducer")
    except: # libhfst.NotValidAttFormatException:
        print("Error reading transducer: not valid AT&T format.")

ifile.close()
print("Read {0} transducers in total.".format(len(transducers)))


# State is not final (and cannot have a final weight). 
# ----------------------------------------------------
print("StateIsNotFinalException")
Exemple #3
0
try:
    instr = libhfst.HfstInputStream('foofile')
    tr = instr.read()
    print(tr)
    instr.close()
except libhfst.NotTransducerStreamException:
    print(
        "Could not print transducer: the file does not contain binary transducers."
    )

f = open('testfile1.att', 'w')
f.write('0 1 a b\n\
1 2 c\n\
2\n')
f.close()
f = libhfst.hfst_open('testfile1.att', 'r')
try:
    tr = libhfst.read_att(f)
except libhfst.NotValidAttFormatException:
    print('Could not read file: it is not in valid ATT format.')
f.close()

# StateIsNotFinalException
tr = libhfst.HfstBasicTransducer()
tr.add_state(1)
# An exception is thrown as state number 1 is not final
try:
    w = tr.get_final_weight(1)
except libhfst.StateIsNotFinalException:
    print("State is not final.")
# 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")
    ifile.close()
    os.remove('test.att')

print("HfstBasicTransducer: symbol handling")

t.add_symbol_to_alphabet("foo")
assert (alphabet_contains(t.get_alphabet(), "foo"))
Exemple #5
0
except:  # libhfst.NotTransducerStreamException:
    print("file does not contain transducers.")

# The stream is not in valid AT&T format.
# ---------------------------------------
print("NotValidAttFormatException")

testfile_att = open("testfile.att", "wb")
testfile_att.write('0 1 a b\n'.encode('ascii'))
testfile_att.write('1\n'.encode('ascii'))
testfile_att.write('c\n'.encode('ascii'))
testfile_att.close()

for type in types:
    transducers = []
    ifile = libhfst.hfst_open("testfile.att", "r")
    try:
        t = libhfst.HfstTransducer(ifile, type, "epsilon")
        transducers.append(t)
        print("read one transducer")
    except:  # libhfst.NotValidAttFormatException:
        print("Error reading transducer: not valid AT&T format.")

ifile.close()
print("Read {0} transducers in total.".format(len(transducers)))

# State is not final (and cannot have a final weight).
# ----------------------------------------------------
print("StateIsNotFinalException")

tr = libhfst.HfstBasicTransducer()
Exemple #6
0
def remove_generated_files():
    # fails on MinGW..
    #os.remove('foo.att')
    #os.remove('foo.hfst')
    pass

for ttype in (libhfst.SFST_TYPE, libhfst.TROPICAL_OPENFST_TYPE, libhfst.FOMA_TYPE):

    tr1 = libhfst.HfstTransducer('a', 'b', ttype)
    tr2 = libhfst.HfstTransducer('c', 'd', ttype)
    ostr = libhfst.HfstOutputStream('foo.hfst', tr1.get_type())
    ostr.redirect(tr1)
    ostr.redirect(tr2)
    ostr.close()
    att_file = libhfst.hfst_open('foo.att', 'w')

    istr = libhfst.HfstInputStream('foo.hfst')

    transducers_read = 0
    while True:
        try:
            tr = libhfst.HfstTransducer(istr)
            transducers_read += 1
            if transducers_read == 1:
                if not tr.compare(tr1):
                    print("ERROR: transducer 1 changed.")
                    remove_generated_files()
                    sys.exit(1)
            if transducers_read == 2:
                if not tr.compare(tr2):
Exemple #7
0
    tr = libhfst.regex('(a a:b (b));')
    tr.set_final_weights(0.1)
    tr.set_final_weights(0.4, True)

    # 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')
    f.close()

    numtr = 0
    f = libhfst.hfst_open('testfile.att', 'r')
    while not f.is_eof():
        TR = libhfst.read_att(f)
        numtr += 1
    try:
        libhfst.read_att(f)
    except libhfst.EndOfStreamException:
        pass
    f.close()
    if numtr != 2:
        raise RuntimeError(get_linenumber())

    f = libhfst.hfst_open('foo_att_prolog', 'w')
    f.write('-- in ATT format --\n')
    TR.write_att(f)
    f.write('-- in prolog format --\n')
Exemple #8
0
    # fails on MinGW..
    #os.remove('foo.att')
    #os.remove('foo.hfst')
    pass


for ttype in (libhfst.SFST_TYPE, libhfst.TROPICAL_OPENFST_TYPE,
              libhfst.FOMA_TYPE):

    tr1 = libhfst.HfstTransducer('a', 'b', ttype)
    tr2 = libhfst.HfstTransducer('c', 'd', ttype)
    ostr = libhfst.HfstOutputStream('foo.hfst', tr1.get_type())
    ostr.redirect(tr1)
    ostr.redirect(tr2)
    ostr.close()
    att_file = libhfst.hfst_open('foo.att', 'w')

    istr = libhfst.HfstInputStream('foo.hfst')

    transducers_read = 0
    while True:
        try:
            tr = libhfst.HfstTransducer(istr)
            transducers_read += 1
            if transducers_read == 1:
                if not tr.compare(tr1):
                    print("ERROR: transducer 1 changed.")
                    remove_generated_files()
                    sys.exit(1)
            if transducers_read == 2:
                if not tr.compare(tr2):
Exemple #9
0
try:
    instr = libhfst.HfstInputStream("foofile")
    tr = instr.read()
    print(tr)
    instr.close()
except libhfst.NotTransducerStreamException:
    print("Could not print transducer: the file does not contain binary transducers.")

f = open("testfile1.att", "w")
f.write(
    "0 1 a b\n\
1 2 c\n\
2\n"
)
f.close()
f = libhfst.hfst_open("testfile1.att", "r")
try:
    tr = libhfst.read_att(f)
except libhfst.NotValidAttFormatException:
    print("Could not read file: it is not in valid ATT format.")
f.close()

# StateIsNotFinalException
tr = libhfst.HfstBasicTransducer()
tr.add_state(1)
# An exception is thrown as state number 1 is not final
try:
    w = tr.get_final_weight(1)
except libhfst.StateIsNotFinalException:
    print("State is not final.")