简体   繁体   中英

How to visualize Pyfst transducers via dot files

I am learning how to create transducers with Pyfst and I am trying to visualize the ones I create. The ultimate goal is to be able to write the transducers to dot files and see them in Graphviz.

I took a sample code to see how to visualize the following acceptor.

a = fst.Acceptor()
a.add_arc(0, 1, 'x', 0.1)
a[1].final = -1
a.draw()

When I use draw(), which comes with the package, I get an error:

File "/Users/.../tests.py", line 42, in <module>
a.draw()

File "_fst.pyx", line 816, in fst._fst.StdVectorFst.draw
(fst/_fst.cpp:15487)

File "/Users/.../venv-3.6/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)

TypeError: cannot use a string pattern on a bytes-like object

If I try to write the above mentioned acceptor to .dot via this:

def fst_dot(dot_object, filename):
path, file = split(filename)
new_path = join(dot_files_folder_path, path)
if not os.path.exists(new_path):
    os.makedirs(new_path)
if hasattr(dot_object, 'dotFormat'):
    draw_string = dot_object.dotFormat()
else:
    draw_string = dot_object.draw()
open(join(dot_files_folder_path, filename + ".dot"), "w").write(draw_string)

then also I get the following error:

File "/Users/...tests.py", line 43, in <module>
fst_dot(a, 'acceptor')

File "/Users/...tests.py", line 22, in fst_dot
draw_string = dot_object.draw()

File "_fst.pyx", line 816, in fst._fst.StdVectorFst.draw
(fst/_fst.cpp:15487)

File "/Users/.../venv-3.6/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)

TypeError: cannot use a string pattern on a bytes-like object

So, both errors look the same - there is some kind of an issue with draw(). On the pyfst site it says that draw is used for dot format representation of the transducer.

I can't understand how to fix the error. Any help would be greatly appreciated.

I am using OSX and PyCharm.

You might try using Python2 to see if that helps at all.

However, I think you'll be better off using the Python bindings that are included with OpenFST 1.5+. That library also has the ability to write to GraphViz .dot files. There is documentation available here:

http://www.openfst.org/twiki/bin/view/FST/PythonExtension

I recommend you fstdraw command from openfst.

after a.write('tmp.fst') in python. $ fstdraw tmp.fst > tmp.dot in shell.

EDIT:

Finally, I found that UFAL's forked pyfst works fine with python3. https://github.com/UFAL-DSG/pyfst

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM