fst-graph
程序员文章站
2022-07-13 14:41:41
...
这里就直接贴上代码了。
digraph G {
node [shape = circle]
0 -> 1 [ label = sil ]
0 -> 2 [ label = w ]
1 -> 1 [ label = sil ]
1 -> 2 [ label = w ]
2 -> 2 [ label = w ]
2 -> 3 [ label = u3 ]
3 -> 3 [ label = u3 ]
3 -> 4 [ label = sil ]
3 -> 5 [ label = h ]
4 -> 4 [ label = sil ]
4 -> 5 [ label = h ]
5 -> 5 [ label = h ]
5 -> 6 [ label = an4 ]
6 -> 6 [ label = an4 ]
6 -> 7 [ label = sil ]
6 -> 8 [ label = j ]
7 -> 7 [ label = sil ]
7 -> 8 [ label = j ]
8 -> 8 [ label = j ]
8 -> 9 [ label = ia1 ]
9 -> 9 [ label = ia1 ]
9 -> 10 [ label = sil ]
9 -> 11 [ label = y ]
10 -> 10 [ label = sil ]
10 -> 11 [ label = y ]
11 -> 11 [ label = y ]
11 -> 12 [ label = iu2 ]
11 -> 13 [ label = ou1 ]
11 -> 14 [ label = ou2 ]
12 -> 12 [ label = iu2 ]
12 -> 15 [ label = sil ]
12 -> 16 [ label = <eps> ]
13 -> 13 [ label = ou1 ]
13 -> 15 [ label = sil ]
13 -> 16 [ label = <eps> ]
14 -> 14 [ label = ou2 ]
14 -> 15 [ label = sil ]
14 -> 16 [ label = <eps> ]
15 -> 15 [ label = sil ]
15 -> 16 [ label = <eps> ]
16 [shape = doublecircle]
rankdir = LR
}
这个是构建图的时候
## 武 汉 加 油
dot -Tpng test.dot -o test.png
fst图。
上图的打印输出,是由图的宽度遍历打印而来。
std::string DotGraph(const std::string& filename,
bool do_write) {
std::stringstream stream_line;
stream_line << "digraph G {\n";
stream_line << " node [shape = circle]\n";
// bfs here
for (int i = 0; i < nodes_.size(); i++) {
nodes_[i]->trans_tag = 0;
}
std::queue<Node*> que_node;
que_node.push(graph_head_);
graph_head_->trans_tag = 1;
int32 num_arc_count = 0;
while (!que_node.empty()) {
Node* pnode = que_node.front();
que_node.pop();
if (pnode == final_node_) {
stream_line << " " << pnode->node_id << " [shape = doublecircle]\n";
}
for (int i = 0; i < pnode->next_arcs.size(); i++) {
Arc* parc = pnode->next_arcs[i];
Node* ptmp = parc->dest;
stream_line << " " << pnode->node_id << " -> " << ptmp->node_id;
std::string label_str = " [ label = ";
label_str += GetLabel(parc->label);
label_str += " ]";
stream_line << label_str << "\n";
if (parc->dest->trans_tag == 0) {
que_node.push(parc->dest);
parc->dest->trans_tag = 1;
}
}
}
stream_line << " rankdir = LR\n";
stream_line << "}\n";
if (do_write) {
std::ofstream ofs;
ofs.open(filename.c_str());
ofs << stream_line.str();
ofs.close();
}
return stream_line.str();
}
上一篇: wav格式
推荐阅读