-
Notifications
You must be signed in to change notification settings - Fork 45
/
porterstemmer_stem_string_test.go
131 lines (93 loc) · 2.63 KB
/
porterstemmer_stem_string_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
package porterstemmer
import (
"bufio"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
)
func TestStemString(t *testing.T) {
testDataDirName := "testdata"
_, err := os.Stat(testDataDirName)
if nil != err {
_ = os.Mkdir(testDataDirName, 0755)
}
_, err = os.Stat(testDataDirName)
if nil != err {
t.Errorf("The test data folder ([%s]) does not exists (and could not create it). Received error: [%v]", testDataDirName, err)
/////// RETURN
return
}
vocFileName := testDataDirName + "/voc.txt"
_, err = os.Stat(vocFileName)
if nil != err {
vocHref := "http://tartarus.org/martin/PorterStemmer/voc.txt"
resp, err := http.Get(vocHref)
if nil != err {
t.Errorf("Could not download test file (from web) from URL: [%s]. Received error: [%v]", vocHref, err)
/////////// RETURN
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if nil != err {
t.Errorf("Error loading the contents of from URL: [%s]. Received error: [%v].", vocHref, err)
/////////// RETURN
return
}
_ = ioutil.WriteFile(vocFileName, respBody, 0644)
}
vocFd, err := os.Open(vocFileName)
if nil != err {
t.Errorf("Could NOT open testdata file: [%s]. Received error: [%v]", vocFileName, err)
/////// RETURN
return
}
defer vocFd.Close()
voc := bufio.NewReaderSize(vocFd, 1024)
outFileName := testDataDirName + "/output.txt"
_, err = os.Stat(outFileName)
if nil != err {
outHref := "http://tartarus.org/martin/PorterStemmer/output.txt"
resp, err := http.Get(outHref)
if nil != err {
t.Errorf("Could not download test file (from web) from URL: [%s]. Received error: [%v]", outHref, err)
/////////// RETURN
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if nil != err {
t.Errorf("Error loading the contents of from URL: [%s]. Received error: [%v].", outHref, err)
/////////// RETURN
return
}
_ = ioutil.WriteFile(outFileName, respBody, 0644)
}
outFd, err := os.Open(outFileName)
if nil != err {
t.Errorf("Could NOT open testdata file: [%s]. Received error: [%v]", outFileName, err)
/////// RETURN
return
}
defer outFd.Close()
out := bufio.NewReaderSize(outFd, 1024)
for {
vocS, err := voc.ReadString('\n')
if nil != err {
/////// BREAK
break
}
vocS = strings.Trim(vocS, "\n\r\t ")
expected, err := out.ReadString('\n')
if nil != err {
t.Errorf("Received unexpected error when trying to read a line from [%s]. Received error: [%v]", outFileName, err)
/////// BREAK
break
}
expected = strings.Trim(expected, "\n\r\t ")
actual := StemString(vocS)
if expected != actual {
t.Errorf("Input: [%s] -> Actual: [%s]. Expected: [%s]", vocS, actual, expected)
}
}
}