如何通过点文件可视化Pyfst传感器

时间:2017-01-19 17:11:01

标签: python-3.x dot transducer openfst fst

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

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

OrderByDescending(a=>a.TitleName == "Other" ? 0:1).ThenBy(a=>a.TitleName)

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

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

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

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

然后我也收到以下错误:

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)

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

我无法理解如何修复错误。任何帮助将不胜感激。

我正在使用OSX和PyCharm。

2 个答案:

答案 0 :(得分:3)

您可以尝试使用Python2来查看它是否有帮助。

但是,我认为使用OpenFST 1.5+附带的Python绑定会更好。该库还可以写入GraphViz .dot文件。这里有文档:

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

答案 1 :(得分:2)

我建议您使用openfst中的fstdraw命令。

在python中的a.write('tmp.fst')之后。 shell中的$ fstdraw tmp.fst > tmp.dot

修改

最后,我发现UFAL的分叉pyfst可以正常使用python3。 https://github.com/UFAL-DSG/pyfst