This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_test.go
160 lines (145 loc) · 3.91 KB
/
extract_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
package text
import (
"io/ioutil"
"reflect"
"testing"
"launchpad.net/goyaml"
)
type conformanceSuite struct {
Tests struct {
Hashtags []struct {
Description string
Text string
Expected []string
}
HashtagIndices []struct {
Description string
Text string
Expected []struct {
Hashtag string
Indices []int
}
} `yaml:"hashtags_with_indices"`
URLs []struct {
Description string
Text string
Expected []string
}
URLIndices []struct {
Description string
Text string
Expected []struct {
URL string
Indices []int
}
} `yaml:"urls_with_indices"`
}
}
var conformance conformanceSuite
func init() {
data, err := ioutil.ReadFile("conformance/extract.yml")
if err != nil {
panic(err)
}
if err := goyaml.Unmarshal(data, &conformance); err != nil {
panic(err)
}
}
func TestExtractHashtags(t *testing.T) {
for _, test := range conformance.Tests.Hashtags {
res := Extract(test.Text, URLsAndHashtags).Hashtags // extract urls too so that overlapping entities are ignored
if len(test.Expected) == 0 && len(res) == 0 {
continue
}
hashtags := make([]string, len(res))
for i, m := range res {
hashtags[i] = m.Text
}
if !reflect.DeepEqual(hashtags, test.Expected) {
t.Errorf("%s: want %v, got %v", test.Description, test.Expected, res)
}
}
}
func TestExtractHashtagIndices(t *testing.T) {
for _, test := range conformance.Tests.HashtagIndices {
res := ExtractHashtagMatches(test.Text)
if len(test.Expected) != len(res) {
t.Errorf("%s: want %v, got %v", test.Description, test.Expected, res)
continue
}
for i, expected := range test.Expected {
ei := unicodeToByteOffset(test.Text, [2]int{expected.Indices[0], expected.Indices[1]})
if res[i].Text != expected.Hashtag || res[i].Indices[0] != ei[0] || res[i].Indices[1] != ei[1] {
t.Errorf("%s: [%d] want %v, got {%s %v}", test.Description, i, expected, res[i].Text, ei)
}
}
}
}
var skipURLTests = map[int]bool{
32: true, 33: true, 34: true, // CJK surrounded without protocol
65: true, 66: true, // special t.co extraction
26: true, // broken: https://github.com/twitter/twitter-text-conformance/pull/73
}
func TestExtractURLs(t *testing.T) {
for i, test := range conformance.Tests.URLs {
if skipURLTests[i] {
continue
}
res := ExtractURLs(test.Text)
if len(test.Expected) == 0 && len(res) == 0 {
continue
}
if !reflect.DeepEqual(res, test.Expected) {
t.Errorf("%s: [%d] got %#v, want %#v", test.Description, i, res, test.Expected)
}
}
}
var skipURLIndexTests = map[int]bool{
3: true, 4: true, // CJK surrounded without protocol
5: true, // special t.co extraction
8: true, // contains unassigned idn tld
}
func TestExtractURLIndices(t *testing.T) {
for i, test := range conformance.Tests.URLIndices {
if skipURLIndexTests[i] {
continue
}
res := ExtractURLMatches(test.Text)
if len(test.Expected) != len(res) {
t.Errorf("%s: [%d] want %v, got %v", test.Description, i, test.Expected, res)
continue
}
for j, expected := range test.Expected {
ei := unicodeToByteOffset(test.Text, [2]int{expected.Indices[0], expected.Indices[1]})
if res[j].Text != expected.URL || res[j].Indices[0] != ei[0] || res[j].Indices[1] != ei[1] {
t.Errorf("%s: [%d-%d] want %v, got {%s %v}", test.Description, i, j, expected, res[j].Text, ei)
}
}
}
}
func BenchmarkExtractHashtags(b *testing.B) {
for i := 0; i < b.N; i++ {
ExtractHashtagMatches("Getting my Oktoberfest on #münchen")
}
}
func BenchmarkExtractURLs(b *testing.B) {
for i := 0; i < b.N; i++ {
ExtractURLMatches("Extract valid URL: http://google.com/#search?q=iphone%20-filter%3Alinks")
}
}
func unicodeToByteOffset(s string, charIdx [2]int) (byteIdx [2]int) {
var j int
for i := range s {
j++
if j == charIdx[0]+1 {
byteIdx[0] = i
} else if j > charIdx[1] {
byteIdx[1] = i
break
}
}
if byteIdx[1] == 0 {
byteIdx[1] = len(s)
}
return
}