-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTokenizer.java
More file actions
276 lines (235 loc) · 8.84 KB
/
Tokenizer.java
File metadata and controls
276 lines (235 loc) · 8.84 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
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.util.HashSet;
class Tokenizer {
private HashSet<String> stopwords;
private HashMap<String, Integer> tokenFreqs;
public Tokenizer()
{
stopwords = new HashSet<String>();
tokenFreqs = new HashMap<String, Integer>();
}
public void setStopwordList(HashSet<String> stopwordsSet)
{
stopwords.addAll(stopwordsSet);
}
// Add the token to the token frequencies HashMap
private void addToken(String token)
{
//Stemmer class taken from http://tartarus.org/martin/PorterStemmer/
Stemmer porterStemmer = new Stemmer();
porterStemmer.add(token.toCharArray(), token.length());
porterStemmer.stem();
String stemmedToken = porterStemmer.toString();
if (tokenFreqs.containsKey(stemmedToken))
{
tokenFreqs.put(stemmedToken, tokenFreqs.get(stemmedToken)+1);
} else {
tokenFreqs.put(stemmedToken, 1);
}
}
private String getEmailAddressesFromString(String stringToCheck)
{
String returnString = stringToCheck;
// regex for email addresses
String r = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
Pattern p = Pattern.compile(r);
Matcher m = p.matcher(stringToCheck);
while (m.find())
{
String email = m.group(0);
addToken(email);
returnString = returnString.replaceFirst(email, "");
}
return returnString;
}
// returns HashMap of {token -> frequency}
// 1. remove whitespace seperated capital letter chaines
// a. if its chain at end of line, dont add it wait to check next line
// b. if chain is at start of line and last line had chain then first combine them
// c. otherwise just add the token
// 2. remove email addresses
// 3. split rest of tokens on whitespace and punctuation
// 4.
public HashMap<String,Integer> tokenize(ArrayList<String> lines)
{
boolean lineEndedWithCapitalLetterChain = false;
boolean currentLineEndedWithCapitalLetterChain = false;
boolean addCurrentToken = true;
boolean lineEndedWithHyphen = false;
boolean firstWordNeedsJoining = false;
boolean isAStopword = false;
boolean specialCheckUpperCaseWord = false;
String endOfLineCapitalLetterChain = "";
String curentEndOfLineCapitalLetterChain = "";
String hyphenatedLastWord = "";
int totalLines = lines.size();
int lineNumber = 1;
Iterator<String> it = lines.iterator();
while (it.hasNext())
{
String line = it.next(); //get the next line
///////////////////////////////////////////////////////////////////////////////////////
//Now check for two or more words seperated by whitespace beginning with capital letters
//regex for capital letter chain
String capitalLetterChainRegex = "([A-Z][\\w-]*(?:\\s+[A-Z][\\w-]*)+)";
Pattern p = Pattern.compile(capitalLetterChainRegex);
Matcher m = p.matcher(line);
while (m.find())
{
String capitalLetterChain = m.group(0);
// store the chain we want to remove from the line seperateley because we may
// alter the chain if its at the start of the line and needed to be appeneded
// to the end of the previous line
String chainToRemove = capitalLetterChain;
if (line.startsWith(capitalLetterChain) && lineEndedWithCapitalLetterChain)
{
if (endOfLineCapitalLetterChain.endsWith("-"))
{
// if end of letter chain ended with hyphen, remove it before adding the starting capital
// letter chain
capitalLetterChain = endOfLineCapitalLetterChain.substring(0,endOfLineCapitalLetterChain.length()-1)+capitalLetterChain;
} else {
capitalLetterChain = endOfLineCapitalLetterChain + " " + capitalLetterChain;
}
// taken care of appending to the hyphen
lineEndedWithCapitalLetterChain = false;
}
//if this chain is at end of line and contains hyphen
if (line.endsWith(capitalLetterChain))
{
currentLineEndedWithCapitalLetterChain = true;
curentEndOfLineCapitalLetterChain = capitalLetterChain;
if (curentEndOfLineCapitalLetterChain.endsWith("-"))
{
firstWordNeedsJoining = true;
}
addCurrentToken = false;
} else {
currentLineEndedWithCapitalLetterChain = false;
addCurrentToken = true;
}
if (totalLines == 1)
{
addCurrentToken = true;
}
if (addCurrentToken)
{
addToken(capitalLetterChain);
}
//remove the original chain from the line for rest of process
line = line.replaceFirst(chainToRemove, "");
}
/////////////////////////////////////////////////////////
// END capital letter seperated by whitespace processing
//////////////////////////////////////////////////////////
//
//Second check for email addresses and remove them and add them if they exist
//
line = getEmailAddressesFromString(line);
//
//
///////////////////////////////////////////////////////////////
// For all other text split into tokes using as delimeter whitespace
// or subset of punctuation
String[] words = line.trim().split("[ {.,:;\"\'()?!}]+");
addCurrentToken = true; // set this to true after using it for capital letter chains
int i = 1; // word count in the line
int numbWords = words.length; //number words in current line
for (String word : words)
{
if (word.length() > 0)
{
////////////////////
// First do some checks
////////////////////
// Check if we are at the end of the line and word has hyphen at end of it
if (i == numbWords)
{
if (word.endsWith("-"))
{
firstWordNeedsJoining = true;
lineEndedWithHyphen = true;
hyphenatedLastWord = word;
} else {
firstWordNeedsJoining = false;
lineEndedWithHyphen = false;
hyphenatedLastWord = "";
}
if (Character.isUpperCase(word.charAt(0)))
{
curentEndOfLineCapitalLetterChain = word;
currentLineEndedWithCapitalLetterChain = true;
specialCheckUpperCaseWord = true;
} else {
specialCheckUpperCaseWord = false;
}
}
// Is a stopword
if (stopwords.contains(word))
{
isAStopword = true;
} else {
isAStopword = false;
}
if (isAStopword || lineEndedWithHyphen || specialCheckUpperCaseWord)
{
addCurrentToken = false;
} else {
addCurrentToken = true;
}
////////////////////
// END preliminary checks
////////////////////
// for special case where last word was with a hyphen and now we
// are at the first word, join it to the last word
if ((i == 1) && firstWordNeedsJoining)
{
if (lineEndedWithCapitalLetterChain && endOfLineCapitalLetterChain.endsWith("-"))
{
endOfLineCapitalLetterChain = endOfLineCapitalLetterChain.substring(0,endOfLineCapitalLetterChain.length()-1)+word;
addCurrentToken = false;
} else if (hyphenatedLastWord.length() > 0) {
word = hyphenatedLastWord.substring(0,hyphenatedLastWord.length()-1)+word;
addCurrentToken = true;
}
lineEndedWithHyphen = false;
}
//for special case of single capital word that had a capital word before it that needed to
//be joined to the first word of this line because of hyphen
if ((i == 2) && specialCheckUpperCaseWord && Character.isUpperCase(word.charAt(0)))
{
word = endOfLineCapitalLetterChain+ " " +word;
addCurrentToken = true;
specialCheckUpperCaseWord = false;
}
// OK now add the word if there is nothing stopping it
if (addCurrentToken)
{
addToken(word);
}
addCurrentToken = true;
// increment word count within the current line
i++;
}
}
///////////////////////////////////////////////////////////
/// END other token adding
////////////////////////////////////////////////////////////
if (currentLineEndedWithCapitalLetterChain)
{
lineEndedWithCapitalLetterChain = true;
currentLineEndedWithCapitalLetterChain = false;
endOfLineCapitalLetterChain = curentEndOfLineCapitalLetterChain;
}
lineNumber++;
}
return tokenFreqs;
}
}