-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerator_test.go
105 lines (85 loc) · 2.37 KB
/
generator_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
package randtxt
import (
"os"
"regexp"
"testing"
"github.com/pboyd/markov"
)
func TestParagraph(t *testing.T) {
chain, close := testChain(t, "testfiles/ion/trigram.mkv")
defer close()
const (
min = 3
max = 5
)
g, err := NewGenerator(chain)
if err != nil {
t.Fatalf("invalid chain: %v", err)
}
text, err := g.Paragraph(3, 5)
if err != nil {
t.Fatalf("got error %v, want nil", err)
}
matchTag := regexp.MustCompile(`/[A-Z$.:]+`)
if matchTag.MatchString(text) {
t.Errorf("text contained POS tags")
}
matchSpaceBeforePunctuation := regexp.MustCompile(` [,.?!:]`)
if matchSpaceBeforePunctuation.MatchString(text) {
t.Errorf("text contained spaces before punctuation")
}
matchLowerCaseSentenceStart := regexp.MustCompile(`[.?!] [a-z]`)
if matchLowerCaseSentenceStart.MatchString(text) {
t.Errorf("text contained lower case letters at the beginning of a sentence")
}
sentenceEndings := regexp.MustCompile(`[.?!]`).FindAllString(text, -1)
if len(sentenceEndings) < min {
t.Errorf("got %d sentences, want at least %d", len(sentenceEndings), min)
}
if len(sentenceEndings) > max {
t.Errorf("got %d sentences, want at most %d", len(sentenceEndings), max)
}
matchFullSentence := regexp.MustCompile(`[.?!]$`)
if !matchFullSentence.MatchString(text) {
t.Errorf("text did not end with a sentence ending")
}
}
func testChain(t *testing.T, path string) (chain markov.Chain, close func() error) {
t.Helper()
fh, err := os.Open(path)
if err != nil {
t.Fatalf("could not open %q: %v", path, err)
}
chain, err = markov.ReadDiskChain(fh)
if err != nil {
t.Fatalf("could not read chain from %q: %v", path, err)
}
return chain, fh.Close
}
func TestValidChain(t *testing.T) {
chain, close := testChain(t, "testfiles/ion/trigram.mkv")
defer close()
_, err := NewGenerator(chain)
if err != nil {
t.Errorf("got error for valid chain: %v", err)
}
}
func TestInvalidChains(t *testing.T) {
cases := map[string]markov.Chain{
"non-string": invalidChain(1),
"untagged": invalidChain("Paul George Ringo"),
"unknown tag": invalidChain("Paul;NN George;NN Ringo;NN"),
"just tags": invalidChain("/NN /VBZ"),
}
for desc, chain := range cases {
_, err := NewGenerator(chain)
if err == nil {
t.Errorf("%s: no error", desc)
}
}
}
func invalidChain(root interface{}) markov.Chain {
chain := markov.NewMemoryChain(1)
chain.Add(root)
return chain
}