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