-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatLine.java
More file actions
278 lines (227 loc) · 11.3 KB
/
FormatLine.java
File metadata and controls
278 lines (227 loc) · 11.3 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
277
278
/*
* Programmer: Dan Hopp
* Date: 27-FEB-2020
* Description: Via the WebCrawl, parse the line by:
Trimming the spaces.
Remove the HTML tags. If it's an "only-HTML" line (Scripts and codeing),
then remove all text.
Remove any commas, numbers, or special characters.
Remove any common or unwanted words.
Split the formatted string by the spaces and add each word to the Word Object
array. If the word already exists, up the word's count by 1.
(To make the split string less of a mess, I thought about making a function
to remove extra spaces in-between the words. But, one line to check the
content's length is smaller than say 12 or so lines of code)
*/
package lab3;
class FormatLine {
//List of common words to remove
public static final String COMMON_WORDS_REGEX = "( a | the | in | on | for"
+ " | with | and | or | you | i | your | my | if | as | are | can"
+ " | of | at | is | to | be | it | its | they | his | her | has"
+ " | was | by | that | this | we | not | us | our | ours | from"
+ " | so | he | hers | also | st | nd | rd | th | no | yes | will"
+ " | have | which | use | an"
+ " | copyright | copy | all | rights | reserved | retrieved"
+ " | https | com | www | txt | http | function | return | var"
+ " | length | else | null | true | data | false | test | nodetype"
+ " | font | px | edu | line | lato | size | li"
+ " | event | type | typeof | style | call | nodename | parentnode"
+ " | url | body | jquery | href | mm | mw | output | parser | cs"
+ " | width | left | br | nbsp | utm | amp | touchenabled | true"
+ " | responsive | top | gt | lt | raquo | laquo | bull | mdash"
+ " | quot | uri | wc | iri | org | larr | uarr | rarr | harr"
+ " | hellip | darr )";
/*For whatever reason, \p{P} will not remove <, =, +, or >.
For sure it doesn't remove | */
public static final String REGEX_PUNCT_ODDITY = "[<=>|+]";
public static final String REGEX_HTML_ENTITIES = "(\\ |\\…"
+ "|\\←|\\↑|\\→|\\↔|\\↓|\\>|\\<"
+ "|\\&|\\"|\\'|\\¢|\\£|\\¥"
+ "|\\€|\\©|\\®"
+ "|\\&#\\d{4};)";
/*Remove HTML tags, punctuation, common words, numbers, and trim blank
spaces from the string*/
static String scrubString(String rawText){
//Remove any spaces on the ends
rawText = rawText.trim();
//Change text to lowercase
rawText = rawText.toLowerCase();
//Remove HTML tags and code
rawText = removeHTML(rawText);
//Remove punctuation
rawText = rawText.replaceAll("\\p{P}", " ");
//Remove numbers
rawText = rawText.replaceAll("[0-9]", "");
//Remove the characters \p{P} missed
rawText = rawText.replaceAll(REGEX_PUNCT_ODDITY, " ");
//Clean up line if previous replacements removed all data
rawText = rawText.trim();
//If the entire line wasn't all HTML or code, remove the common words
if (rawText.length() > 0) {
rawText = removeCommonWords(rawText);
}
//One last trim
rawText = rawText.trim();
return rawText;
}
/*Remove all things HTML. If the line has <script>, ignore. Remove basic tags by
finding the start and end indexes of the <>'s:
<footer>Das Foot</footer> = Das Foot
Remove HTML entities such as → etc
If the line is only HTML code with no HTML tags, ignore.
*/
static String removeHTML(String rawText){
StringBuilder formattedString = new StringBuilder(rawText);
//If line is <script>, remove
if (rawText.matches(".*script>.*")){
rawText = "";
}
/*Remove as many HTML tags from the line as possible,
until no more </ are found. Contians a counter to exit incase of an
infinte loop. Yes, somewere out there in the net, there's a line that
has:
searchText = searchText.replace(/</gi, "< "); */
int loopcount = 0;
while (rawText.matches(".*</.+")){
++loopcount;
//Get index number of <
int firstIndex = rawText.indexOf('<');
//Get index number of >
int lastIndex = rawText.indexOf('>');
/*If the first index is less than the last index, insert a blank
space so the formatted string will not have
Clumped up text.Such as thisline.*/
if (firstIndex < lastIndex){
formattedString.replace(firstIndex, lastIndex + 1, " ");
}
/*Else delete everything to the left of the first index, inclusive
(in case there's a line such as:
style=\"display:none;width:0px;height:0px\"></iframe>) */
else {
formattedString.replace(0, firstIndex, " ");
}
/*Put StringBuilder back into a String so subsequent RexEx's
will work */
rawText = formattedString.toString();
if (loopcount > 5000){
break;
}
}
//Reduce string builder storage size, just in case
formattedString.trimToSize();
//Remove HTML Character Entities
rawText = replaceAllLoop(rawText, REGEX_HTML_ENTITIES);
//Cleanup
rawText = rawText.trim();
/*What if the line is just HTML with no end tag? ie:
<ul class="ldst__banner clearfix">
Or, what if the line is something like:
var cookie_suffix = '';
Or, any other funky HTML syntaxes in the line?
If there are, then return "" */
if (rawText.length() > 0) {
//Get last character
String lastChar = rawText.charAt(rawText.length() - 1) + "";
if (rawText.startsWith("<")
//Codeing operands or assignemnts
|| lastChar.matches(REGEX_PUNCT_ODDITY)
|| lastChar.matches("\\p{P}")
//Comments
|| rawText.matches(".*//.*")
//Unwanted file extentions or HTML no-page/moved messages
|| !WebCrawl.isHtmlValid(rawText)){
rawText = "";
}
}
return rawText;
}
/*Function to remove common words and non-breaking space characters. Repeat
the replaceAll until no more concurrent common words are in the string*/
static String removeCommonWords(String rawText){
rawText = replaceAllLoop(rawText, COMMON_WORDS_REGEX);
//Remove non-breaking space characters
rawText = rawText.replace('\u00A0',' ');
//Check for "bookend" common words in the string
rawText = removeBookendCommonWords(rawText, COMMON_WORDS_REGEX);
return rawText;
}
/*Loop to match a word and replace it with a space, until there are no more
words to replace*/
static String replaceAllLoop(String rawText, String finalString){
/*Inserting a blank space so the formatted string will not have
Clumped up text.Such as thisLINE. */
while (rawText.matches(".*" + finalString + ".*")){
rawText = rawText.replaceAll(finalString, " ");
}
return rawText;
}
/*What if a common word is at the beginning of a line with no spaces
before it? Or at the end with no spaces after?*/
static String removeBookendCommonWords(String rawText, String finalString){
//Prep COMMON_WORDS_REGEX for array split
String commonWordListPrep = finalString.replaceAll("[(|)]", "");
String[] commonWordsArray = commonWordListPrep.split(" ");
//Loop through the array
for (int i = 0; i < commonWordsArray.length; i++){
//Get the common word's length
int commWordLength = commonWordsArray[i].length();
//See if the common word's length is the same as rawText's
boolean isCommWrdLenEqToTxtLn = (rawText.length() == commWordLength);
//Skip spaces/blank items in the array
if (commWordLength > 0) {
/*If the rawText's length is equal to or greater than the common
word, continue*/
if (rawText.length() >= commWordLength){
//Check the left bookend of rawText
if (rawText.startsWith(commonWordsArray[i])){
/*Make sure the character after the word in rawText is a
space, to avoid changing something like "to" in
"towards"*/
if(!isCommWrdLenEqToTxtLn){
if (rawText.charAt(commWordLength) == ' '){
rawText = rawText.replaceFirst
(commonWordsArray[i], " ");
}
}
else{
//Remove common word
rawText = rawText.replaceFirst
(commonWordsArray[i], " ");
}
}
//Check the right bookend of rawText
if (rawText.endsWith(commonWordsArray[i])){
if(!isCommWrdLenEqToTxtLn){
/*Make sure the character before the word in rawText
is a space, to avoid changing something like "by" in
"willoughby"*/
if(rawText.charAt((rawText.length() - commWordLength)
- 1 ) == ' '){
//Call function
rawText = replaceRightBookend(rawText,
commonWordsArray[i], commWordLength);
}
}
else{
//Call function
rawText = replaceRightBookend(rawText,
commonWordsArray[i], commWordLength);
}
}
}
}
} //End of for loop
return rawText;
}
/*replaceFirst will not properly replace the 2nd "by" in "Willoughby by".
Changing string to stringBuilder so word can be replaced via string indexes*/
static String replaceRightBookend(String rawText, String commonWord,
int commWordLength){
StringBuilder tempStrBuilder = new StringBuilder(rawText);
tempStrBuilder.replace(rawText.length() - commWordLength,
rawText.length(), " ");
rawText = tempStrBuilder.toString();
return rawText;
}
}