mhulden / foma

Automatically exported from code.google.com/p/foma
117 stars 90 forks source link

How to save the output in txt (or any other) format? #136

Open puputrizqiyah opened 2 years ago

puputrizqiyah commented 2 years ago

Hello, i would like to ask, can we export the output of foma into some txt file or any other file?

mhulden commented 2 years ago

Standard redirects work within foma for apply down/up. For example:

down < testwords.txt > output.txt

would read words from westwards.txt, apply the transductions, and send the results to output.txt.

Likewise, words and pairs have the same behavior if you just want to extract the set of strings in an automaton or relations from a transducer to a file. Of course, these would have to be finite in number:

pairs > inoutpairs.txt

More control can be gotten by the separate utility flookup which has various flags for passing words through a transducer as a pipe (if that transducer has been saved as a binary through save stack). Minimal working example:

foma[0]: regex a:c | b:d;
339 bytes. 2 states, 2 arcs, 2 paths.
foma[1]: save stack mytransducer.fomabin
Writing to file mytransducer.fomabin.
foma[1]: ^C

bash$  echo -e "a\nb" | flookup -i mytransducer.fomabin 
a   c

b   d

As was mentioned earlier, you can also use the Python interface to load a binary transducer (or to compile one from regular expressions), and pass words to it to perform the transduction. Another minimal working example in Python using the previous transducer:

>>> from foma import *
>>> myFST = FST.load("mytransducer.fomabin")
>>> myFST['a']
['c']
>>> myFST['b']
['d']