-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemmy.go
299 lines (255 loc) · 6.81 KB
/
lemmy.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
package main
import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"text/scanner"
"time"
"github.com/psywolf/autocache"
"gopkg.in/alecthomas/kingpin.v1"
)
var (
urlBase = "http://www.perseus.tufts.edu/hopper/xmlmorph?lang=la&lookup="
MAX_REQUESTS = kingpin.Flag("requestCount", "The max number of concurrant requests that lemmy should send to www.perseus.tufts.edu (More isn't always better)").Default("10").Short('r').Int()
CACHE_SIZE = kingpin.Flag("cacheSize", "The max number of lemmatized words to cache").Default("10000").Short('c').Int()
verbose = kingpin.Flag("verbose", "Print extra debugging information").Short('v').Bool()
input = kingpin.Arg("input", "File or Folder containing latin text to be lemmatized").Required().File()
outputPath = kingpin.Arg("output", "File or Folder to output the lemmatized text").Required().String()
)
func main() {
kingpin.Version("1.0.3")
kingpin.Parse()
defer input.Close()
if *MAX_REQUESTS < 1 {
fmt.Fprintf(os.Stderr, "ERROR: requestCount of %d is invalid. Must be greater than 0.\n", *MAX_REQUESTS)
os.Exit(1)
}
if *CACHE_SIZE < 0 {
fmt.Fprintf(os.Stderr, "ERROR: cacheSize of %d is invalid. Must be at least 0.\n", *CACHE_SIZE)
os.Exit(1)
}
rand.Seed(time.Now().UnixNano())
inputInfo, err := input.Stat()
if err != nil {
panic(err)
}
outputInfo, err := os.Stat(*outputPath)
if !os.IsNotExist(err) && err != nil {
panic(err)
}
if !os.IsNotExist(err) && outputInfo.IsDir() != inputInfo.IsDir() {
fmt.Fprintf(os.Stderr, "ERROR: Output and Input must both be either files or folders\n")
os.Exit(1)
}
if !inputInfo.IsDir() {
LemmatizeFile(*input, *outputPath)
} else {
fileInfos, err := input.Readdir(0)
if err != nil {
panic(err)
}
if _, err := os.Stat(*outputPath); os.IsNotExist(err) {
os.Mkdir(*outputPath, 0777)
}
for _, fi := range fileInfos {
f, err := os.Open(filepath.Join(input.Name(), fi.Name()))
defer f.Close()
if err != nil {
panic(err)
}
LemmatizeFile(f, filepath.Join(*outputPath, fi.Name()))
}
}
}
func LemmatizeFile(inputFile *os.File, outputPath string) {
if _, err := os.Stat(outputPath); !os.IsNotExist(err) {
fmt.Printf("WARNING: ")
for {
fmt.Printf("File '%s' already exists. Overwrite? (y/n): ", outputPath)
var yn string
fmt.Scanf("%s\n", &yn)
if yn == "y" || yn == "Y" {
break
} else if yn == "n" || yn == "N" {
return
}
fmt.Println("Invalid Input.")
}
}
outFile, err := os.Create(outputPath)
defer outFile.Close()
if err != nil {
panic(err)
}
lr := NewLemmaReader(inputFile)
fmt.Printf("Lemmatizing '%s' into '%s'", inputFile.Name(), outputPath)
i := 1
for w, done := lr.Read(); !done; w, done = lr.Read() {
outFile.WriteString(w + " ")
if i%50 == 0 {
fmt.Print(".")
}
i++
}
outFile.WriteString("\n")
fmt.Println()
}
func LemmatizeText(f io.Reader) *LemmaReader {
return NewLemmaReader(f)
}
type LemmaReader struct {
outChan chan *postLemMsg
cache *autocache.Cache
s scanner.Scanner
}
func NewLemmaReader(f io.Reader) *LemmaReader {
l := &LemmaReader{outChan: make(chan *postLemMsg)}
inChan := make(chan *preLemMsg)
l.s.Init(f)
//change the mode to only look for words and numbers
//the default mode ignores go style comments
//and chokes on unmatched quotes or backticks
l.s.Mode = scanner.ScanIdents
httpClient := &http.Client{}
l.cache = autocache.New(*CACHE_SIZE, func(word string) (string, error) {
lemmyd, err := LemmatizeWord(httpClient, word)
for err != nil {
if *verbose {
fmt.Fprintf(os.Stderr, "\nError on word '%s'\ntype is '%T'\nerr is '%s'\nRETRYING\n", word, err, err)
}
time.Sleep(time.Duration(500+rand.Intn(500)) * time.Millisecond)
lemmyd, err = LemmatizeWord(httpClient, word)
}
return lemmyd, nil
})
go l.populateInChan(inChan)
go l.processInChan(inChan)
return l
}
func (l *LemmaReader) populateInChan(inChan chan *preLemMsg) {
waitOn := make(chan struct{})
if *MAX_REQUESTS > 1 {
//kick off the first word
go func(waitOn chan struct{}) {
waitOn <- struct{}{}
}(waitOn)
}
var proceed chan struct{}
for l.s.Scan() != scanner.EOF {
token := l.s.TokenText()
if token != "" {
if *verbose {
fmt.Printf("token is '%s'\n", token)
}
proceed = make(chan struct{})
inChan <- &preLemMsg{token, waitOn, proceed}
waitOn = proceed
}
}
close(inChan)
//drain the last waitOn channel so the final goroutine doesn't block on it
if *MAX_REQUESTS > 1 {
<-waitOn
}
}
func (l *LemmaReader) processInChan(inChan chan *preLemMsg) {
doneChan := make(chan struct{})
//create MAX_REQUESTS worker goroutines
for i := 0; i < *MAX_REQUESTS; i++ {
go func(inChan chan *preLemMsg, doneChan chan struct{}, i int) {
for msg := range inChan {
if *verbose {
fmt.Printf("thread #%d word '%s': get from cache\n", i, msg.word)
}
lemmyd, err := l.cache.Get(msg.word)
if err != nil {
panic(err)
}
if *MAX_REQUESTS > 1 {
if *verbose {
fmt.Printf("thread #%d word '%s': waiting to output '%s'\n", i, msg.word, lemmyd)
}
<-msg.waitOn
}
if lemmyd != "" {
l.outChan <- &postLemMsg{lemmyd, false}
}
if *MAX_REQUESTS > 1 {
if *verbose {
fmt.Printf("thread #%d word '%s': signaling next thread to proceed\n", i, msg.word)
}
msg.proceed <- struct{}{}
}
if *verbose {
fmt.Printf("thread #%d word '%s': exiting thread\n", i, msg.word)
}
}
doneChan <- struct{}{}
}(inChan, doneChan, i)
}
//wait for all goroutines to complete
for i := 0; i < *MAX_REQUESTS; i++ {
<-doneChan
}
l.outChan <- &postLemMsg{"", true}
}
func (l *LemmaReader) Read() (string, bool) {
msg := <-l.outChan
return msg.word, msg.done
}
type preLemMsg struct {
word string
waitOn chan struct{}
proceed chan struct{}
}
type postLemMsg struct {
word string
done bool
}
func LemmatizeWord(httpClient *http.Client, word string) (string, error) {
if *verbose {
fmt.Printf("lemmatizing word '%s'\n", word)
}
req, err := http.NewRequest("GET", urlBase+url.QueryEscape(word), nil)
if err != nil {
return "", err
}
req.Close = true
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("StatusCode %d when trying to lemmatize word '%s' (expected 200)", resp.StatusCode, word)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
lemXml := Analyses{}
err = xml.Unmarshal(body, &lemXml)
if err != nil {
if *verbose {
fmt.Fprintf(os.Stderr, "\n%s\n", body)
}
return "", err
}
if len(lemXml.Analysis) == 0 {
return "", nil
}
return lemXml.Analysis[0].Lemma, nil
}
type Analysis struct {
Lemma string `xml:"lemma"`
}
type Analyses struct {
XMLName xml.Name `xml:"analyses"`
Analysis []Analysis `xml:"analysis"`
}