This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
ExtractorTest.java
370 lines (312 loc) · 15.4 KB
/
ExtractorTest.java
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package com.twitter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
public class ExtractorTest extends TestCase {
protected Extractor extractor;
public static Test suite() {
Class<?>[] testClasses = { OffsetConversionTest.class, ReplyTest.class,
MentionTest.class, HashtagTest.class, URLTest.class };
return new TestSuite(testClasses);
}
public void setUp() throws Exception {
extractor = new Extractor();
}
public static class OffsetConversionTest extends ExtractorTest {
public void testConvertIndices() {
assertOffsetConversionOk("abc", "abc");
assertOffsetConversionOk("\ud83d\ude02abc", "abc");
assertOffsetConversionOk("\ud83d\ude02abc\ud83d\ude02", "abc");
assertOffsetConversionOk("\ud83d\ude02abc\ud838\ude02abc", "abc");
assertOffsetConversionOk("\ud83d\ude02abc\ud838\ude02abc\ud83d\ude02",
"abc");
assertOffsetConversionOk("\ud83d\ude02\ud83d\ude02abc", "abc");
assertOffsetConversionOk("\ud83d\ude02\ud83d\ude02\ud83d\ude02abc",
"abc");
assertOffsetConversionOk
("\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d\ude02", "abc");
// Several surrogate pairs following the entity
assertOffsetConversionOk
("\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d\ude02\ud83d" +
"\ude02\ud83d\ude02", "abc");
// Several surrogate pairs surrounding multiple entities
assertOffsetConversionOk
("\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02", "abc");
// unpaired low surrogate (at start)
assertOffsetConversionOk
("\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02", "abc");
// unpaired low surrogate (at end)
assertOffsetConversionOk
("\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ude02\ude02", "abc");
// unpaired low and high surrogates (at end)
assertOffsetConversionOk
("\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02abc\ud83d" +
"\ude02\ud83d\ude02\ud83d\ud83d\ude02\ude02", "abc");
assertOffsetConversionOk("\ud83dabc\ud83d", "abc");
assertOffsetConversionOk("\ude02abc\ude02", "abc");
assertOffsetConversionOk("\ude02\ude02abc\ude02\ude02", "abc");
assertOffsetConversionOk("abcabc", "abc");
assertOffsetConversionOk("abc\ud83d\ude02abc", "abc");
assertOffsetConversionOk("aa", "a");
assertOffsetConversionOk("\ud83d\ude02a\ud83d\ude02a\ud83d\ude02", "a");
}
private void assertOffsetConversionOk(String testData, String patStr) {
// Build an entity at the location of patStr
final Pattern pat = Pattern.compile(patStr);
final Matcher matcher = pat.matcher(testData);
List<Extractor.Entity> entities = new ArrayList<Extractor.Entity>();
List<Integer> codePointOffsets = new ArrayList<Integer>();
List<Integer> charOffsets = new ArrayList<Integer>();
while (matcher.find()) {
final int charOffset = matcher.start();
charOffsets.add(charOffset);
codePointOffsets.add(testData.codePointCount(0, charOffset));
entities.add(new Extractor.Entity(matcher, "unused", 0, 0));
}
extractor.modifyIndicesFromUTF16ToToUnicode(testData, entities);
for (int i = 0; i < entities.size(); i++) {
assertEquals(codePointOffsets.get(i), entities.get(i).getStart());
}
extractor.modifyIndicesFromUnicodeToUTF16(testData, entities);
for (int i = 0; i < entities.size(); i++) {
// This assertion could fail if the entity location is in the middle
// of a surrogate pair, since there is no equivalent code point
// offset to that location. It would be pathological for an entity to
// start at that point, so we can just let the test fail in that case.
assertEquals(charOffsets.get(i), entities.get(i).getStart());
}
}
}
/**
* Tests for the extractReplyScreenname method
*/
public static class ReplyTest extends ExtractorTest {
public void testReplyAtTheStart() {
String extracted = extractor.extractReplyScreenname("@user reply");
assertEquals("Failed to extract reply at the start", "user", extracted);
}
public void testReplyWithLeadingSpace() {
String extracted = extractor.extractReplyScreenname(" @user reply");
assertEquals("Failed to extract reply with leading space", "user", extracted);
}
}
/**
* Tests for the extractMentionedScreennames{WithIndices} methods
*/
public static class MentionTest extends ExtractorTest {
public void testMentionAtTheBeginning() {
List<String> extracted = extractor.extractMentionedScreennames("@user mention");
assertList("Failed to extract mention at the beginning", new String[]{"user"}, extracted);
}
public void testMentionWithLeadingSpace() {
List<String> extracted = extractor.extractMentionedScreennames(" @user mention");
assertList("Failed to extract mention with leading space", new String[]{"user"}, extracted);
}
public void testMentionInMidText() {
List<String> extracted = extractor.extractMentionedScreennames("mention @user here");
assertList("Failed to extract mention in mid text", new String[]{"user"}, extracted);
}
public void testMultipleMentions() {
List<String> extracted = extractor.extractMentionedScreennames("mention @user1 here and @user2 here");
assertList("Failed to extract multiple mentioned users", new String[]{"user1", "user2"}, extracted);
}
public void testMentionWithIndices() {
List<Extractor.Entity> extracted = extractor.extractMentionedScreennamesWithIndices(" @user1 mention @user2 here @user3 ");
assertEquals(extracted.size(), 3);
assertEquals(extracted.get(0).getStart().intValue(), 1);
assertEquals(extracted.get(0).getEnd().intValue(), 7);
assertEquals(extracted.get(1).getStart().intValue(), 16);
assertEquals(extracted.get(1).getEnd().intValue(), 22);
assertEquals(extracted.get(2).getStart().intValue(), 28);
assertEquals(extracted.get(2).getEnd().intValue(), 34);
}
public void testMentionWithSupplementaryCharacters() {
// insert U+10400 before " @mention"
String text = String.format("%c @mention %c @mention", 0x00010400, 0x00010400);
// count U+10400 as 2 characters (as in UTF-16)
List<Extractor.Entity> extracted = extractor.extractMentionedScreennamesWithIndices(text);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).value, "mention");
assertEquals(extracted.get(0).start, 3);
assertEquals(extracted.get(0).end, 11);
assertEquals(extracted.get(1).value, "mention");
assertEquals(extracted.get(1).start, 15);
assertEquals(extracted.get(1).end, 23);
// count U+10400 as single character
extractor.modifyIndicesFromUTF16ToToUnicode(text, extracted);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).start, 2);
assertEquals(extracted.get(0).end, 10);
assertEquals(extracted.get(1).start, 13);
assertEquals(extracted.get(1).end, 21);
// count U+10400 as 2 characters (as in UTF-16)
extractor.modifyIndicesFromUnicodeToUTF16(text, extracted);
assertEquals(2, extracted.size());
assertEquals(3, extracted.get(0).start);
assertEquals(11, extracted.get(0).end);
assertEquals(15, extracted.get(1).start);
assertEquals(23, extracted.get(1).end);
}
}
/**
* Tests for the extractHashtags method
*/
public static class HashtagTest extends ExtractorTest {
public void testHashtagAtTheBeginning() {
List<String> extracted = extractor.extractHashtags("#hashtag mention");
assertList("Failed to extract hashtag at the beginning", new String[]{"hashtag"}, extracted);
}
public void testHashtagWithLeadingSpace() {
List<String> extracted = extractor.extractHashtags(" #hashtag mention");
assertList("Failed to extract hashtag with leading space", new String[]{"hashtag"}, extracted);
}
public void testHashtagInMidText() {
List<String> extracted = extractor.extractHashtags("mention #hashtag here");
assertList("Failed to extract hashtag in mid text", new String[]{"hashtag"}, extracted);
}
public void testMultipleHashtags() {
List<String> extracted = extractor.extractHashtags("text #hashtag1 #hashtag2");
assertList("Failed to extract multiple hashtags", new String[]{"hashtag1", "hashtag2"}, extracted);
}
public void testHashtagWithIndices() {
List<Extractor.Entity> extracted = extractor.extractHashtagsWithIndices(" #user1 mention #user2 here #user3 ");
assertEquals(extracted.size(), 3);
assertEquals(extracted.get(0).getStart().intValue(), 1);
assertEquals(extracted.get(0).getEnd().intValue(), 7);
assertEquals(extracted.get(1).getStart().intValue(), 16);
assertEquals(extracted.get(1).getEnd().intValue(), 22);
assertEquals(extracted.get(2).getStart().intValue(), 28);
assertEquals(extracted.get(2).getEnd().intValue(), 34);
}
public void testHashtagWithSupplementaryCharacters() {
// insert U+10400 before " #hashtag"
String text = String.format("%c #hashtag %c #hashtag", 0x00010400, 0x00010400);
// count U+10400 as 2 characters (as in UTF-16)
List<Extractor.Entity> extracted = extractor.extractHashtagsWithIndices(text);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).value, "hashtag");
assertEquals(extracted.get(0).start, 3);
assertEquals(extracted.get(0).end, 11);
assertEquals(extracted.get(1).value, "hashtag");
assertEquals(extracted.get(1).start, 15);
assertEquals(extracted.get(1).end, 23);
// count U+10400 as single character
extractor.modifyIndicesFromUTF16ToToUnicode(text, extracted);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).start, 2);
assertEquals(extracted.get(0).end, 10);
assertEquals(extracted.get(1).start, 13);
assertEquals(extracted.get(1).end, 21);
// count U+10400 as 2 characters (as in UTF-16)
extractor.modifyIndicesFromUnicodeToUTF16(text, extracted);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).start, 3);
assertEquals(extracted.get(0).end, 11);
assertEquals(extracted.get(1).start, 15);
assertEquals(extracted.get(1).end, 23);
}
}
/**
* Tests for the extractURLsWithIndices method
*/
public static class URLTest extends ExtractorTest {
public void testUrlWithIndices() {
List<Extractor.Entity> extracted = extractor.extractURLsWithIndices("http://t.co url https://www.twitter.com ");
assertEquals(extracted.get(0).getStart().intValue(), 0);
assertEquals(extracted.get(0).getEnd().intValue(), 11);
assertEquals(extracted.get(1).getStart().intValue(), 16);
assertEquals(extracted.get(1).getEnd().intValue(), 39);
}
public void testUrlWithoutProtocol() {
String text = "www.twitter.com, www.yahoo.co.jp, t.co/blahblah, www.poloshirts.uk.com";
assertList("Failed to extract URLs without protocol",
new String[]{"www.twitter.com", "www.yahoo.co.jp", "t.co/blahblah", "www.poloshirts.uk.com"},
extractor.extractURLs(text));
List<Extractor.Entity> extracted = extractor.extractURLsWithIndices(text);
assertEquals(extracted.get(0).getStart().intValue(), 0);
assertEquals(extracted.get(0).getEnd().intValue(), 15);
assertEquals(extracted.get(1).getStart().intValue(), 17);
assertEquals(extracted.get(1).getEnd().intValue(), 32);
assertEquals(extracted.get(2).getStart().intValue(), 34);
assertEquals(extracted.get(2).getEnd().intValue(), 47);
}
public void testURLFollowedByPunctuations() {
String text ="http://games.aarp.org/games/mahjongg-dimensions.aspx!!!!!!";
assertList("Failed to extract URLs followed by punctuations",
new String[]{"http://games.aarp.org/games/mahjongg-dimensions.aspx"},
extractor.extractURLs(text));
}
public void testUrlWithPunctuation() {
String[] urls = new String[] {
"http://www.foo.com/foo/path-with-period./",
"http://www.foo.org.za/foo/bar/688.1",
"http://www.foo.com/bar-path/some.stm?param1=foo;param2=P1|0||P2|0",
"http://foo.com/bar/123/foo_&_bar/",
"http://foo.com/bar(test)bar(test)bar(test)",
"www.foo.com/foo/path-with-period./",
"www.foo.org.za/foo/bar/688.1",
"www.foo.com/bar-path/some.stm?param1=foo;param2=P1|0||P2|0",
"foo.com/bar/123/foo_&_bar/"
};
for (String url : urls) {
assertEquals(url, extractor.extractURLs(url).get(0));
}
}
public void testUrlnWithSupplementaryCharacters() {
// insert U+10400 before " http://twitter.com"
String text = String.format("%c http://twitter.com %c http://twitter.com", 0x00010400, 0x00010400);
// count U+10400 as 2 characters (as in UTF-16)
List<Extractor.Entity> extracted = extractor.extractURLsWithIndices(text);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).value, "http://twitter.com");
assertEquals(extracted.get(0).start, 3);
assertEquals(extracted.get(0).end, 21);
assertEquals(extracted.get(1).value, "http://twitter.com");
assertEquals(extracted.get(1).start, 25);
assertEquals(extracted.get(1).end, 43);
// count U+10400 as single character
extractor.modifyIndicesFromUTF16ToToUnicode(text, extracted);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).start, 2);
assertEquals(extracted.get(0).end, 20);
assertEquals(extracted.get(1).start, 23);
assertEquals(extracted.get(1).end, 41);
// count U+10400 as 2 characters (as in UTF-16)
extractor.modifyIndicesFromUnicodeToUTF16(text, extracted);
assertEquals(extracted.size(), 2);
assertEquals(extracted.get(0).start, 3);
assertEquals(extracted.get(0).end, 21);
assertEquals(extracted.get(1).start, 25);
assertEquals(extracted.get(1).end, 43);
}
}
/**
* Helper method for asserting that the List of extracted Strings match the expected values.
*
* @param message to display on failure
* @param expected Array of Strings that were expected to be extracted
* @param actual List of Strings that were extracted
*/
protected void assertList(String message, String[] expected, List<String> actual) {
List<String> expectedList = Arrays.asList(expected);
if (expectedList.size() != actual.size()) {
fail(message + "\n\nExpected list and extracted list are differnt sizes:\n" +
" Expected (" + expectedList.size() + "): " + expectedList + "\n" +
" Actual (" + actual.size() + "): " + actual);
} else {
for (int i=0; i < expectedList.size(); i++) {
assertEquals(expectedList.get(i), actual.get(i));
}
}
}
}