-
Notifications
You must be signed in to change notification settings - Fork 1
/
words.go
393 lines (357 loc) · 7.19 KB
/
words.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"path"
"regexp"
"strconv"
"strings"
)
var (
spaceRE = regexp.MustCompile("[^ \t]+")
commaRE = regexp.MustCompile("[^ \t,]+")
)
type affix interface {
expand(string)
String() string
}
type prefix struct {
strip string
affix string
cond string
re *regexp.Regexp
morph []string
}
type suffix struct {
strip string
affix string
cond string
re *regexp.Regexp
morph []string
}
type rule struct {
name string
cross bool
ln int
suffix bool
need bool
affix []affix
}
var rules map[string]*rule
var flagv = flag.Bool("v", false, "print verbose output")
var verbose bool
var (
numflags bool
flaglen = 1
needaffix string // tags stems in a dictionary
)
type affixReader struct {
r *bufio.Reader
ln int
}
func (a *prefix)String() string {
s := fmt.Sprintf(" strip:%v affix:%v cond:%v", a.strip, a.affix, a.cond)
if a.morph != nil {
return s + " morph:" + fmt.Sprintf("%v", a.morph)
}
return s
}
func (a *suffix)String() string {
s := fmt.Sprintf(" strip:%v affix:%v cond:%v", a.strip, a.affix, a.cond)
if a.morph != nil {
return s + " morph:" + fmt.Sprintf("%v", a.morph)
}
return s
}
func (r *rule)String() string {
s := fmt.Sprintf("name:%v cross:%v ln:%v suffix:\n", r.name, r.cross, r.ln)
for _,a := range r.affix {
s = s + fmt.Sprintf("%v\n", a)
}
return s
}
func (a *affixReader) getRule() []string {
for {
line, err := a.r.ReadString('\n')
if err == io.EOF {
return nil
}
a.ln++
arg := spaceRE.FindAllString(strings.TrimSpace(line), -1)
if arg == nil || len(arg) == 0 || arg[0][0] == '#' {
continue
}
//fmt.Fprintf(os.Stderr, "%d '%s'\n", len(arg), arg[0])
switch arg[0] {
default:
fmt.Fprintf(os.Stderr, "%d: invalid option: %s\n", a.ln, arg[0])
case "FLAG":
if len(arg) == 1 {
fmt.Fprintf(os.Stderr, "%d: FLAG needs an argument\n", a.ln)
continue
}
switch arg[1] {
case "num":
numflags = true
case "long":
flaglen = 2
default:
fmt.Fprintf(os.Stderr, "%d: invalid FLAG value: %s\n", a.ln, arg[1])
}
case "PFX", "SFX":
return arg
case "SET":
if len(arg) == 1 || arg[1] != "UTF-8" {
fmt.Fprintf(os.Stderr, "%d: only UTF-8 can be set\n", a.ln)
}
case "TRY": // ignore for now
case "NEEDAFFIX":
if len(arg) < 2 {
fmt.Fprintf(os.Stderr, "%d: needs a flag arg\n", a.ln)
continue
}
if needaffix != "" {
fmt.Fprintf(os.Stderr, "%d: NEEDAFIX already specified as %s\n", a.ln, needaffix)
continue
}
needaffix = arg[1]
}
}
}
func newPrefix(strip string, aff string, cond string) (*prefix, error) {
var err error
var re *regexp.Regexp
if cond != "." {
re, err = regexp.Compile("^"+cond+".*")
if err != nil {
return nil, err
}
} else {
cond = ""
}
return &prefix{strip:strip, affix: aff, cond: cond, re: re}, nil
}
func newSuffix(strip string, aff string, cond string) (*suffix, error) {
var err error
var re *regexp.Regexp
if cond != "." {
re, err = regexp.Compile(".*"+cond+"$")
if err != nil {
return nil, err
}
} else {
cond = ""
}
return &suffix{strip:strip, affix: aff, cond: cond, re: re}, nil
}
func processRules(f *os.File) {
a := &affixReader{bufio.NewReader(f), 0}
rules = make(map[string]*rule)
for {
arg := a.getRule()
if arg == nil {
break
}
if verbose {
fmt.Fprintf(os.Stderr, "%v\n", arg)
}
flag := arg[1]
r, ok := rules[flag]
if ok {
fmt.Fprintf(os.Stderr, "%s: duplicate rule @ line %d\n", flag, r.ln)
}
count, err := strconv.Atoi(arg[3])
if err != nil {
fmt.Fprintf(os.Stderr, "%s: badcount: %v\n", flag, arg[3])
continue
}
r = &rule{name:flag,ln:a.ln, affix:make([]affix, count)}
if arg[2] == "Y" {
r.cross = true
}
var suffix bool
if arg[0] == "SFX" {
suffix = true
}
for i := range r.affix {
arg := a.getRule()
if arg == nil {
fmt.Fprintf(os.Stderr, "%s: file too short\n", flag)
panic("bad affix file")
}
if arg[2] == "0" {
arg[2] = ""
}
if len(arg) < 5 {
//fmt.Fprintf(os.Stderr, "%d: not enough fields\n", a.ln)
arg = append(arg, ".")
}
var affix affix
if arg[3] == "0" {
arg[3] = ""
}
if suffix {
affix, err = newSuffix(arg[2], arg[3], arg[4])
} else {
affix, err = newPrefix(arg[2], arg[3], arg[4])
}
if err != nil {
fmt.Fprintf(os.Stderr, "%d: %v\n", a.ln, err)
}
r.affix[i] = affix
}
rules[flag] = r
}
if verbose {
fmt.Fprintf(os.Stderr, "rules:\n")
for _, r := range rules {
fmt.Fprintf(os.Stderr, "%v\n", r)
}
}
if needaffix != "" {
rules[needaffix] = &rule{name:needaffix, need: true}
}
}
func getflags(aff string) (flags []string) {
if numflags {
flags = commaRE.FindAllString(aff, -1)
} else {
flags = make([]string, len(aff)/flaglen)
j := 0
for i,_ := range flags {
flags[i] = aff[j:j+flaglen]
j += flaglen
}
}
return
}
func (a *suffix) expand(word string) {
if a.cond == "" {
if a.strip != "" {
word = word[:len(word)-len(a.strip)]
}
expandAll(word + a.affix)
return
}
if a.re.FindString(word) == "" {
return
}
if a.strip != "" {
word = word[:len(word)-len(a.strip)]
}
expandAll(fmt.Sprintf("%s\n", word+a.affix))
}
func (a *prefix) expand(word string) {
if a.cond == "" {
if a.strip != "" {
word = word[len(a.strip):]
}
expandAll(a.affix+word)
return
}
if a.re.FindString(word) == "" {
return
}
if a.strip != "" {
word = word[len(a.strip):]
}
expandAll(a.affix+word)
}
var ln = 2
func expandAll(line string) {
s := strings.Split(line, "/")
if len(s) == 1 {
fmt.Printf("%s\n", s[0])
return
}
flags := getflags(s[1])
stem := false
for _,key := range flags {
r, ok := rules[key]
if ok && r.need {
stem = true
break
}
}
if !stem {
fmt.Printf("%s\n", s[0])
}
for _,key := range flags {
//fmt.Fprintf(os.Stderr, "key: %v\n", key)
r, ok := rules[key]
if !ok {
fmt.Fprintf(os.Stderr, "%d: %s not found\n", ln, key)
continue
}
if r.need { // ignore this need flag
continue
}
//fmt.Fprintf(os.Stderr, "%v\n", r)
for _,a := range r.affix {
a.expand(s[0])
}
}
}
func processDict(f *os.File) error {
d := bufio.NewReader(f)
line, err := d.ReadString('\n')
if err != nil {
return err
}
count, err := strconv.Atoi(strings.TrimSpace(line))
if err != nil {
panic(fmt.Sprintf("error: %v", err.Error()))
}
if verbose {
fmt.Fprintf(os.Stderr, "%d words\n\n", count)
}
//words := make([]string, count)
//affix := make([]string, count)
for i := 0; ; i++ {
line, err := d.ReadString('\n')
if err == io.EOF {
break
}
line = strings.TrimSpace(line)
if verbose {
fmt.Fprintf(os.Stderr, "%d: |%s|\n",i, line)
}
expandAll(line)
ln++
}
return nil
}
func errExit(msg string) {
fmt.Fprintf(os.Stderr, "%s", msg)
os.Exit(1)
}
func usage() {
errExit("Usage: words aff_file dic_file\n")
}
func main() {
flag.Parse()
verbose = *flagv
if flag.NArg() < 2 {
usage()
}
aff := flag.Arg(0)
dic := flag.Arg(1)
if path.Ext(aff) != ".aff" || path.Ext(dic) != ".dic" {
usage()
}
xaff, err := os.Open(aff)
if err != nil {
errExit(aff + ":" + err.Error())
}
xdict, err := os.Open(dic)
if err != nil {
errExit(dic + ":" + err.Error())
}
processRules(xaff)
processDict(xdict)
xaff.Close()
xdict.Close()
}