forked from habeanf/yap
/
ma.go
185 lines (169 loc) · 5.1 KB
/
ma.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
package app
import (
"os"
"yap/nlp/format/conllu"
"yap/nlp/format/lattice"
"yap/nlp/format/raw"
"yap/nlp/parser/ma"
nlp "yap/nlp/types"
// "yap/util"
"fmt"
"log"
// "os"
"strings"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
var (
dictFile, inRawFile, outLatticeFile string
maxOOVMSRPerPOS int = 10
oovFile string
dopeOOV bool
outFormat string
)
func MAConfigOut() {
log.Println("Configuration")
log.Printf("MA Dict:\t\t%s", dictFile)
log.Printf("Limit:\t\t%v", limit)
log.Printf("Max OOV Msrs/POS:\t%v", maxOOVMSRPerPOS)
log.Printf("Dope:\t\t%v", dopeOOV)
log.Println()
if useConllU {
log.Printf("CoNLL-U Input:\t%s", conlluFile)
} else {
log.Printf("Raw Input:\t\t%s", inRawFile)
}
log.Printf("Output:\t\t%s", outLatticeFile)
log.Printf("Output Format:\t%v", outFormat)
log.Println()
}
func MA(cmd *commander.Command, args []string) error {
useConllU = len(conlluFile) > 0
var REQUIRED_FLAGS []string
if useConllU {
REQUIRED_FLAGS = []string{"dict", "conllu", "out"}
} else {
REQUIRED_FLAGS = []string{"dict", "raw", "out"}
}
VerifyFlags(cmd, REQUIRED_FLAGS)
MAConfigOut()
log.Println("Reading Morphological Analyzer Dictionary")
maData := new(ma.MADict)
if err := maData.ReadFile(dictFile); err != nil {
panic(fmt.Sprintf("Failed reading MA dict file - %v", err))
}
log.Println("OOV POSs:", strings.Join(maData.TopPOS, ", "))
maData.ComputeOOVMSRs(maxOOVMSRPerPOS)
log.Println()
var (
sents []nlp.BasicSentence
oovVectors []interface{}
rawOOV interface{}
err error
)
if useConllU {
conllSents, _, err := conllu.ReadFile(conlluFile, limit)
if err != nil {
panic(fmt.Sprintf("Failed reading CoNLL-U file - %v", err))
}
sents = make([]nlp.BasicSentence, len(conllSents))
for i, sent := range conllSents {
newSent := make([]nlp.Token, len(sent.Tokens))
for j, token := range sent.Tokens {
newSent[j] = nlp.Token(token)
}
sents[i] = newSent
}
} else {
sents, err = raw.ReadFile(inRawFile, limit)
if err != nil {
panic(fmt.Sprintf("Failed reading raw file - %v", err))
}
}
log.Println("Running Morphological Analysis")
lattices := make([]nlp.LatticeSentence, len(sents))
stats := new(ma.AnalyzeStats)
stats.Init()
maData.Init()
maData.Stats = stats
maData.Dope = dopeOOV
if len(oovFile) > 0 {
oovVectors = make([]interface{}, len(sents))
}
var (
outFile *os.File
streamOut bool
latticesWritten int
outFileError error
)
if outFormat == "ud" && !outJSON {
log.Println("Using streaming analysis and output")
// horrible hack for now :(
streamOut = true
lattices = make([]nlp.LatticeSentence, 1)
outFile, outFileError = os.Create(outLatticeFile)
if outFileError != nil {
return outFileError
}
defer outFile.Close()
}
for i, sent := range sents {
if streamOut {
lattices[0], rawOOV = maData.Analyze(sent.Tokens())
output := lattice.Sentence2LatticeCorpus(lattices, nil)
lattice.UDWrite(outFile, output)
latticesWritten += 1
} else {
lattices[i], rawOOV = maData.Analyze(sent.Tokens())
if oovVectors != nil {
oovVectors[i] = rawOOV
}
}
}
log.Println("Analyzed", stats.TotalTokens, "occurences of", len(stats.UniqTokens), "unique tokens")
log.Println("Encountered", stats.OOVTokens, "occurences of", len(stats.UniqOOVTokens), "unknown tokens")
if !streamOut {
output := lattice.Sentence2LatticeCorpus(lattices, nil)
if outFormat == "ud" {
if !outJSON {
// lattice.WriteUDJSONFile(outLatticeFile, output)
// } else {
lattice.WriteUDFile(outLatticeFile, output)
}
} else if outFormat == "spmrl" {
lattice.WriteFile(outLatticeFile, output)
} else {
panic(fmt.Sprintf("Unknown lattice output format - %v", outFormat))
}
if oovVectors != nil {
raw.WriteFile(oovFile, oovVectors)
}
log.Println("Wrote", len(output), "lattices")
} else {
log.Println("Wrote", latticesWritten, "lattices")
}
return nil
}
func MACmd() *commander.Command {
cmd := &commander.Command{
Run: MA,
UsageLine: "ma <file options> [arguments]",
Short: "run data-driven morphological analyzer on raw input",
Long: `
run data-driven morphological analyzer on raw input
$ ./yap ma -dict <dict file> -raw <raw file> -out <output file> [options]
`,
Flag: *flag.NewFlagSet("ma", flag.ExitOnError),
}
cmd.Flag.StringVar(&dictFile, "dict", "", "Dictionary for morphological analyzer")
cmd.Flag.StringVar(&inRawFile, "raw", "", "Input raw (tokenized) file")
cmd.Flag.StringVar(&conlluFile, "conllu", "", "CoNLL-U-format input file")
cmd.Flag.StringVar(&outLatticeFile, "out", "", "Output lattice file")
cmd.Flag.StringVar(&outFormat, "format", "spmrl", "Output lattice format [spmrl|ud]")
cmd.Flag.StringVar(&oovFile, "oov", "", "OOV File")
cmd.Flag.IntVar(&maxOOVMSRPerPOS, "maxmsrperpos", 10, "For OOV tokens, max MSRs per POS to add")
cmd.Flag.BoolVar(&dopeOOV, "dope", false, "Dope potential OOV tokens")
cmd.Flag.IntVar(&limit, "limit", 0, "limit training set")
cmd.Flag.BoolVar(&outJSON, "json", false, "Output using JSON")
return cmd
}