算法介绍见博客:http://www.cnblogs.com/tornadomeet/archive/2012/03/24/2415889.html,实现结果与他一样,下面是我的实现细节:
一 程序整体布局
分为loadmodel、viterbi搜索、print输出结果三个部分。主体程序如下:
#include "Viterbi.h"
#include <fstream>
#include <string>
int main()
{
Viterbi viterbi_1;
viterbi_1.loadModel();
viterbi_1.viterbi();
viterbi_1.print();
return 0;
}
二 头文件的定义
头文件中主要用来确定需要用那些函数和数据,这里面使用什么样的数据结构尤其需要考虑,我的实现中
二维数组: vector < vector<type> > , 第一维表示帧等,第二维表示状态数;
观测序列与发射矩阵中第二维的对应: 用map容器;
最后的结果反向遍历得到,然后又正向输出结果: 用stack容器适配器;
具体代码如下:
/*
* File: Viterbi.h
* Author: junan
*
* Created on December 3, 2014, 4:16 PM
*/
#include <iostream>
#include <vector>
#include <fstream>
#include <map>
#include <stack>
#define DBL_MIN 2.2250738585072014e-308
#ifndef VITERBI_H
#define VITERBI_H
#endif /* VITERBI_H */
using namespace std;
class Viterbi{
public:
// default construct function
Viterbi(): max_pro(0), forward_path(0){
initProbF = "/home/junan/Documents/C++/viterbi/ArrayIniprob_3.txt";
transF = "/home/junan/Documents/C++/viterbi/ArrayTranmat_