-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwordnet_test.go
164 lines (143 loc) · 3.42 KB
/
wordnet_test.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
package wnram
import (
"path"
"runtime"
"testing"
)
const PathToWordnetDataFiles = "./data"
func sourceCodeRelPath(suffix string) string {
_, fileName, _, _ := runtime.Caller(1)
return path.Join(path.Dir(fileName), suffix)
}
var wnInstance *Handle
var wnErr error
func init() {
wnInstance, wnErr = New(sourceCodeRelPath(PathToWordnetDataFiles))
}
func TestParsing(t *testing.T) {
if wnErr != nil {
t.Fatalf("Can't initialize: %s", wnErr)
}
}
func TestBasicLookup(t *testing.T) {
// very basic test
found, err := wnInstance.Lookup(Criteria{Matching: "good"})
if err != nil {
t.Fatalf("%s", err)
}
gotAdjective := false
for _, f := range found {
if f.POS() == Adjective {
gotAdjective = true
break
}
}
if !gotAdjective {
t.Errorf("couldn't find basic adjective form for good")
}
}
func TestLemma(t *testing.T) {
found, err := wnInstance.Lookup(Criteria{Matching: "awesome", POS: []PartOfSpeech{Adjective}})
if err != nil {
t.Fatalf("%s", err)
}
if len(found) != 1 {
for _, f := range found {
f.Dump()
}
t.Fatalf("expected one synonym cluster for awesome, got %d", len(found))
}
if found[0].Lemma() != "amazing" {
t.Errorf("incorrect lemma for awesome (%s)", found[0].Lemma())
}
}
func setContains(haystack, needles []string) bool {
for _, n := range needles {
found := false
for _, h := range haystack {
if n == h {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func TestSynonyms(t *testing.T) {
found, err := wnInstance.Lookup(Criteria{Matching: "yummy", POS: []PartOfSpeech{Adjective}})
if err != nil {
t.Fatalf("%s", err)
}
if len(found) != 1 {
for _, f := range found {
f.Dump()
}
t.Fatalf("expected one synonym cluster for yummy, got %d", len(found))
}
syns := found[0].Synonyms()
if !setContains(syns, []string{"delicious", "delectable"}) {
t.Errorf("missing synonyms for yummy")
}
}
func TestAntonyms(t *testing.T) {
found, err := wnInstance.Lookup(Criteria{Matching: "good", POS: []PartOfSpeech{Adjective}})
if err != nil {
t.Fatalf("%s", err)
}
var antonyms []string
for _, f := range found {
as := f.Related(Antonym)
for _, a := range as {
antonyms = append(antonyms, a.Word())
}
}
if !setContains(antonyms, []string{"bad", "evil"}) {
t.Errorf("missing antonyms for good")
}
}
func TestHypernyms(t *testing.T) {
found, err := wnInstance.Lookup(Criteria{Matching: "jab", POS: []PartOfSpeech{Noun}})
if err != nil {
t.Fatalf("%s", err)
}
var hypernyms []string
for _, f := range found {
as := f.Related(Hypernym)
for _, a := range as {
hypernyms = append(hypernyms, a.Word())
}
}
if !setContains(hypernyms, []string{"punch"}) {
t.Errorf("missing hypernyms for jab (expected punch, got %v)", hypernyms)
}
}
func TestHyponyms(t *testing.T) {
found, err := wnInstance.Lookup(Criteria{Matching: "food", POS: []PartOfSpeech{Noun}})
if err != nil {
t.Fatalf("%s", err)
}
var hyponyms []string
for _, f := range found {
as := f.Related(Hyponym)
for _, a := range as {
hyponyms = append(hyponyms, a.Word())
}
}
expected := []string{"chocolate", "cheese", "pasta", "leftovers"}
if !setContains(hyponyms, expected) {
t.Errorf("missing hyponyms for candy (expected %v, got %v)", expected, hyponyms)
}
}
func TestIterate(t *testing.T) {
count := 0
wnInstance.Iterate(PartOfSpeechList{Noun}, func(l Lookup) error {
count++
return nil
})
if count != 82192 {
t.Errorf("Missing nouns!")
}
}