forked from habeanf/yap
-
Notifications
You must be signed in to change notification settings - Fork 21
/
genlemmas.go
180 lines (158 loc) · 4.94 KB
/
genlemmas.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
package app
import (
"yap/nlp/format/lattice"
"yap/nlp/format/raw"
// "yap/nlp/format/mapping"
"yap/nlp/parser/disambig"
nlp "yap/nlp/types"
"fmt"
"log"
"os"
"strings"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
func GenLemmasConfigOut() {
log.Println("Configuration")
log.Printf("Parameter Func:\t%v", paramFuncName)
log.Println()
log.Println("Data")
log.Printf("Raw Input:\t\t\t%s", inRawFile)
if !VerifyExists(tLatDis) {
return
}
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 GetLemmas(conf *disambig.MDConfig, pf nlp.MDParam) nlp.AmbMorphs {
return conf.Lattices.FindGoldAmbMorphs(conf.Mappings, pf)
}
func GetLemmasCorpus(goldSequences []*disambig.MDConfig, rawSents []nlp.BasicSentence, pf nlp.MDParam) {
f, _ := os.Create(outMap)
defer f.Close()
prefix := log.Prefix()
for i, goldSeq := range goldSequences {
log.SetPrefix(fmt.Sprintf("%v graph# %v ", prefix, i))
log.Println("At sent", i)
result := GetLemmas(goldSeq, pf)
rawSent := rawSents[i]
for _, ambLemma := range result {
fmt.Fprintf(f, "%v,%v,\"%s\",\"%s\",\"%s\"\n", i, ambLemma.Token, strings.Replace(rawSent.Joined("|"), "\"", "\\\"", -1), strings.Join(ambLemma.Lemmas, "|"), strings.Join(ambLemma.PrevGold, ";"))
}
}
log.SetPrefix(prefix)
}
func genLemmasInstance(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 genLemmasInstances(goldLats, ambLats []interface{}) []*disambig.MDConfig {
configs := make([]*disambig.MDConfig, 0, len(goldLats))
for i, goldMap := range goldLats {
ambLat := ambLats[i].(nlp.LatticeSentence)
result := genLemmasInstance(goldMap.(nlp.LatticeSentence), ambLat)
configs = append(configs, result)
}
return configs
}
func GenLemmas(cmd *commander.Command, args []string) error {
paramFunc, exists := nlp.MDParams[paramFuncName]
if !exists {
log.Fatalln("Param Func", paramFuncName, "does not exist")
}
REQUIRED_FLAGS := []string{"d", "l"}
VerifyFlags(cmd, REQUIRED_FLAGS)
// RegisterTypes()
GenLemmasConfigOut()
if allOut {
log.Println()
// start processing - setup enumerations
log.Println("Setup enumerations")
}
SetupMDEnum()
if allOut {
log.Println("Amb. Lat:\tReading ambiguous lattices from", tLatAmb)
}
lAmb, lAmbE := lattice.ReadFile(tLatAmb, limit)
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, limit)
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")
}
combined := genLemmasInstances(goldDisLat, goldAmbLat)
rawSents, err := raw.ReadFile(inRawFile, limit)
if err != nil {
panic(fmt.Sprintf("Failed reading raw file - %v", err))
}
if allOut {
log.Println("Read", len(rawSents), "raw sentences")
log.Println("Getting lemmas for", len(combined), "sentences")
}
GetLemmasCorpus(combined, rawSents, paramFunc)
return nil
}
func GenLemmasCmd() *commander.Command {
cmd := &commander.Command{
Run: GenLemmas,
UsageLine: "lemmas <file options> [arguments]",
Short: "gets ambiguous lemmas in the hebrew tb for gold paths",
Long: `
gets ambiguous lemmas in the hebrew tb for gold paths
$ ./yap lemmas -d <disamb. lat> -l <amb. lat> [-p <param func>] [options]
`,
Flag: *flag.NewFlagSet("lemmas", flag.ExitOnError),
}
cmd.Flag.StringVar(&tLatDis, "d", "", "Disambiguated Lattices File")
cmd.Flag.StringVar(&tLatAmb, "l", "", "Ambiguous Lattices File")
cmd.Flag.StringVar(&inRawFile, "r", "", "Input raw (tokenized) file")
cmd.Flag.StringVar(&outMap, "om", "", "Output Mapping File")
cmd.Flag.StringVar(¶mFuncName, "p", "Funcs_Main_POS_Both_Prop", "Param Func types: ["+nlp.AllParamFuncNames+"]")
cmd.Flag.IntVar(&limit, "limit", 0, "limit training set")
return cmd
}