Difference between revisions of "How to use null flush in python"

From Apertium
Jump to navigation Jump to search
(Created page with "Many Apertium executables have "null flush" modes (usually with <tt>-z</tt>), which allows the executable to run once, stay open, accept input, and flush the output only on a...")
 
 
(One intermediate revision by the same user not shown)
Line 22: Line 22:
transducer_process.wait()
transducer_process.wait()
</pre>
</pre>

There's a [https://github.com/apertium/lttoolbox/blob/master/tests/basictest.py more involved example in the lttoolbox tests], which adds handling for when the process hangs.


[[Category:Documentation]]

Latest revision as of 18:12, 15 July 2022

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.