-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordFrequencies.java
78 lines (71 loc) · 2.22 KB
/
WordFrequencies.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
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class WordFrequencies{
public static void main (String args[]) throws FileNotFoundException, IOException {
Map map1 = new HashMap();
String argument = "";
try (BufferedReader bRead = new BufferedReader(new InputStreamReader(System.in))) {
StringBuilder sBuild = new StringBuilder();
String line = "";
String text = "";
if(args.length > 0) {
argument = args[0];
} else {
argument = argument;
}
while((line = bRead.readLine()) != null) {
text += line;
}
String[] words = new String[0];
if((argument.equals("-s")) || (argument.equals("-sc")) || (argument.equals("-cs"))) {
String[] temp = text.replaceAll("[\\p{Punct}&&[^0-9]]", "").split(" ");
words = temp;
} else {
String[] temp = ((text.replaceAll("[\\p{Punct}&&[^0-9]]", "")).toUpperCase()).split(" ");
words = temp;
}
//String[] words = ((text.replaceAll("[\\p{Punct}&&[^0-9]]", "")).toUpperCase()).split(" ");
//String line = "";
//for(int i = 0; i < stringy.length; i++) {
// line = line + stringy[i] + " ";
//}
//line = line.toUpperCase();
//while(line != null) {
//String[] words = line.split(" ");
for(int i = 0; i < words.length; i++) {
if(words[i].trim().isEmpty()) {
continue;
} else {
if(map1.get(words[i]) == null) {
map1.put(words[i], 1);
} else {
int newValue = Integer.valueOf(String.valueOf(map1.get(words[i])));
newValue++;
map1.put(words[i], newValue);
}
}
}
//sBuild.append(System.lineSeparator());
//line = bRead.readLine();
//}
}
Map<String, String> sorted = new TreeMap<String, String>(map1);
boolean isT = true;
if((argument.equals("-c")) || (argument.equals("-cs"))|| (argument.equals("-sc"))){
for(Object key : sorted.keySet()) {
System.out.println(key);
}
} else {
for (Object key : sorted.keySet()) {
System.out.println(key + " " + map1.get(key));
}
}
}
}