-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordFrequency.java
More file actions
362 lines (344 loc) · 12.5 KB
/
WordFrequency.java
File metadata and controls
362 lines (344 loc) · 12.5 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
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
import java.util.*;
import java.io.*;
import java.lang.*;
public class WordFrequency
{
//private ArrayList<WordPair> list = new ArrayList<WordPair>();
private Map<String, Integer> map = new LinkedHashMap<String, Integer>();
private int threshold = 0;
public static void main(String[] args)
{
WordFrequency hw = new WordFrequency();
if(args.length==0||args.length==1)
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
else if(args.length==2)
{
if(args[0].equals("--by-freq"))
{
try
{
File readIn = new File(args[1]);
hw.storeInfo(readIn);
hw.sortByFreq();
}
catch(IOException e)
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else if(args[0].equals("--by-word"))
{
try
{
File readIn = new File(args[1]);
hw.storeInfo(readIn);
hw.sortByWord();
}
catch(IOException e)
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else if(args[0].equals("--by-orig"))
{
try
{
File readIn = new File(args[1]);
hw.storeInfo(readIn);
hw.sortByOrig();
}
catch(IOException e)
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else if(args.length==3)
{
if(args[0].equals("--by-freq"))
{
String[] threshold = args[1].split("=");
if(threshold.length==2&&threshold[0].equals("--threshold"))
{
hw.setThreshold(Integer.parseInt(threshold[1]));
try
{
File readIn = new File(args[2]);
hw.storeInfo(readIn);
hw.sortByFreq();
}
catch(IOException e)
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else if(args[0].equals("--by-word"))
{
String[] threshold = args[1].split("=");
if(threshold.length==2&&threshold[0].equals("--threshold"))
{
hw.setThreshold(Integer.parseInt(threshold[1]));
try
{
File readIn = new File(args[2]);
hw.storeInfo(readIn);
hw.sortByWord();
}
catch(IOException e)
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else if(args[0].equals("--by-orig"))
{
String[] threshold = args[1].split("=");
if(threshold.length==2&&threshold[0].equals("--threshold"))
{
//System.out.println("I am here");
hw.setThreshold(Integer.parseInt(threshold[1]));
try
{
File readIn = new File(args[2]);
hw.storeInfo(readIn);
hw.sortByOrig();
}
catch(IOException e)
{
//System.out.println("isItException?");
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
else
{
System.err.println("Error: Mode and filename expected");
System.out.println("Usage: java WordFrequency <MODE> [--threshold=NUM] <TEXTFILE>");
System.out.println("Utility to count the occurences of each word in a text file.");
System.out.println("MODE is one of:");
System.out.println(" --by-freq sort by frequency count, from highest to lowest");
System.out.println(" --by-word sort by words, alphabetically");
System.out.println(" --by-orig show words in order of first appearance");
}
}
}
/*
* Cite: Book
* Title: Introduction to JAVA programming comprehensive version Tenth Edition
* Author: Y.Daniel Liang
* Page: 815
* Citation: Line 235-249
*
*
*/
public void storeInfo(File readIn) throws IOException
{
Scanner in = new Scanner(readIn);
in.useDelimiter("[^A-Za-z']+");
while(in.hasNext())
{
String text = in.next();
String word = text.toLowerCase();
if(word.length()>0)
{
if(!this.map.containsKey(word))
{
this.map.put(word,1);
}
else
{
int value = this.map.get(word);
value++;
this.map.put(word,value);
}
}
}
}
public void sortByOrig()
{
Set<Map.Entry<String, Integer>> entrySet = this.map.entrySet();
for(Map.Entry<String, Integer> entry: entrySet)
{
if(entry.getValue()>this.threshold)
{
System.out.printf("%18s: %d%n", entry.getKey(), entry.getValue());
}
}
}
public void sortByWord()
{
Map<String, Integer> treeMap = new TreeMap<String, Integer>(this.map);
Set<Map.Entry<String, Integer>> entrySet = treeMap.entrySet();
for(Map.Entry<String, Integer> entry: entrySet)
{
if(entry.getValue()>this.threshold)
{
System.out.printf("%18s: %d%n", entry.getKey(), entry.getValue());
}
}
}
/*
* Cite:
* URL: http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-
* value-on-the-values-in-java
* Date:July 10th
*
* Description: I referenced to the algorithum it is used on the web page
* in order to achieve the goal to sort the entries based on the value
* instead of the key
*
*
*/
public void sortByFreq()
{
FreqComparator comparator = new FreqComparator(this.map);
TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>(comparator);
treeMap.putAll(this.map);
Set<Map.Entry<String, Integer>> entrySet = treeMap.entrySet();
for(Map.Entry<String, Integer> entry: entrySet)
{
if(entry.getValue()>this.threshold)
{
System.out.printf("%18s: %d%n", entry.getKey(), entry.getValue());
}
}
}
private void setThreshold(int holder)
{
this.threshold = holder;
}
private class FreqComparator implements Comparator<String>
{
Map<String, Integer> base;
public FreqComparator(Map<String, Integer> base)
{
this.base = base;
}
public int compare(String a, String b)
{
if(base.get(a)>base.get(b))
{
return -1;
}
else if(base.get(a)<base.get(b))
{
return 1;
}
else
{
return a.compareTo(b);
}
}
}
/*private class WordPair
{
private String word;
private int frequency;
public WordPair(String word, int frequency)
{
setFrequency(frequency);
setWord(word);
}
public String getWord()
{
return this.word;
}
public int getFrequency()
{
return this.frequency;
}
public void setWord(String word)
{
this.word = word;
}
public void setFrequency(int frequency)
{
this.frequency = frequency;
}
}*/
}