如何在Python中解析DOT文件 [英] How to parse a DOT file in Python

查看:1460
本文介绍了如何在Python中解析DOT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以DOT文件形式保存的传感器.我可以使用gvedit看到图形的图形表示形式,但是如果我想将DOT文件转换为可执行的转换器,该怎么办,以便可以测试转换器并查看它接受什么字符串以及不接受什么字符串.

I have a transducer saved in the form of a DOT file. I can see a graphical representation of the graphs using gvedit, but what if I want to convert the DOT file to an executable transducer, so that I can test the transducer and see what strings it accepts and what it doesn't.

在我在Openfst,Graphviz及其Python扩展中看到的大多数工具中,DOT文件仅用于创建图形表示,但是如果我想解析该文件以获取可在其中测试的交互式程序,该怎么办?换能器上的弦?

In most of the tools I have seen in Openfst, Graphviz, and their Python extensions, DOT files are only used to create a graphical representation, but what if I want to parse the file to get an interactive program where I can test the strings against the transducer?

那里有可以执行此任务的库吗?还是我应该从头开始编写它?

Are there any libraries out there that would do the task or should I just write it from scratch?

正如我所说,DOT文件与我设计的模拟英语形态的传感器有关.这是一个巨大的文件,但为了让您大致了解它的样子,我提供了一个示例.假设我想创建一个转换器,该转换器可以对英语在名词和复数方面的行为进行建模.我的词典只有三个词(书,男孩,女孩).在这种情况下,我的传感器看起来像这样:

As I said, the DOT file is related to a transducer I have designed that simulates morphology of English. It is a huge file, but just to give you an idea of how it is like, I provide a sample. Let's say I want to create a transducer that would model the behavior of English with regards to Nouns and in terms of plurality. My lexicon consists of only three words (book, boy, girl). My transducer in this case would look something like this:

直接从此DOT文件构造而成:

which is directly constructed from this DOT file:

digraph A {
rankdir = LR;
node [shape=circle,style=filled] 0
node [shape=circle,style=filled] 1
node [shape=circle,style=filled] 2
node [shape=circle,style=filled] 3
node [shape=circle,style=filled] 4
node [shape=circle,style=filled] 5
node [shape=circle,style=filled] 6
node [shape=circle,style=filled] 7
node [shape=circle,style=filled] 8
node [shape=circle,style=filled] 9
node [shape=doublecircle,style=filled] 10
0 -> 4 [label="g "];
0 -> 1 [label="b "];
1 -> 2 [label="o "];
2 -> 7 [label="y "];
2 -> 3 [label="o "];
3 -> 7 [label="k "];
4 -> 5 [label="i "];
5 -> 6 [label="r "];
6 -> 7 [label="l "];
7 -> 9 [label="<+N:s> "];
7 -> 8 [label="<+N:0> "];
8 -> 10 [label="<+Sg:0> "];
9 -> 10 [label="<+Pl:0> "];
}

现在针对该单词测试此换能器意味着,如果用book+Pl喂食它,它应该吐回books,反之亦然.我想看看如何将点文件转换为允许进行此类分析和测试的格式.

Now testing this transducer against the words means that if you feed it with book+Pl it should spit back books and vice versa. I'd like to see how it is possible to turn the dot file into a format that would allow such analysis and testing.

推荐答案

您可以首先使用 https://code.google.com/p/pydot/.从那里开始,应该相对简单地编写代码,以根据输入字符串遍历内存中的图.

You could start by loading the file using https://code.google.com/p/pydot/ . From there it should be relatively simply to write the code to traverse the in-memory graph according to an input string.

这篇关于如何在Python中解析DOT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆