算法实现系列第五章.viterbi算法

package algorithm;

public class Viterbi {
/**
* 维特比算法(Viterbi algorithm)是一种动态规划算法。它用于寻找最有可能产生观测事件序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔可夫模型中。
术语“维特比路径”和“维特比算法”也被用于寻找观察结果最有可能解释相关的动态规划算法。例如在统计句法分析中动态规划算法可以被用于发现最可能的上下文无关的派生(解析)的字符串,有时被称为“维特比分析”。
* @param args
*/
public static void main(String[] args) {
// 用分词举例有如下结构.采用少词方式
// 0 中 中国 中国人
// 1 国 国人
// 2 人 人民
// 构建一个数组将如上结构放入数组中

Node begin = new Node("B", 0);
begin.score = 1 ;
Node end = new Node("END", 5);
Node[][] graph = new Node[6][0];
graph[0] = new Node[] { begin };
graph[1] = new Node[] { new Node("中", 1), new Node("中国", 1), new Node("中国人", 1) };
graph[2] = new Node[] { new Node("国", 2), new Node("国人", 2) };
graph[3] = new Node[] { new Node("人", 3), new Node("人民", 3) };
graph[4] = new Node[] { new Node("民", 4) };
graph[5] = new Node[] { end };

int to = 0;
Node node = null;

// viterbi寻找最优路径
for (int i = 0; i < graph.length - 1; i++) {
for (int j = 0; j < graph[i].length; j++) {
node = graph[i][j];
to = node.getTo();
for (int k = 0; k < graph[to].length; k++) {
graph[to][k].setFrom(node);
}
}
}

// 反向遍历寻找结果
node = graph[5][0];
while ((node = node.getFrom()) != null) {
System.out.println(node.getName());
}

}

static class Node {
private String name;
private Node from;
private int offe;
private Integer score;

public Node(String name, int offe) {
this.name = name;
this.offe = offe;
}

public Node(Node node, Node node2, Node node3) {
// TODO Auto-generated constructor stub
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Node getFrom() {
return from;
}

public void setFrom(Node from) {
if (this.score == null) {
this.score = from.score + 1;
this.from = from;
} else if (this.score > from.score + 1) {
this.score = from.score + 1;
this.from = from;
}
}

public int getTo() {
return this.offe + name.length();
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值