forked from habeanf/yap
/
validmagold.go
212 lines (185 loc) · 5.53 KB
/
validmagold.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
package app
import (
"yap/alg/perceptron"
"yap/alg/search"
"yap/alg/transition"
"yap/nlp/format/lattice"
"yap/nlp/parser/disambig"
nlp "yap/nlp/types"
"fmt"
"log"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
var (
transitionSystem transition.TransitionSystem
vmaParamFuncName string
)
func ValidMAGoldConfigOut(t transition.TransitionSystem) {
log.Println("Configuration")
log.Printf("Transition System:\t%s", t.Name())
log.Printf("Infuse Gold:\t\t%v", combineGold)
log.Printf("Parameter Func:\t%v", vmaParamFuncName)
log.Println()
log.Println("Data")
log.Printf("Disamb. lattice file:\t%s", tLatDis)
if !VerifyExists(tLatDis) {
return
}
log.Printf("Ambig. lattice file:\t%s", tLatAmb)
if !VerifyExists(tLatAmb) {
return
}
}
func ValidateInstance(decoded perceptron.DecodedInstance) string {
conf := &disambig.MDConfig{
ETokens: ETokens,
POP: POP,
}
extractor := &perceptron.EmptyFeatureExtractor{}
deterministic := &search.Deterministic{
TransFunc: transitionSystem,
FeatExtractor: extractor,
ReturnModelValue: false,
ReturnSequence: false,
ShowConsiderations: false,
Base: conf,
NoRecover: false,
}
deterministic.DecodeGold(decoded, nil)
return "ok"
}
func ValidateCorpus(goldSequences []perceptron.DecodedInstance) map[string]int {
retval := make(map[string]int, 4)
prefix := log.Prefix()
for i, goldSeq := range goldSequences {
log.SetPrefix(fmt.Sprintf("%v graph# %v ", prefix, i))
log.Println("At sent", i)
result := ValidateInstance(goldSeq)
curval, exists := retval[result]
if !exists {
curval = 0
}
retval[result] = curval + 1
}
log.SetPrefix(prefix)
return retval
}
func genInstance(goldLat, ambLat nlp.LatticeSentence) *disambig.MDConfig {
// generate morph. disambiguation (= mapping) and nodes
mappings := make([]*nlp.Mapping, len(goldLat))
for i, lat := range goldLat {
// log.Println("At lat", i)
lat.GenSpellouts()
lat.GenToken()
if len(lat.Spellouts) == 0 {
continue
}
mapping := &nlp.Mapping{
lat.Token,
lat.Spellouts[0],
}
mappings[i] = mapping
}
m := &disambig.MDConfig{
Mappings: mappings,
Lattices: ambLat,
}
return m
}
func genInstances(goldLats, ambLats []interface{}) []interface{} {
configs := make([]interface{}, 0, len(goldLats))
for i, goldMap := range goldLats {
ambLat := ambLats[i].(nlp.LatticeSentence)
result := genInstance(goldMap.(nlp.LatticeSentence), ambLat)
configs = append(configs, result)
}
return configs
}
func ValidMAGold(cmd *commander.Command, args []string) error {
paramFunc, exists := nlp.MDParams[vmaParamFuncName]
if !exists {
log.Fatalln("Param Func", vmaParamFuncName, "does not exist")
}
var mdTrans transition.TransitionSystem
mdTrans = &disambig.MDTrans{
ParamFunc: paramFunc,
}
// arcSystem := &morph.Idle{morphArcSystem, IDLE}
transitionSystem = transition.TransitionSystem(mdTrans)
REQUIRED_FLAGS := []string{"d", "l"}
VerifyFlags(cmd, REQUIRED_FLAGS)
// RegisterTypes()
ValidMAGoldConfigOut(transitionSystem)
if allOut {
log.Println()
// start processing - setup enumerations
log.Println("Setup enumerations")
}
SetupMDEnum()
mdTrans.(*disambig.MDTrans).POP = POP
mdTrans.(*disambig.MDTrans).Transitions = ETrans
mdTrans.AddDefaultOracle()
if allOut {
log.Println("Amb. Lat:\tReading ambiguous lattices from", tLatAmb)
}
lAmb, lAmbE := lattice.ReadFile(tLatAmb, 0)
if lAmbE != nil {
log.Println(lAmbE)
return lAmbE
}
if allOut {
log.Println("Amb. Lat:\tRead", len(lAmb), "ambiguous lattices")
log.Println("Amb. Lat:\tConverting lattice format to internal structure")
}
goldAmbLat := lattice.Lattice2SentenceCorpus(lAmb, EWord, EPOS, EWPOS, EMorphProp, EMHost, EMSuffix)
// goldAmbLat = Limit(goldAmbLat, 1)
if allOut {
log.Println("Dis. Lat.:\tReading disambiguated lattices from", tLatDis)
}
lDis, lDisE := lattice.ReadFile(tLatDis, 0)
if lDisE != nil {
log.Println(lDisE)
return lDisE
}
if allOut {
log.Println("Dis. Lat.:\tRead", len(lDis), "disambiguated lattices")
log.Println("Dis. Lat.:\tConverting lattice format to internal structure")
}
goldDisLat := lattice.Lattice2SentenceCorpus(lDis, EWord, EPOS, EWPOS, EMorphProp, EMHost, EMSuffix)
// goldDisLat = Limit(goldDisLat, 1)
if allOut {
log.Println("Combining train files into gold morph graphs with original lattices")
}
var combined []interface{}
if combineGold {
combined, _, _, _ = CombineLatticesCorpus(goldDisLat, goldAmbLat)
} else {
combined = genInstances(goldDisLat, goldAmbLat)
}
goldSequences := TrainingSequences(combined, GetMDConfigAsLattices, GetMDConfigAsMappings)
if allOut {
log.Println("Validating corpus of", len(goldSequences), "lattices")
}
stats := ValidateCorpus(goldSequences)
log.Println("Results", stats)
return nil
}
func ValidateMAGoldCmd() *commander.Command {
cmd := &commander.Command{
Run: ValidMAGold,
UsageLine: "vma <file options> [arguments]",
Short: "validates gold paths in given lattices",
Long: `
validates gold paths in given lattices
$ ./yap vma -d <disamb. lat> -l <amb. lat> [-p <param func>] [options]
`,
Flag: *flag.NewFlagSet("vma", flag.ExitOnError),
}
cmd.Flag.StringVar(&tLatDis, "d", "", "Disambiguated Lattices File")
cmd.Flag.StringVar(&tLatAmb, "l", "", "Ambiguous Lattices File")
cmd.Flag.StringVar(&outMap, "om", "", "Output Mapping File")
cmd.Flag.BoolVar(&combineGold, "infuse", false, "Infuse gold morphs into lattices")
cmd.Flag.StringVar(&vmaParamFuncName, "p", "Funcs_Main_POS_Both_Prop_WLemma", "Param Func types: ["+nlp.AllParamFuncNames+"]")
return cmd
}