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
|
package nlp_test
import (
"github.com/Fuwn/kivia/internal/nlp"
"os"
"path/filepath"
"strings"
"testing"
)
func TestDictionaryRecognizesLexiconWords(testingContext *testing.T) {
dictionaryFile := filepath.Join("..", "..", "testdata", "dictionary", "words.txt")
testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryFile)
dictionary, err := nlp.NewDictionary()
if err != nil {
testingContext.Fatalf("NewDictionary returned an error: %v", err)
}
if !dictionary.IsWord("options") {
testingContext.Fatalf("Expected options to be recognized.")
}
if !dictionary.IsWord("has") {
testingContext.Fatalf("Expected has to be recognized.")
}
if !dictionary.IsWord("resources") {
testingContext.Fatalf("Expected resources to be recognized through plural inflection.")
}
}
func TestDictionaryFindsAbbreviationExpansions(testingContext *testing.T) {
dictionaryFile := filepath.Join("..", "..", "testdata", "dictionary", "words.txt")
testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryFile)
dictionary, err := nlp.NewDictionary()
if err != nil {
testingContext.Fatalf("NewDictionary returned an error: %v", err)
}
cases := map[string]string{
"expr": "expression",
"ctx": "context",
"err": "error",
}
for token, expectedExpansion := range cases {
expansion, ok := dictionary.AbbreviationExpansion(token)
if !ok {
testingContext.Fatalf("Expected an abbreviation expansion for %q.", token)
}
if expansion != expectedExpansion {
testingContext.Fatalf("Expected %q to expand to %q, got %q.", token, expectedExpansion, expansion)
}
}
}
func TestDictionaryLoadsFromMultipleDictionaryFiles(testingContext *testing.T) {
tempDirectory := testingContext.TempDir()
firstDictionaryPath := filepath.Join(tempDirectory, "first.txt")
secondDictionaryPath := filepath.Join(tempDirectory, "second.txt")
combinedPathList := strings.Join([]string{firstDictionaryPath, secondDictionaryPath}, string(os.PathListSeparator))
if err := os.WriteFile(firstDictionaryPath, []byte("alpha\n"), 0o644); err != nil {
testingContext.Fatalf("os.WriteFile returned an error: %v", err)
}
if err := os.WriteFile(secondDictionaryPath, []byte("beta\n"), 0o644); err != nil {
testingContext.Fatalf("os.WriteFile returned an error: %v", err)
}
testingContext.Setenv("KIVIA_DICTIONARY_PATH", combinedPathList)
dictionary, err := nlp.NewDictionary()
if err != nil {
testingContext.Fatalf("NewDictionary returned an error: %v", err)
}
if !dictionary.IsWord("alpha") {
testingContext.Fatalf("Expected alpha to be recognized.")
}
if !dictionary.IsWord("beta") {
testingContext.Fatalf("Expected beta to be recognized.")
}
}
func TestDictionaryFailsWhenConfiguredPathHasNoWords(testingContext *testing.T) {
tempDirectory := testingContext.TempDir()
emptyDictionaryPath := filepath.Join(tempDirectory, "empty.txt")
if err := os.WriteFile(emptyDictionaryPath, []byte("\n"), 0o644); err != nil {
testingContext.Fatalf("os.WriteFile returned an error: %v", err)
}
testingContext.Setenv("KIVIA_DICTIONARY_PATH", emptyDictionaryPath)
_, err := nlp.NewDictionary()
if err == nil {
testingContext.Fatalf("Expected NewDictionary to fail when configured dictionary has no usable words.")
}
}
func TestDictionaryRecognizesDerivedForms(testingContext *testing.T) {
tempDirectory := testingContext.TempDir()
dictionaryPath := filepath.Join(tempDirectory, "base_words.txt")
if err := os.WriteFile(dictionaryPath, []byte("trim\ntoken\n"), 0o644); err != nil {
testingContext.Fatalf("os.WriteFile returned an error: %v", err)
}
testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPath)
dictionary, err := nlp.NewDictionary()
if err != nil {
testingContext.Fatalf("NewDictionary returned an error: %v", err)
}
if !dictionary.IsWord("trimmed") {
testingContext.Fatalf("Expected trimmed to be recognized from trim.")
}
if !dictionary.IsWord("tokenize") {
testingContext.Fatalf("Expected tokenize to be recognized from token.")
}
}
func TestDictionaryRecognizesBritishAndAmericanVariants(testingContext *testing.T) {
tempDirectory := testingContext.TempDir()
dictionaryPath := filepath.Join(tempDirectory, "british_words.txt")
if err := os.WriteFile(dictionaryPath, []byte("normalise\ncolour\ncentre\n"), 0o644); err != nil {
testingContext.Fatalf("os.WriteFile returned an error: %v", err)
}
testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPath)
dictionary, err := nlp.NewDictionary()
if err != nil {
testingContext.Fatalf("NewDictionary returned an error: %v", err)
}
if !dictionary.IsWord("normalize") {
testingContext.Fatalf("Expected normalize to be recognized from normalise.")
}
if !dictionary.IsWord("color") {
testingContext.Fatalf("Expected color to be recognized from colour.")
}
if !dictionary.IsWord("center") {
testingContext.Fatalf("Expected center to be recognized from centre.")
}
}
|