-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorpus.go
208 lines (168 loc) · 4.11 KB
/
corpus.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
package main
import (
"bufio"
"encoding/xml"
"fmt"
"io"
"os"
"strings"
"time"
)
const forwardPunctuators = "«"
const backwardPunctuators = ",.:»"
func normalizeSentence(sentence string) string {
sentence = strings.Trim(sentence, " ")
if len(sentence) == 0 {
return ""
}
for _, chr := range forwardPunctuators {
punct := string(chr)
sentence = strings.Replace(sentence, punct+" ", punct, -1)
}
for _, chr := range backwardPunctuators {
punct := string(chr)
sentence = strings.Replace(sentence, " "+punct, punct, -1)
}
last := sentence[len(sentence)-1]
if last != '.' && last != '?' && last != '!' {
return ""
}
return sentence
}
func BuildCorpus(xmlFilename, corpusFilename string) error {
xml_, err := os.Open(xmlFilename)
if err != nil {
return err
}
defer xml_.Close()
txt, err := os.Create(corpusFilename)
if err != nil {
return err
}
defer txt.Close()
decoder := xml.NewDecoder(xml_)
for {
token, err := decoder.Token()
if token == nil {
break
}
if err != nil {
return err
}
switch element := token.(type) {
case xml.StartElement:
if element.Name.Local != "source" {
continue
}
var sentence string
decoder.DecodeElement(&sentence, &element)
if sentence = normalizeSentence(sentence); sentence == "" {
continue
}
if _, err = txt.WriteString(sentence + "\n"); err != nil {
return err
}
}
}
return nil
}
func ReadCorpus(corpusFilename string, from, to int) ([]string, error) {
file, err := os.Open(corpusFilename)
if err != nil {
return nil, err
}
defer file.Close()
index := 0
var sentences []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if index >= from && index < to {
sentences = append(sentences, scanner.Text())
}
index += 1
}
return sentences, scanner.Err()
}
type ParseCorpusSentenceCallback func(
index int, sentence string, matches []ParseMatch, verbose bool)
type ParseCorpusStatsCallback func(succeeded, failed int)
func readRecord(file *os.File, index int) (bool, error) {
byte_ := []byte{0}
offset := int64(index / 8)
if _, err := file.ReadAt(byte_, offset); err != nil {
if err == io.EOF {
return false, nil
}
return false, err
}
return byte_[0]&(1<<uint(index%8)) != 0, nil
}
func writeRecord(file *os.File, index int, parsed bool) error {
byte_ := []byte{0}
offset := int64(index / 8)
if _, err := file.ReadAt(byte_, offset); err != nil && err != io.EOF {
return err
}
if parsed {
byte_[0] |= 1 << uint(index%8)
} else {
byte_[0] &= ^(1 << uint(index%8))
}
_, err := file.WriteAt(byte_, offset)
return err
}
func ParseCorpus(corpusFilename string, from, to int,
recordFilename string, saveChanges, verbose bool,
sentenceCallback ParseCorpusSentenceCallback,
statsCallback ParseCorpusStatsCallback) error {
sentences, err := ReadCorpus(corpusFilename, from, to)
if err != nil {
return fmt.Errorf("failed to read text corpus: %s\n", err)
}
record, err := os.OpenFile(recordFilename, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return fmt.Errorf("failed to open record file: %s\n", err)
}
defer record.Close()
if err := initParser(); err != nil {
return fmt.Errorf("failed to initialize parser: %s\n", err)
}
defer finalizeParser()
lastTime := time.Now()
succeeded, failed, index := 0, 0, from
for _, sentence := range sentences {
matches := Parse(strings.ToLower(sentence), "sentence", 0)
ClearCache()
parsed := false
for _, m := range matches {
if len(m.Text) == len(sentence) {
parsed = true
break
}
}
parsedBefore, err := readRecord(record, index)
if err != nil {
return fmt.Errorf("failed to read record file: %s\n", err)
}
if parsed != parsedBefore {
sentenceCallback(index, sentence, matches, verbose)
if saveChanges {
if err := writeRecord(record, index, parsed); err != nil {
return fmt.Errorf("failed to write record file: %s\n", err)
}
}
}
if parsed {
succeeded += 1
} else {
failed += 1
}
index += 1
if now := time.Now(); now.Sub(lastTime).Seconds() >= 1 {
statsCallback(succeeded, failed)
lastTime = now
}
}
statsCallback(succeeded, failed)
return nil
}