-
Notifications
You must be signed in to change notification settings - Fork 1
/
semdb.go
executable file
·229 lines (200 loc) · 5.16 KB
/
semdb.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
package linguo
import (
"container/list"
"io/ioutil"
"strings"
set "gopkg.in/fatih/set.v0"
)
const (
SEMDB_WN_POS_MAP = 1 + iota
SEMDB_DATA_FILES
)
type SenseInfo struct {
sense string
parents *list.List
semFile string
words *list.List
tonto *list.List
sumo string
cyc string
}
func NewSenseInfo(syn string, data string) *SenseInfo {
this := SenseInfo{
sense: syn,
}
if data != "" {
fields := StrArray2StrList(Split(data, " "))
f := fields.Front()
if string(f.Value.(string)[0]) != "-" {
this.parents = StrArray2StrList(Split(f.Value.(string), ":"))
}
f = f.Next()
this.semFile = f.Value.(string)
f = f.Next()
if string(f.Value.(string)[0]) != "-" {
this.tonto = StrArray2StrList(Split(f.Value.(string), ":"))
}
f = f.Next()
if string(f.Value.(string)[0]) != "-" {
this.sumo = f.Value.(string)
}
f = f.Next()
if string(f.Value.(string)[0]) != "-" {
this.cyc = f.Value.(string)
}
}
return &this
}
func (this *SenseInfo) getParentsString() string {
out := make([]string, this.parents.Len())
for i, p := 0, this.parents.Front(); i < this.parents.Len() && p != nil; i, p = i+1, p.Next() {
out[i] = p.Value.(string)
}
return strings.Join(out, ":")
}
type PosMapRule struct {
pos string
wnpos string
lemma string
}
type SemanticDB struct {
posMap *list.List
formDict *Database
senseDB *Database
wndb *Database
}
func NewSemanticDB(wsdFile string) *SemanticDB {
this := SemanticDB{
posMap: list.New(),
}
var formFile, dictFile, wnFile string
path := wsdFile[0:strings.LastIndex(wsdFile, "/")]
posset := set.New()
cfg := NewConfigFile(true, "")
cfg.AddSection("WNposMap", SEMDB_WN_POS_MAP)
cfg.AddSection("DataFiles", SEMDB_DATA_FILES)
if !cfg.Open(wsdFile) {
panic("Error opening configuration file " + wsdFile)
}
line := ""
for cfg.GetContentLine(&line) {
items := Split(line, " ")
switch cfg.GetSection() {
case SEMDB_WN_POS_MAP:
{
r := PosMapRule{}
r.pos = items[0]
r.wnpos = items[1]
r.lemma = items[2]
this.posMap.PushBack(r)
if r.lemma != "L" && r.lemma != "F" {
posset.Add(r.lemma)
}
break
}
case SEMDB_DATA_FILES:
{
key := items[0]
fname := items[1]
if key == "formDictFile" {
formFile = path + "/" + strings.Replace(fname, "./", "", -1)
} else if key == "senseDictFile" {
dictFile = path + "/" + strings.Replace(fname, "./", "", -1)
} else if key == "wnFile" {
wnFile = path + "/" + strings.Replace(fname, "./", "", -1)
}
break
}
default:
break
}
}
if formFile == "" || posset.Size() == 0 {
this.formDict = nil
} else {
fileString, err := ioutil.ReadFile(formFile)
if err != nil {
panic("Error loading file " + formFile)
}
lines := strings.Split(string(fileString), "\n")
this.formDict = NewDatabase(DB_MAP)
for _, line := range lines {
items := Split(line, " ")
form := items[0]
for i := 1; i < len(items); i = i + 2 {
lemma := items[i]
tag := items[i+1]
if posset.Has(tag) {
this.formDict.addDatabase(lemma+" "+tag, form)
}
}
}
}
if dictFile == "" {
this.senseDB = nil
} else {
fileString, err := ioutil.ReadFile(dictFile)
if err != nil {
panic("Error loading file " + dictFile)
}
lines := strings.Split(string(fileString), "\n")
this.senseDB = NewDatabase(DB_MAP)
for _, line := range lines {
items := Split(line, " ")
sens := items[0]
tag := sens[strings.Index(sens, "-")+1:]
for i := 1; i < len(items); i++ {
wd := items[i]
this.senseDB.addDatabase("S:"+sens, wd)
this.senseDB.addDatabase("W:"+wd+":"+tag, sens)
}
}
}
if wnFile == "" {
this.wndb = nil
} else {
this.wndb = NewDatabaseFromFile(wnFile)
}
return &this
}
func (this *SemanticDB) getWordSenses(form string, lemma string, pos string) *list.List {
searchList := list.New()
this.getWNKeys(form, lemma, pos, searchList)
lsen := list.New()
for p := searchList.Front(); p != nil; p = p.Next() {
s := StrArray2StrList(Split(this.senseDB.accessDatabase("W:"+p.Value.(Pair).first.(string)+":"+p.Value.(Pair).second.(string)), " "))
for ss := s.Front(); ss != nil; ss = ss.Next() {
lsen.PushBack(ss.Value.(string))
}
if lsen.Len() > 0 {
}
}
return lsen
}
func (this *SemanticDB) getSenseWords(sens string) *list.List {
return StrArray2StrList(Split(this.senseDB.accessDatabase("S:"+sens), " "))
}
func (this *SemanticDB) getSenseInfo(syn string) *SenseInfo {
sinf := NewSenseInfo(syn, this.wndb.accessDatabase(syn))
sinf.words = this.getSenseWords(syn)
return sinf
}
func (this *SemanticDB) getWNKeys(form string, lemma string, tag string, searchList *list.List) {
searchList = searchList.Init()
for p := this.posMap.Front(); p != nil; p = p.Next() {
if strings.Index(tag, p.Value.(PosMapRule).pos) == 0 {
var lm string
if p.Value.(PosMapRule).lemma == "L" {
lm = lemma
} else if p.Value.(PosMapRule).lemma == "F" {
lm = form
} else {
lm = this.formDict.accessDatabase(lemma + " " + p.Value.(PosMapRule).lemma)
}
fms := StrArray2StrList(Split(lm, " "))
for ifm := fms.Front(); ifm != nil; ifm = ifm.Next() {
searchList.PushBack(Pair{ifm.Value.(string), p.Value.(PosMapRule).wnpos})
}
}
}
}