mirror of
https://github.com/fumiama/jieba.git
synced 2026-06-05 00:32:51 +08:00
make some public variable/function to private
This commit is contained in:
@@ -23,30 +23,30 @@ func init() {
|
||||
probStart['S'] = -1.4652633398537678
|
||||
}
|
||||
|
||||
type Viterbi struct {
|
||||
type probState struct {
|
||||
prob float64
|
||||
state byte
|
||||
}
|
||||
|
||||
func (v Viterbi) String() string {
|
||||
return fmt.Sprintf("(%f, %x)", v.prob, v.state)
|
||||
func (p probState) String() string {
|
||||
return fmt.Sprintf("(%f, %x)", p.prob, p.state)
|
||||
}
|
||||
|
||||
type Viterbis []*Viterbi
|
||||
type probStates []*probState
|
||||
|
||||
func (vs Viterbis) Len() int {
|
||||
return len(vs)
|
||||
func (ps probStates) Len() int {
|
||||
return len(ps)
|
||||
}
|
||||
|
||||
func (vs Viterbis) Less(i, j int) bool {
|
||||
if vs[i].prob == vs[j].prob {
|
||||
return vs[i].state < vs[j].state
|
||||
func (ps probStates) Less(i, j int) bool {
|
||||
if ps[i].prob == ps[j].prob {
|
||||
return ps[i].state < ps[j].state
|
||||
}
|
||||
return vs[i].prob < vs[j].prob
|
||||
return ps[i].prob < ps[j].prob
|
||||
}
|
||||
|
||||
func (vs Viterbis) Swap(i, j int) {
|
||||
vs[i], vs[j] = vs[j], vs[i]
|
||||
func (ps probStates) Swap(i, j int) {
|
||||
ps[i], ps[j] = ps[j], ps[i]
|
||||
}
|
||||
|
||||
func viterbi(obs []rune, states []byte) (float64, []byte) {
|
||||
@@ -66,7 +66,7 @@ func viterbi(obs []rune, states []byte) (float64, []byte) {
|
||||
newPath := make(map[byte][]byte)
|
||||
V[t] = make(map[byte]float64)
|
||||
for _, y := range states {
|
||||
vs0 := make(Viterbis, 0)
|
||||
ps0 := make(probStates, 0)
|
||||
var em_p float64
|
||||
if val, ok := probEmit[y][obs[t]]; ok {
|
||||
em_p = val
|
||||
@@ -81,21 +81,21 @@ func viterbi(obs []rune, states []byte) (float64, []byte) {
|
||||
transP = MinFloat
|
||||
}
|
||||
prob0 := V[t-1][y0] + transP + em_p
|
||||
vs0 = append(vs0, &Viterbi{prob: prob0, state: y0})
|
||||
ps0 = append(ps0, &probState{prob: prob0, state: y0})
|
||||
}
|
||||
sort.Sort(sort.Reverse(vs0))
|
||||
V[t][y] = vs0[0].prob
|
||||
pp := make([]byte, len(path[vs0[0].state]))
|
||||
copy(pp, path[vs0[0].state])
|
||||
sort.Sort(sort.Reverse(ps0))
|
||||
V[t][y] = ps0[0].prob
|
||||
pp := make([]byte, len(path[ps0[0].state]))
|
||||
copy(pp, path[ps0[0].state])
|
||||
newPath[y] = append(pp, y)
|
||||
}
|
||||
path = newPath
|
||||
}
|
||||
vs := make(Viterbis, 0)
|
||||
ps := make(probStates, 0)
|
||||
for _, y := range []byte{'E', 'S'} {
|
||||
vs = append(vs, &Viterbi{V[len(obs)-1][y], y})
|
||||
ps = append(ps, &probState{V[len(obs)-1][y], y})
|
||||
}
|
||||
sort.Sort(sort.Reverse(vs))
|
||||
v := vs[0]
|
||||
sort.Sort(sort.Reverse(ps))
|
||||
v := ps[0]
|
||||
return v.prob, path[v.state]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user