forked from chewxy/lingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
featureExtraction.go
145 lines (126 loc) · 3.46 KB
/
featureExtraction.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
package dep
import (
"github.com/chewxy/lingo"
"github.com/chewxy/lingo/corpus"
)
// getFeatures extracts the IDs to pass into the neural network. These IDs are used in the network to construct the input layers
func getFeatures(c *configuration, dict *corpus.Corpus) []int {
// logf("CONFIG: %v", c)
wordFeats := make([]int, 0)
posFeats := make([]lingo.POSTag, 0)
labelFeats := make([]lingo.DependencyType, 0)
unknownID, _ := dict.Id("-UNKNOWN-")
for j := 2; j >= 0; j-- {
index := c.stackValue(j)
mor := c.annotation(index)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
}
// logf("wordFeats: %v", wordFeats)
for j := 0; j <= 2; j++ {
index := c.bufferValue(j)
mor := c.annotation(index)
// logf("Want: %v Index: %d. Morpheme: %v", j, index, mor)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
}
// logf("wordFeats: %v", wordFeats)
for j := 0; j <= 1; j++ {
k := c.stackValue(j)
index := c.lc(k, 1)
mor := c.annotation(index)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
labelFeats = append(labelFeats, c.label(index))
index = c.rc(k, 1)
mor = c.annotation(index)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
labelFeats = append(labelFeats, c.label(index))
index = c.lc(k, 2)
mor = c.annotation(index)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
labelFeats = append(labelFeats, c.label(index))
index = c.rc(k, 2)
mor = c.annotation(index)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
labelFeats = append(labelFeats, c.label(index))
leftChild := c.lc(k, 1)
index = c.lc(leftChild, 1)
mor = c.annotation(index)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
labelFeats = append(labelFeats, c.label(index))
rightChild := c.rc(k, 1)
index = c.rc(rightChild, 1)
mor = c.annotation(index)
if wordID, ok := dict.Id(mor.Value); ok {
wordFeats = append(wordFeats, wordID)
} else {
wordFeats = append(wordFeats, unknownID)
}
posFeats = append(posFeats, mor.POSTag)
labelFeats = append(labelFeats, c.label(index))
}
// the embedding matrix is arranged thus:
/*
POSTag0 0, 1, ... 50
POSTag1
...
MAXTAG-1
DepType0
DepType1
...
MAXDEPTYPE-1
WordID0
...
WordIDN
*/
features := make([]int, MAXFEATURE)
for i, w := range wordFeats {
features[i] = w + wordFeatsStartAt
}
for i, t := range posFeats {
features[i+POS_OFFSET] = int(t)
}
for i, l := range labelFeats {
features[i+DEP_OFFSET] = int(l) + labelFeatsStartAt
}
return features
}
const (
POS_OFFSET int = 18
DEP_OFFSET = 36
STACK_OFFSET = 6
STACK_NUMBER = 6
)