如何通过点文件可视化 Pyfst 换能器



我正在学习如何用Pyfst创建换能器,我正在尝试可视化我创建的换能器。最终目标是能够将换能器写入点文件并在 Graphviz 中查看它们。

我拿了一个示例代码来了解如何可视化以下接受器。

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

当我使用软件包附带的draw()时,我收到一个错误:

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

如果我尝试通过以下方式将上述接受器写入 .dot:

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)

然后我也收到以下错误:

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

因此,这两个错误看起来相同 - draw() 存在某种问题。在pyfst网站上,它说绘制用于换能器的点格式表示。

我不明白如何解决错误。任何帮助将不胜感激。

我正在使用OSX和PyCharm。

你可以尝试使用 Python2 看看是否有帮助。

但是,我认为最好使用OpenFST 1.5+中包含的Python绑定。 该库还能够写入 GraphViz .dot 文件。 此处提供了文档:

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

我建议你从openfst fstdraw命令。

在 Python 中a.write('tmp.fst')之后。 $ fstdraw tmp.fst > tmp.dot壳。

编辑:

最后,我发现 UFAL 的分叉 pyfst 在 python3 中工作得很好。https://github.com/UFAL-DSG/pyfst

最新更新