forked from habeanf/yap
-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.go
214 lines (183 loc) · 5.37 KB
/
config.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package joint
import (
. "yap/alg"
"yap/alg/search"
"yap/alg/transition"
dep "yap/nlp/parser/dependency/transition"
"yap/nlp/parser/disambig"
nlp "yap/nlp/types"
"yap/util"
"fmt"
// "log"
)
type JointConfig struct {
dep.SimpleConfiguration
disambig.MDConfig
InternalPrevious *JointConfig
Last transition.Transition
ETrans *util.EnumSet
MDTrans transition.Transition
lastAssignment uint16
}
var (
_ transition.Configuration = &JointConfig{}
_ search.Aligned = &JointConfig{}
_ dep.DependencyConfiguration = &JointConfig{}
_ nlp.DependencyGraph = &JointConfig{}
_ nlp.MorphDependencyGraph = &JointConfig{}
)
func (c *JointConfig) Init(abstractLattice interface{}) {
// initialize MDConfig as usual (doesn't know the difference)
c.MDConfig.Init(abstractLattice)
// initialize SimpleConfiguration explicitly
// we don't know # of morphemes in advance, only an estimate
estMorphemes := len(c.MDConfig.Lattices) * 2
c.SimpleConfiguration.InternalStack = NewStackArray(estMorphemes)
c.SimpleConfiguration.InternalQueue = NewQueueSlice(estMorphemes)
c.SimpleConfiguration.InternalArcs = dep.NewArcSetSimple(estMorphemes)
c.SimpleConfiguration.NumHeadStack = 0
c.SimpleConfiguration.Nodes = make([]*dep.ArcCachedDepNode, 0, estMorphemes)
// note we don't initialize the queue at all, morph. disambig. will enqueue
c.Last = transition.ConstTransition(0)
c.InternalPrevious = nil
}
func (c *JointConfig) State() byte {
return 'J'
}
func (c *JointConfig) Terminal() bool {
return c.MDConfig.Terminal() && c.SimpleConfiguration.Terminal()
}
func (c *JointConfig) Copy() transition.Configuration {
newConf := new(JointConfig)
c.CopyTo(newConf)
return newConf
}
func (c *JointConfig) CopyTo(target transition.Configuration) {
newConf, ok := target.(*JointConfig)
if !ok {
panic("Can't copy into non *JointConfig")
}
newConf.Last = c.Last
newConf.InternalPrevious = c
newConf.ETrans = c.ETrans
newConf.MDTrans = c.MDTrans
c.MDConfig.CopyTo(&newConf.MDConfig)
c.SimpleConfiguration.CopyTo(&newConf.SimpleConfiguration)
}
func (c *JointConfig) GetSequence() transition.ConfigurationSequence {
if c.Mappings == nil || c.Arcs() == nil {
return make(transition.ConfigurationSequence, 0)
}
retval := make(transition.ConfigurationSequence, 0, len(c.Morphemes)+c.Arcs().Size())
currentConf := c
for currentConf != nil {
retval = append(retval, currentConf)
currentConf = currentConf.InternalPrevious
}
return retval
}
func (c *JointConfig) SetLastTransition(t transition.Transition) {
c.Last = t
}
func (c *JointConfig) GetLastTransition() transition.Transition {
return c.Last
}
func (c *JointConfig) String() string {
if c.Mappings == nil {
return fmt.Sprintf("\t=>\t([],\t[],\t[],\t,\t)")
}
var trans string
if c.Last.Value() < 0 {
trans = ""
} else {
trans = c.SimpleConfiguration.ETrans.ValueOf(c.Last.Value()).(string)
}
mapLen := len(c.Mappings)
if mapLen > 0 {
return fmt.Sprintf("%s\t=>\t([%s],\t[%s],\t[%s],\t[%s],\t[%v])",
trans,
c.StringStack(),
c.StringQueue(),
c.StringLatticeQueue(),
c.StringArcs(),
c.Mappings[mapLen-1])
} else {
return fmt.Sprintf("%s\t=>\t([],\t[],\t[%s],\t,\t)", trans, c.StringLatticeQueue())
}
}
func (c *JointConfig) Equal(otherEq util.Equaler) bool {
if (otherEq == nil && c != nil) || (c == nil && otherEq != nil) {
return false
}
switch other := otherEq.(type) {
case *JointConfig:
if (other == nil && c != nil) || (c == nil && other != nil) {
return false
}
if !other.Last.Equal(c.Last) {
return false
}
if c.InternalPrevious == nil && other.InternalPrevious == nil {
return true
}
if c.InternalPrevious != nil && other.InternalPrevious != nil {
return c.InternalPrevious.Equal(other.InternalPrevious)
} else {
return false
}
}
panic("Can't equal to non-Joint config")
}
func (c *JointConfig) Address(location []byte, offset int) (nodeID int, exists bool, isGenerator bool) {
if location[0] == 'M' || location[0] == 'L' {
return c.MDConfig.Address(location, offset)
} else {
return c.SimpleConfiguration.Address(location, offset)
}
}
func (c *JointConfig) GenerateAddresses(nodeID int, location []byte) (nodeIDs []int) {
return c.SimpleConfiguration.GenerateAddresses(nodeID, location)
}
func (c *JointConfig) Attribute(source byte, nodeID int, attribute []byte, transitions []int) (interface{}, bool, bool) {
if source == 'M' || source == 'L' {
return c.MDConfig.Attribute(source, nodeID, attribute, transitions)
} else {
return c.SimpleConfiguration.Attribute(source, nodeID, attribute, transitions)
}
}
func (c *JointConfig) Previous() transition.Configuration {
return c.InternalPrevious
}
func (c *JointConfig) SetPrevious(prev transition.Configuration) {
c.InternalPrevious = prev.(*JointConfig)
}
func (c *JointConfig) Clear() {
c.SimpleConfiguration.Clear()
c.MDConfig.Clear()
c.InternalPrevious = nil
}
func (c *JointConfig) Alignment() int {
return c.MDConfig.Alignment()
}
func (c *JointConfig) GetMappings() nlp.Mappings {
return c.Mappings
}
func (c *JointConfig) GetMorpheme(i int) *nlp.EMorpheme {
return c.Morphemes[i]
}
func (c *JointConfig) Len() int {
if c == nil {
return 0
}
if c.Previous() != nil {
return 1 + c.Previous().Len()
} else {
return 1
}
}
func (c *JointConfig) Assignment() uint16 {
return c.lastAssignment
}
func (c *JointConfig) Assign(to uint16) {
c.lastAssignment = to
}