-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsentence.go
321 lines (271 loc) · 7.11 KB
/
sentence.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
package lingo
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/pkg/errors"
)
/* Lexeme Sentence */
type LexemeSentence []Lexeme
func NewLexemeSentence() LexemeSentence { return LexemeSentence(make([]Lexeme, 0)) }
func (ls LexemeSentence) String() string {
var buf bytes.Buffer
for _, lex := range ls {
buf.WriteString(lex.Value)
buf.WriteString(" ")
}
return strings.Trim(buf.String(), " ")
}
/* Annotated Sentence */
// AnnotatedSentence is a sentence, but each word has been annotated.
type AnnotatedSentence []*Annotation
func NewAnnotatedSentence() AnnotatedSentence { return make(AnnotatedSentence, 0) }
func (as AnnotatedSentence) Clone() AnnotatedSentence {
retVal := make(AnnotatedSentence, len(as))
for i, a := range as {
// don't clone rootAnnotation
if i == 0 && a == rootAnnotation {
retVal[i] = a
continue
}
retVal[i] = a.Clone()
}
return retVal
}
func (as AnnotatedSentence) SetID() {
for i, a := range as {
if i == 0 && a == rootAnnotation {
continue
}
a.ID = i
}
}
func (as AnnotatedSentence) Fix() {
if as[0].Lexeme == rootLexeme {
as[0] = rootAnnotation
}
as.SetID()
for _, a := range as {
if a.Head != nil {
if a.HeadID() == -1 && a.Head.Lexeme == rootLexeme {
a.Head = rootAnnotation
continue
}
a.SetHead(as[a.HeadID()])
}
}
}
func (as AnnotatedSentence) IsValid() bool {
// check that IDs are set
zeroes := 0
for _, a := range as {
if a.ID == 0 {
zeroes++
}
}
// IDs not properly set
if zeroes > 1 {
return false
}
// TODO
// check that there is only one root
return true
}
/* Return slices of x */
// Phrase returns the slice of the sentence. While you can do the same by simply doing as[start:end], this method returns errors instead of panicking
func (as AnnotatedSentence) Phrase(start, end int) (AnnotatedSentence, error) {
if start < 0 {
return nil, errors.Errorf("Start: %d < 0", start)
}
if end > len(as) {
return nil, errors.Errorf("End: %d > len(as): %d", end, len(as))
}
return as[start:end], nil
}
// IDs returns the list of IDs in the sentence. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) IDs() []int {
retVal := make([]int, len(as))
for i, a := range as {
retVal[i] = a.ID
}
return retVal
}
// Tags returns the POSTags of the sentence. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) Tags() []POSTag {
retVal := make([]POSTag, len(as))
for i, a := range as {
retVal[i] = a.POSTag
}
return retVal
}
// Heads returns the head IDs of the sentence. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) Heads() []int {
retVal := make([]int, len(as))
for i, a := range as {
retVal[i] = a.HeadID()
}
return retVal
}
// Leaves returns the *Annotations which are leaves. If the dependency hasn't been set yet, every single *Annotation is a leaf.
func (as AnnotatedSentence) Leaves() (retVal []int) {
for i := range as {
if len(as.Children(i)) == 0 {
retVal = append(retVal, i)
}
}
return
}
// Labels returns the DependencyTypes of the sentence. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) Labels() []DependencyType {
retVal := make([]DependencyType, len(as))
for i, a := range as {
retVal[i] = a.DependencyType
}
return retVal
}
// StringSlice returns the original words as a slice of string. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) StringSlice() []string {
retVal := make([]string, len(as), len(as))
for i, a := range as {
retVal[i] = a.Value
}
return retVal
}
// LoweredStringSlice returns the lowercased version of the words in the sentence as a slice of string. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) LoweredStringSlice() []string {
retVal := make([]string, len(as), len(as))
for i, a := range as {
retVal[i] = a.Lowered
}
return retVal
}
// Lemmas returns the lemmas as as slice of string. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) Lemmas() []string {
lemmas := make([]string, len(as))
for i, a := range as {
lemmas[i] = a.Lemma
}
return lemmas
}
// Stems returns the stems as a slice of string. The return value has exactly the same length as the sentence.
func (as AnnotatedSentence) Stems() []string {
stems := make([]string, len(as))
for i, a := range as {
stems[i] = a.Stem
}
return stems
}
func (as AnnotatedSentence) Children(h int) (retVal []int) {
for i, v := range as {
if v.HeadID() == h {
retVal = append(retVal, i)
}
}
return
}
func (as AnnotatedSentence) Edges() (retVal []DependencyEdge) {
for _, a := range as {
var head = -1
if a.Head != nil {
head = a.HeadID()
}
if head == -1 {
head = 0
}
edge := DependencyEdge{as[head], a, a.DependencyType}
retVal = append(retVal, edge)
}
sort.Sort(edgeByID(retVal))
return
}
/* To other structures */
func (as AnnotatedSentence) Dependency() *Dependency {
return NewDependency(FromAnnotatedSentence(as))
}
func (as AnnotatedSentence) Tree() *DependencyTree {
tracker := make([]*DependencyTree, len(as))
rootNode := NewDependencyTree(nil, 0, rootAnnotation)
tracker[0] = rootNode
for i := 1; i < len(as); i++ {
head := as[i].HeadID()
var headDep *DependencyTree
if head == -1 {
headDep = rootNode
} else {
headDep = tracker[head]
}
if headDep == nil {
// make a dependency for the head
headDep = NewDependencyTree(nil, head, as[head])
tracker[head] = headDep
}
dep := tracker[i]
if dep == nil {
dep = NewDependencyTree(headDep, i, as[i])
tracker[i] = dep
} else {
dep.Parent = headDep
}
headDep.AddChild(dep)
dep.Type = as[i].DependencyType
}
// return tracker[len(tracker)-1]
// log.Printf("Tracker: %v, len(as): %d. Root: %v", tracker, len(as), rootNode.Children)
return rootNode
}
// Stringer interface
func (as AnnotatedSentence) String() string {
var buf bytes.Buffer
for i, a := range as {
buf.WriteString(fmt.Sprintf("%s/%s", a.Value, a.POSTag))
if i < len(as)-1 {
buf.WriteString(" ")
}
}
return buf.String()
}
func (as AnnotatedSentence) ValueString() string {
var buf bytes.Buffer
for i, a := range as {
buf.WriteString(a.Value)
if i < len(as)-1 {
buf.WriteString(" ")
}
}
return buf.String()
}
func (as AnnotatedSentence) LoweredString() string {
var buf bytes.Buffer
for i, a := range as {
buf.WriteString(a.Lowered)
if i < len(as)-1 {
buf.WriteString(" ")
}
}
return buf.String()
}
func (as AnnotatedSentence) LemmaString() string {
var buf bytes.Buffer
for i, a := range as {
buf.WriteString(a.Lemma)
if i < len(as)-1 {
buf.WriteString(" ")
}
}
return buf.String()
}
func (as AnnotatedSentence) StemString() string {
var buf bytes.Buffer
for i, a := range as {
buf.WriteString(a.Stem)
if i < len(as)-1 {
buf.WriteString(" ")
}
}
return buf.String()
}
// sort interface
func (as AnnotatedSentence) Len() int { return len(as) }
func (as AnnotatedSentence) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
func (as AnnotatedSentence) Less(i, j int) bool { return as[i].ID < as[j].ID }