How to use null flush in python

From Apertium
Jump to navigation Jump to search

Many Apertium executables have "null flush" modes (usually with -z), which allows the executable to run once, stay open, accept input, and flush the output only on a null character.

Here's a simple example of how to implement a wrapper in python3 around lt-proc and a transducer.

from subprocess import Popen, PIPE

things_to_transduce = ['foo', 'bar', 'hargle', 'bargle']

transducer_process = Popen(["lt-proc", "-t", "-z", "transducer.bin"], stdin=PIPE, stdout=PIPE)

def transduce(inputString):
	transducer_process.stdin.write(bytes('{}\n'.format(inputString), 'utf-8'))
	transducer_process.stdin.write(b'\0')
	transducer_process.stdin.flush()
	return repr(transducer_process.stdout.readline().strip(b'\0').strip(b'\n').decode())

for thing_to_transduce in things_to_transduce:
	print(transduce(thing_to_transduce))

transducer_process.stdin.close()
transducer_process.wait()

There's a more involved example in the lttoolbox tests, which adds handling for when the process hangs.