fst-graph

这里就直接贴上代码了。

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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值