File: porterstemmer_stem_string_test.go

package info (click to toggle)
golang-github-blevesearch-go-porterstemmer 1.0.1%2Bgit20141230.9.23a2c8e-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 188 kB
  • sloc: makefile: 2
file content (119 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download | duplicates (2)
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
package porterstemmer

import (
	"bufio"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"testing"
)

func TestStemString(t *testing.T) {
	t.Skip("Skipping tests that attempts to access network resources.")

	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)
		}
	}
}