-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamma.go
145 lines (130 loc) · 3.06 KB
/
gamma.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package hmm
import(
"fmt"
"bytes"
"log"
"github.com/johnmcconnell/gologspace"
)
type Gamma struct {
tags []Tag
sequence []string
s gologspace.Space
forward *Forward
backward *Backward
}
// NewViterb ...
func NewGamma(tags []Tag, sequence []string, s gologspace.Space, i *Initial, t *Transition, e *Emission) *Gamma {
b := NewBackward(tags, sequence, s, i, t, e)
b.FillTrellis()
f := NewForward(tags, sequence, s, i, t, e)
f.FillTrellis()
g := Gamma{tags, sequence, s, f, b}
return &g
}
// String ...
func (g *Gamma) String() string {
buffer := bytes.NewBufferString(fmt.Sprintf("Gamma: '%s'\n", g.sequence))
for _, tag := range g.tags {
buffer.WriteString(g.RowString(tag))
}
buffer.WriteString(g.SumRowString())
return fmt.Sprintf("%s\n", buffer.String())
}
// SumRowString ...
func (g *Gamma) SumRowString() string {
buffer := bytes.NewBufferString("|Sum: ' '|")
for i, _ := range g.sequence {
r := g.SumColumn(i)
buffer.WriteString(r.String())
}
return buffer.String()
}
// RowString ...
func (g *Gamma) RowString(tag Tag) string {
buffer := bytes.NewBufferString(fmt.Sprintf("|Tag: '%v'|", tag))
for i, _ := range g.sequence {
buffer.WriteString(g.R(tag, i).FormatString(g.s))
}
buffer.WriteString(fmt.Sprintf("\n"))
return buffer.String()
}
// ComputeProb ...
func (g *Gamma) CompP(pF, pB, pS float64) float64 {
s := g.s
return s.Div(s.Mul(pF, pB), pS)
}
// ComputeProb ...
func (g *Gamma) ComputeP(tag Tag, i int) float64 {
rF := g.forward.R(tag, i)
pF := rF.Prob
rB := g.backward.R(tag, i)
pB := rB.Prob
pS := g.CompColumnSum(i)
return g.CompP(pF, pB, pS)
}
func(g *Gamma) CompTransP(pF, pT, pE, pB float64) float64 {
s := g.s
a := s.Mul(s.Mul(pF, pT), s.Mul(pE, pB))
if (a > 0) {
log.Printf("a[%v] = pF[%v] + pT[%v] + pE[%v] + pB[%v]", a, pF, pT, pE, pB)
}
return a
}
// ComputeTransitionProb ...
func(g *Gamma) ComputeTransP(tag1, tag2 Tag, i int) float64 {
t := *g.forward.t
e := *g.forward.e
value := g.sequence[i + 1]
pF := g.forward.R(tag1, i).Prob
pT := t[tag1][tag2]
pE := e[tag2][value]
pB := g.backward.R(tag2, i + 1).Prob
// p := g.CompColumnSum(i)
return g.CompTransP(pF, pT, pE, pB)
}
// ComputeColumnSum ...
func (g *Gamma) CompColumnSum(i int) float64 {
pSum := 0.0
for _, tag := range g.tags {
rF := g.forward.R(tag, i)
rB := g.backward.R(tag, i)
a := g.s.Mul(rF.Prob, rB.Prob)
if pSum == 0.0 {
pSum = a
} else {
pSum = g.s.Add(a, pSum)
}
}
return pSum
}
// InitialMass ...
func(g *Gamma) InitialMass(tag Tag) float64 {
return g.ComputeP(tag, 0)
}
// TransitionMass ...
func(g *Gamma) TransMass(tag1, tag2 Tag) float64 {
pSum := 0.0
limit := len(g.sequence) - 1
for i, _ := range g.sequence {
if (i < limit) {
p := g.ComputeTransP(tag1, tag2, i)
if pSum == 0.0 {
pSum = p
} else {
pSum = g.s.Add(pSum, p)
}
}
}
// P(tag1, tag2 | w) / P(w)
return pSum
}
// SumColumn
func (g *Gamma) SumColumn(index int) *Result {
p := g.CompColumnSum(index)
return &Result{"e", p}
}
// Result ...
func (g *Gamma) R(tag Tag, index int) *Result {
p := g.ComputeP(tag, index)
return &Result{"e", p}
}