This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTMLParser.java
249 lines (226 loc) · 8.7 KB
/
HTMLParser.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class HTMLParser {
// Holds parsed array.
private ArrayList<String> parsedArray = new ArrayList<String>();
// Replaces unwanted characters and spaces. Used to create parsed ArrayList
// from string.
private static final String SPLIT_CODE = "<!split!>";
// Constructor, parses the passed in html file
public HTMLParser(String file) {
File html = new File(file);
parseHTML(html);
}
// Called in constructor, takes a file and creates a parsed ArrayList of words
private void parseHTML(File file) {
// Stores all contents of file in a single string
String fullString = fileToString(file);
// Turns the string into an array of words, split non accepted
// characters
ArrayList<String> parsedArray = splitToArray(fullString);
// Removes empty strings
parsedArray = removeEmptyEntries(parsedArray);
// Makes all words in array lowercase
parsedArray = arrayToLowercase(parsedArray);
// Removes duplicate entries
// parsedArray = removeDuplicates(parsedArray);
// Removes html commands/tags
parsedArray = removeHTMLCommands(parsedArray);
// Sorts the array alphanumerically
//parsedArray = sortAlphanumeric(parsedArray);
this.parsedArray = parsedArray;
}
// Returns parsed array
public ArrayList<String> getParsedArray() {
return parsedArray;
}
// Prints the parsed array
public void printArray() {
System.out.println("WORDS");
for (String i : parsedArray)
System.out.println(i);
// Prints the number of unique words
System.out.println("\nNUMBER OF WORDS: " + parsedArray.size());
}
// Adds a word to the parsed array.
public void addToArray(String string) {
// Format string
string = formatString(string);
// Check if string already exists in array
if (!parsedArray.contains(string)) {
// Binary search for position to add string
parsedArray.add(binarySearch(string, parsedArray), string);
}
}
private int binarySearch(String string, ArrayList<String> array) {
int currentPos = array.size()-1 / 2;
int min = 0;
int max = array.size()-1;
// Returns the location that string will go in the array
return binarySearchTail(array, string, min, max);
}
// Binary search to find where to place the given string an ArrayList
public int binarySearchTail (ArrayList<String> word, String key,int first, int last){
int mid = first + ((last-first)/2);
if (word.size() == 0){
return 0;
}
if (first>last){
return mid;
}
if (key.equals(word.get(mid))){
return mid;
}
if(word.get(mid).compareTo(key)>0){
return binarySearchTail(word, key, first, mid-1);
}
if(word.get(mid).compareTo(key)<0){
return binarySearchTail(word,key,mid+1, last);
}
else{
return mid;
}
}
// Removes all non letter/number characters and makes string lowercase
private String formatString(String string) {
char[] characterArray = string.toLowerCase().toCharArray();
String formatted = "";
for (char c : characterArray) {
// Tests numbers 0-9
if (c >= 48 && c <= 57)
formatted += c;
// Tests letters a-z
if (c >= 97 && c <= 122)
formatted += c;
// Tests apostrophe
if (c == 39)
formatted += c;
}
return formatted.trim();
}
// Takes a file, creates a string of its contents
private String fileToString(File file) {
// Stores the contents of the file
String fullFile = "";
// Creates fullFile String
try {
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
fullFile += in.nextLine() + "\n";
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("Error: File not found.");
System.exit(0);
}
return fullFile.trim();
}
// Removes HTML takes from an ArrayList of words
private ArrayList<String> removeHTMLCommands(ArrayList<String> array) {
for (int i = 0; i < array.size(); i++) {
if (array.get(i).contains("<") || array.get(i).contains(">")) {
array.remove(i);
i--;
}
}
return array;
}
// Splits a string into an ArrayList of words
private ArrayList<String> splitToArray(String full) {
// Takes a string and breaks it into a String[] by words, skips HTML
// tags
// Iterate through each character in the string
boolean skipping = false;
for (int i = 0; i < full.length(); i++) {
// If the character is NOT either a letter or a number or an
// apostrophe, set true to replace with SPLIT_CODE
boolean characterPasses = false;
// Tests numbers 0-9
if (full.charAt(i) >= 48 && full.charAt(i) <= 57)
characterPasses = true;
// Tests letters A-Z
if (full.charAt(i) >= 65 && full.charAt(i) <= 90)
characterPasses = true;
// Tests letters a-z
if (full.charAt(i) >= 97 && full.charAt(i) <= 122)
characterPasses = true;
// Tests apostrophe
if (full.charAt(i) == 39)
characterPasses = true;
// Tests left-angle bracket(begins skipping for HTML tag)
if (full.charAt(i) == 60) {
characterPasses = true;
full = changeCharacterAtPosition(i, full, SPLIT_CODE + "<");
i += SPLIT_CODE.length();
skipping = true;
}
if (characterPasses == false && skipping == false) {
// Swaps the character with a split code if it is not a
// "passing" character
full = changeCharacterAtPosition(i, full, SPLIT_CODE);
// Advances the character check to pass over the newly created
// split code.
i += SPLIT_CODE.length() - 1;
}
// Tests left-angle bracket(ends skipping for HTML tag)
if (full.charAt(i) == 62) {
skipping = false;
full = changeCharacterAtPosition(i, full, ">" + SPLIT_CODE);
i += SPLIT_CODE.length();
}
}
// Splits the array
String[] split = full.split(SPLIT_CODE);
return convertToArrayList(split);
}
// Removes empty strings in an array
private ArrayList<String> removeEmptyEntries(ArrayList<String> array) {
// ArrayList to hold non-empty strings
ArrayList<String> temporaryArray = new ArrayList<String>();
// Populates ArrayList
for (String s : array)
if (!s.equals(""))
temporaryArray.add(s);
return temporaryArray;
}
// Converts all entries in an ArrayList to lowercase
private ArrayList<String> arrayToLowercase(ArrayList<String> array) {
for (int i = 0; i < array.size(); i++) {
array.set(i, array.get(i).toLowerCase().trim());
}
return array;
}
// Replaces a character in a string with another string
private String changeCharacterAtPosition(int pos, String str, String rep) {
String alteredString = str.substring(0, pos) + rep
+ str.substring(pos + 1, str.length());
return alteredString;
}
// Removes duplicates in an array of strings
private ArrayList<String> removeDuplicates(ArrayList<String> fullArray) {
// ArrayList to hold non-repeated words
ArrayList<String> originals = new ArrayList<String>();
// Populates ArrayList
for (String s : fullArray)
if (!originals.contains(s))
originals.add(s);
return originals;
}
// Sorts an ArrayList alphabetically
private ArrayList<String> sortAlphanumeric(ArrayList<String> array) {
ArrayList<String> sorted = new ArrayList<String>();
for (String s : array) {
sorted.add(binarySearch(s, sorted), s);
}
return sorted;
}
// Converts a String[] to an ArrayList
private ArrayList<String> convertToArrayList(String[] array) {
ArrayList<String> convert = new ArrayList<String>();
for (String i : array)
convert.add(i);
return convert;
}
}