forked from twitter-archive/twitter-text-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractor.java
More file actions
247 lines (209 loc) · 6.97 KB
/
Copy pathExtractor.java
File metadata and controls
247 lines (209 loc) · 6.97 KB
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
package com.twitter;
import java.util.*;
import java.util.regex.*;
/**
* A class to extract usernames, lists, hashtags and URLs from Tweet text.
*/
public class Extractor {
public static class Entity {
protected Integer start = null;
protected Integer end = null;
protected String value = null;
protected String type = null;
public Entity(Matcher matcher, String valueType, Integer groupNumber) {
// Offset -1 on start index to include @, # symbols for mentions and hashtags
this(matcher, valueType, groupNumber, -1);
}
public Entity(Matcher matcher, String valueType, Integer groupNumber, int startOffset) {
this.start = matcher.start(groupNumber) + startOffset; // 0-indexed.
this.end = matcher.end(groupNumber);
this.value = matcher.group(groupNumber);
this.type = valueType;
}
/** Constructor used from conformance data */
public Entity(Map<String, Object> config, String valueType) {
this.type = valueType;
this.value = (String)config.get(valueType);
List<Integer> indices = (List<Integer>)config.get("indices");
this.start = indices.get(0);
this.end = indices.get(1);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Entity)) {
System.out.println("incorrect type");
return false;
}
Entity other = (Entity)obj;
if (this.type.equals(other.type) &&
this.start.equals(other.start) &&
this.end.equals(other.end) &&
this.value.equals(other.value)) {
return true;
} else {
return false;
}
}
public int hashCode() {
return this.type.hashCode() + this.value.hashCode() + this.start + this.end;
}
public Integer getStart() {
return start;
}
public Integer getEnd() {
return end;
}
public String getValue() {
return value;
}
public String getType() {
return type;
}
}
/**
* Create a new extractor.
*/
public Extractor() {
}
/**
* Extract @username references from Tweet text. A mention is an occurance of @username anywhere in a Tweet.
*
* @param text of the tweet from which to extract usernames
* @return List of usernames referenced (without the leading @ sign)
*/
public List<String> extractMentionedScreennames(String text) {
if (text == null) {
return null;
}
List<String> extracted = new ArrayList<String>();
Matcher matcher = Regex.EXTRACT_MENTIONS.matcher(text);
while (matcher.find()) {
if (! Regex.SCREEN_NAME_MATCH_END.matcher(matcher.group(Regex.EXTRACT_MENTIONS_GROUP_AFTER)).matches()) {
extracted.add(matcher.group(Regex.EXTRACT_MENTIONS_GROUP_USERNAME));
}
}
return extracted;
}
/**
* Extract @username references from Tweet text. A mention is an occurance of @username anywhere in a Tweet.
*
* @param text of the tweet from which to extract usernames
* @return List of usernames referenced (without the leading @ sign)
*/
public List<Entity> extractMentionedScreennamesWithIndices(String text) {
if (text == null) {
return null;
}
List<Entity> extracted = new ArrayList<Entity>();
Matcher matcher = Regex.EXTRACT_MENTIONS.matcher(text);
while (matcher.find()) {
if (! Regex.SCREEN_NAME_MATCH_END.matcher(matcher.group(Regex.EXTRACT_MENTIONS_GROUP_AFTER)).matches()) {
extracted.add(new Entity(matcher, "mention", Regex.EXTRACT_MENTIONS_GROUP_USERNAME));
}
}
return extracted;
}
/**
* Extract a @username reference from the beginning of Tweet text. A reply is an occurance of @username at the
* beginning of a Tweet, preceded by 0 or more spaces.
*
* @param text of the tweet from which to extract the replied to username
* @return username referenced, if any (without the leading @ sign). Returns null if this is not a reply.
*/
public String extractReplyScreenname(String text) {
if (text == null) {
return null;
}
Matcher matcher = Regex.EXTRACT_REPLY.matcher(text);
if (matcher.matches()) {
return matcher.group(Regex.EXTRACT_REPLY_GROUP_USERNAME);
} else {
return null;
}
}
/**
* Extract URL references from Tweet text.
*
* @param text of the tweet from which to extract URLs
* @return List of URLs referenced.
*/
public List<String> extractURLs(String text) {
if (text == null) {
return null;
}
List<String> urls = new ArrayList<String>();
Matcher matcher = Regex.VALID_URL.matcher(text);
while (matcher.find()) {
urls.add(matcher.group(Regex.VALID_URL_GROUP_URL));
}
return urls;
}
/**
* Extract URL references from Tweet text.
*
* @param text of the tweet from which to extract URLs
* @return List of URLs referenced.
*/
public List<Entity> extractURLsWithIndices(String text) {
if (text == null) {
return null;
}
List<Entity> urls = new ArrayList<Entity>();
Matcher matcher = Regex.VALID_URL.matcher(text);
while (matcher.find()) {
urls.add(new Entity(matcher, "url", Regex.VALID_URL_GROUP_URL, 0));
}
return urls;
}
/**
* Extract #hashtag references from Tweet text.
*
* @param text of the tweet from which to extract hashtags
* @return List of hashtags referenced (without the leading # sign)
*/
public List<String> extractHashtags(String text) {
if (text == null) {
return null;
}
return extractList(Regex.AUTO_LINK_HASHTAGS, text, Regex.AUTO_LINK_HASHTAGS_GROUP_TAG);
}
/**
* Extract #hashtag references from Tweet text.
*
* @param text of the tweet from which to extract hashtags
* @return List of hashtags referenced (without the leading # sign)
*/
public List<Entity> extractHashtagsWithIndices(String text) {
if (text == null) {
return null;
}
return extractListWithIndices(Regex.AUTO_LINK_HASHTAGS, text, Regex.AUTO_LINK_HASHTAGS_GROUP_TAG, "hashtag");
}
/**
* Helper method for extracting multiple matches from Tweet text.
*
* @param pattern to match and use for extraction
* @param text of the Tweet to extract from
* @param groupNumber the capturing group of the pattern that should be added to the list.
* @return list of extracted values, or an empty list if there were none.
*/
private List<String> extractList(Pattern pattern, String text, int groupNumber) {
List<String> extracted = new ArrayList<String>();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
extracted.add(matcher.group(groupNumber));
}
return extracted;
}
// TODO: Make this a real object, not a Map
private List<Entity> extractListWithIndices(Pattern pattern, String text, int groupNumber, String valueType) {
List<Entity> extracted = new ArrayList<Entity>();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
extracted.add(new Entity(matcher, valueType, groupNumber));
}
return extracted;
}
}