Exemple #1
0
# The examples given in doxygen documentation

import libhfst

# StreamIsClosedException
try:
    tr = libhfst.regex('foo')
    outstr = libhfst.HfstOutputStream(filename='testfile')
    outstr.close()
    outstr.write(tr)
except libhfst.StreamIsClosedException:
    print("Could not write transducer: stream to file was closed.")

# TransducerIsCyclicException
transducer = libhfst.regex('[a:b]*')
try:
    results = transducer.extract_paths(output='text')
    print("The transducer has %i paths:" % len(results))
    print(results)
except libhfst.TransducerIsCyclicException:
    print(
        "The transducer is cyclic and has an infinite number of paths. Some of them:"
    )
    results = transducer.extract_paths(output='text', max_cycles=5)
    print(results)

# NotTransducerStreamException
f = open('foofile', 'w')
f.write('This is an ordinary text file.\n')
f.close()
try:
Exemple #2
0
import libhfst
libhfst.set_default_fst_type(libhfst.FOMA_TYPE)
ab = libhfst.regex('a:b')
out = libhfst.HfstOutputStream(hfst_format=False)
out.write(ab)
out.flush()
out.close()
Exemple #3
0
ttype = 0
if sys.argv[1] == 'sfst':
    ttype = libhfst.SFST_TYPE
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)
Exemple #4
0
import libhfst

tr1 = libhfst.regex('föö:bär')
tr2 = libhfst.regex('0')
tr3 = libhfst.regex('0-0')

ostr = libhfst.HfstOutputStream()
ostr.write(tr1)
ostr.write(tr2)
ostr.write(tr3)
ostr.flush()
ostr.close()
Exemple #5
0
#for type in types:
#    try:
#        tr = libhfst.HfstTransducer("foo", type)
#        out = libhfst.HfstOutputStream("testfile", type)
#        out.redirect(tr)
#    except libhfst.StreamCannotBeWrittenException:
#        print "ERROR: file cannot be written."

# Stream is closed.
# -----------------
print("StreamIsClosedException")

for type in types:
    try:
        tr = libhfst.HfstTransducer("foo", type)
        out = libhfst.HfstOutputStream("testfile", type)
        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:
Exemple #6
0
        numtr += 1
        tr3 = istr.read()
        numtr += 1
    except libhfst.EndOfStreamException:
        pass
    except:
        raise RuntimeError(get_linenumber())
    istr.close()

    if numtr != 2:
        raise RuntimeError(get_linenumber())

    tr1.convert(libhfst.get_default_fst_type())
    tr2.convert(libhfst.get_default_fst_type())

    ostr = libhfst.HfstOutputStream(filename='foobar2.hfst')
    ostr.write(tr1)
    ostr.write(tr2)
    ostr.flush()
    ostr.close()

    TR1 = None
    TR2 = None
    TR3 = None

    istr = libhfst.HfstInputStream('foobar2.hfst')
    numtr = 0
    try:
        TR1 = istr.read()
        numtr += 1
        TR2 = istr.read()
Exemple #7
0
import os


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.")