-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordStatCount.java
More file actions
210 lines (156 loc) · 6.01 KB
/
WordStatCount.java
File metadata and controls
210 lines (156 loc) · 6.01 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
import java.io.*;
public class WordStatCount {
static void sorting(int[] cnt, String[] words, int last) {
int j = 0;
for (int i = 0; i < last; i++) {
j = i;
while (j != 0 && cnt[j] < cnt[j - 1]) {
int t = 0;
String ts = "";
t = cnt[j];
cnt[j] = cnt[j - 1];
cnt[j - 1] = t;
ts = words[j];
words[j] = words[j - 1];
words[j - 1] = ts;
j--;
}
}
}
static int getHash(String s) {
int MOD = 1000000123;
int p = 1499;
long pow = 1;
long hash = 0;
for (int i = 0; i < s.length(); i++) {
int chr = s.charAt(i);
hash = (hash + (chr * pow) % MOD) % MOD;
pow = pow * p % MOD;
}
return (int) hash;
}
static String[] newStrArray(String[] old) {
int n = old.length;
String[] lst = new String[2 * n];
for (int i = 0; i < n; i++) {
lst[i] = old[i];
}
return lst;
}
static BufferedReader wrapReader(String fileName) throws FileNotFoundException, UnsupportedEncodingException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "utf8"));
return reader;
}
static BufferedWriter wrapWriter(String fileName) throws FileNotFoundException, UnsupportedEncodingException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf8"));
return writer;
}
static boolean isGoodChar(char chr) {
return ((Character.getType(chr) == Character.DASH_PUNCTUATION) || (Character.isLetter(chr)) || (chr == '\''));
}
static String[] readAndFindWords(String inputFile) {
String[] array = new String[1];
int last = 0;
try {
BufferedReader reader = wrapReader(inputFile);
try {
while (true) {
String s = reader.readLine();
if (s == null) {
break;
} else {
s = s + " ";
}
int start = 0;
for (int i = 0; i < s.length(); i++) {
char chr = s.charAt(i);
if (!isGoodChar(chr)) {
if (!s.substring(start, i).isEmpty()) {
if (last == array.length) {
array = newStrArray(array);
}
String word = s.substring(start, i).toLowerCase();
array[last++] = word;
}
start = i + 1;
}
}
}
} finally {
reader.close();
}
} catch (FileNotFoundException e) {
System.out.println("File " + inputFile + " is not found, make sure u have created it!");
} catch (UnsupportedEncodingException e) {
System.out.println("File " + inputFile + " contains symbols that can't be read by the system!");
} catch (IOException e) {
System.out.println("Another string type was expected");
}
return array;
}
static int[] putHashes(String[] array, int len) {
int[] hashes = new int[len];
for (int i = 0; i < len; i++) {
hashes[i] = getHash(array[i]);
}
return hashes;
}
static void writeWords(String[] array, String fileName, int len) {
int[] hashes = putHashes(array, len);
int last = 0;
int[] count = new int[len];
String[] noRepeat = new String[len];
try {
BufferedWriter writer = wrapWriter(fileName);
try {
for (int i = 0; i < len; i++) {
Boolean used = false;
for (int j = 0; j < i; j++) {
if (hashes[i] == hashes[j]) {
used = true;
break;
}
}
if (!used) {
int cnt = 0;
for (int j = i; j < len; j++) {
if (hashes[i] == hashes[j]) {
cnt++;
}
}
String word = array[i];
noRepeat[last] = word;
count[last] = cnt;
last += 1;
}
}
sorting(count, noRepeat, last);
for (int i = 0; i < last; i++) {
writer.write(noRepeat[i] + " " + count[i] + "\n");
}
} finally {
writer.close();
}
} catch (FileNotFoundException e) {
System.out.println("File " + fileName + " is not found, make sure u have created it!");
} catch (UnsupportedEncodingException e) {
System.out.println("File " + fileName + " contains symbols that can't be read by the system!");
} catch (IOException e) {
System.out.println("Another string type was expected");
}
}
// length of not-null String-type array
static int len(String[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
return (i);
}
}
return array.length;
}
public static void main(String[] args) {
String[] array = readAndFindWords(args[0]);
int ln = len(array);
writeWords(array, args[1], ln);
}
}