mirror of
https://github.com/fumiama/jieba.git
synced 2026-06-30 09:00:30 +08:00
removed sorts to slightly improve performance
This commit is contained in:
@@ -75,16 +75,17 @@ func viterbi(obs []rune) []string {
|
|||||||
mem_path[t] = make(map[string]string) // TODO: value needed or not?
|
mem_path[t] = make(map[string]string) // TODO: value needed or not?
|
||||||
V[t] = make(map[string]float64)
|
V[t] = make(map[string]float64)
|
||||||
for _, y := range obs_states {
|
for _, y := range obs_states {
|
||||||
pss := make(probStates, 0)
|
var max, ps probState
|
||||||
for _, y0 := range prev_states {
|
for i, y0 := range prev_states {
|
||||||
ps := probState{
|
ps = probState{
|
||||||
prob: V[t-1][y0] + probTrans[y0].Get(y) + probEmit[y].get(obs[t]),
|
prob: V[t-1][y0] + probTrans[y0].Get(y) + probEmit[y].get(obs[t]),
|
||||||
state: y0}
|
state: y0}
|
||||||
pss = append(pss, ps)
|
if i == 0 || ps.prob > max.prob || (ps.prob == max.prob && ps.state > max.state) {
|
||||||
|
max = ps
|
||||||
}
|
}
|
||||||
sort.Sort(sort.Reverse(pss))
|
}
|
||||||
V[t][y] = pss[0].prob
|
V[t][y] = max.prob
|
||||||
mem_path[t][y] = pss[0].state
|
mem_path[t][y] = max.state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last := make(probStates, 0)
|
last := make(probStates, 0)
|
||||||
|
|||||||
@@ -40,3 +40,10 @@ func TestViterbi(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkViterbi(b *testing.B) {
|
||||||
|
ss := "李小福是创新办主任也是云计算方面的专家;"
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
viterbi([]rune(ss))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user