Example #1
0
import hfst
import hfst_commandline

short_getopts = 'i:o:'
long_getopts = ['input=', 'output=']
options = hfst_commandline.hfst_getopt(short_getopts, long_getopts, 2)
istreams = hfst_commandline.get_two_hfst_input_streams(options)
istr1 = istreams[0][0]
istr2 = istreams[1][0]

# raise RuntimeError('Usage: hfst-shuffle.py INFILE1 INFILE2')
if (istr1.get_type() != istr2.get_type()):
    raise RuntimeError('Error: transducer types differ in ' + argv[1] +
                       ' and ' + argv[2])
ostr = hfst_commandline.get_one_hfst_output_stream(options,
                                                   istr1.get_type())[0]

while ((not istr1.is_eof()) and (not istr2.is_eof())):
    tr1 = istr1.read()
    tr2 = istr2.read()
    tr1.shuffle(tr2)
    tr1.write(ostr)
    ostr.flush()

istr1.close()
istr2.close()
ostr.close()
Example #2
0
transducers_written=0

short_opts = 'f:HS'
long_opts = ['format=','do-not-harmonize','semicolon']
options = hfst_commandline.hfst_getopt(short_opts, long_opts, 1)
for opt in options[0]:
    if opt[0] == '-f' or opt[0] == '--format':
        impl = hfst_commandline.get_implementation_type(opt[1])
    elif opt[0] == '-H' or opt[0] == '--do-not-harmonize':
        harmonize= False
    elif opt[0] == '-S' or opt[0] == '--semicolon':
        semicolons = True
    else:
        pass
istr = hfst_commandline.get_one_text_input_stream(options)[0]
ostr = hfst_commandline.get_one_hfst_output_stream(options, impl)[0]

comp = hfst.XreCompiler(impl)
comp.set_harmonization(harmonize)
if (semicolons):
    data = istr.read()
    i=0
    while(i < len(data)):
        tr_and_chars_read = comp.compile_first(data[i:]) # HFST 4.0: document this
        tr = tr_and_chars_read[0]
        i = i + tr_and_chars_read[1]
        if tr != None:
            ostr.write(tr)
            transducers_written = transducers_written + 1
        else:
            if comp.contained_only_comments(): # HFST 4.0: should return a str 'only comments' instead?
Example #3
0
long_getopts = ['silent','output=','input=','from-file=','from-label=','to-label=']
options = hfst_commandline.hfst_getopt(short_getopts, long_getopts, 1)
for opt in options [0]:
    if opt[0] == '-s' or opt[0] == '--silent':
        silent = True
    elif opt[0] == '-F' or opt[0] == '--from-file':
        from_file = opt[1]
    elif opt[0] == '-f' or opt[0] == '--from-label':
        from_label = opt[1]
    elif opt[0] == '-t' or opt[0] == '--to-label':
        to_label = opt[1]
    else:
        pass

istr = hfst_commandline.get_one_hfst_input_stream(options)[0]
ostr = hfst_commandline.get_one_hfst_output_stream(options, istr.get_type())[0]

def eps(s):
    if s == "@0@":
        return hfst.EPSILON
    else:
        return s

substitutions = {}
if from_file != None:
    f = open(from_file)
    for line in f:
        line = line.rstrip()
        repl = line.split('\t')
        if len(repl) != 2:
            raise RuntimeError('hfst-substitute.py: --from-file FILE: FILE must list substitutions line by line, input and output symbols separated by tabulators.')
Example #4
0
transducers_written = 0

short_opts = 'f:HS'
long_opts = ['format=', 'do-not-harmonize', 'semicolon']
options = hfst_commandline.hfst_getopt(short_opts, long_opts, 1)
for opt in options[0]:
    if opt[0] == '-f' or opt[0] == '--format':
        impl = hfst_commandline.get_implementation_type(opt[1])
    elif opt[0] == '-H' or opt[0] == '--do-not-harmonize':
        harmonize = False
    elif opt[0] == '-S' or opt[0] == '--semicolon':
        semicolons = True
    else:
        pass
istr = hfst_commandline.get_one_text_input_stream(options)[0]
ostr = hfst_commandline.get_one_hfst_output_stream(options, impl)[0]

comp = hfst.XreCompiler(impl)
comp.set_harmonization(harmonize)
if (semicolons):
    data = istr.read()
    i = 0
    while (i < len(data)):
        tr_and_chars_read = comp.compile_first(
            data[i:])  # HFST 4.0: document this
        tr = tr_and_chars_read[0]
        i = i + tr_and_chars_read[1]
        if tr != None:
            ostr.write(tr)
            transducers_written = transducers_written + 1
        else:
Example #5
0
import hfst
import hfst_commandline
options = hfst_commandline.hfst_getopt('', [], 1)
istr = hfst_commandline.get_one_text_input_stream(options)

defs = None
from sys import stdin
if istr[0] == stdin:
    exp = str(istr[0].read())
    defs = hfst.compile_pmatch_expression(exp)
else:
    istr[0].close()
    defs = hfst.compile_pmatch_file(options[1][0])  # todo istr[1]

#    raise RuntimeError('error: hfst-pmatch2fst.py [INFILE]')

ostr = hfst_commandline.get_one_hfst_output_stream(
    options, hfst.ImplementationType.HFST_OLW_TYPE)[0]
for tr in defs:
    ostr.write(tr)
ostr.close()
Example #6
0
import hfst
import hfst_commandline
options = hfst_commandline.hfst_getopt('', [], 1)
istr = hfst_commandline.get_one_text_input_stream(options)

defs=None
from sys import stdin
if istr[0] == stdin:
    exp = str(istr[0].read())
    defs = hfst.compile_pmatch_expression(exp)
else:
    istr[0].close()
    defs = hfst.compile_pmatch_file(options[1][0]) # todo istr[1]

#    raise RuntimeError('error: hfst-pmatch2fst.py [INFILE]')

ostr = hfst_commandline.get_one_hfst_output_stream(options, hfst.ImplementationType.HFST_OLW_TYPE)[0]
for tr in defs:
    ostr.write(tr)
ostr.close()