Skip to content

Commit 68d9450

Browse files
authored
Update TopKWords.java
All has been improved according to your advice.
1 parent df1b68b commit 68d9450

File tree

1 file changed

+59
-67
lines changed

1 file changed

+59
-67
lines changed

Others/TopKWords.java

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
1-
import java.io.*;
2-
import java.util.*;
1+
import java.io.*;
2+
import java.util.*;
33

4-
/* display the most frequent K words in the file and the times it appear
4+
/* display the most frequent K words in the file and the times it appear
55
in the file – shown in order (ignore case and periods) */
66

7-
public class TopKWords {
8-
public static void main(String[] a) {
7+
public class TopKWords {
8+
static class CountWords {
9+
private String fileName;
10+
11+
public CountWords(String fileName) {
12+
this.fileName = fileName;
13+
}
14+
15+
public Map<String, Integer> getDictionary() {
16+
Map<String, Integer> dictionary = new HashMap<>();
17+
FileInputStream fis = null;
18+
19+
try {
20+
21+
fis = new FileInputStream(fileName); // open the file
22+
int in = 0;
23+
String s = new String(); // init a empty word
24+
in = fis.read(); // read one character
25+
26+
while (-1 != in) {
27+
if (Character.isLetter((char)in)) {
28+
s += (char)in; //if get a letter, append to s
29+
} else {
30+
// this branch means an entire word has just been read
31+
if (s.length() > 0) {
32+
// see whether word exists or not
33+
if (dictionary.containsKey(s)) {
34+
// if exist, count++
35+
dictionary.put(s, dictionary.get(s) + 1);
36+
} else {
37+
// if not exist, initiate count of this word with 1
38+
dictionary.put(s, 1);
39+
}
40+
}
41+
s = ""; // reInit a empty word
42+
}
43+
in = fis.read();
44+
}
45+
return dictionary;
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
} finally {
49+
try {
50+
// you always have to close the I/O streams
51+
fis.close();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
return null;
57+
}
58+
}
59+
public static void main(String[] args) {
960
// you can replace the filePath with yours
1061
CountWords cw = new CountWords("/Users/lisanaaa/Desktop/words.txt");
1162
Map<String, Integer> dictionary = cw.getDictionary(); // get the words dictionary: {word: frequency}
@@ -19,74 +70,15 @@ public static void main(String[] a) {
1970
);
2071

2172
Scanner input = new Scanner(System.in);
22-
Integer k = new Integer(input.nextLine());
73+
int k = input.nextInt();
2374
while (k > list.size()) {
2475
System.out.println("Retype a number, your number is too large");
2576
input = new Scanner(System.in);
2677
k = new Integer(input.nextLine());
2778
}
28-
for (int i = 0; i<k; i++) {
29-
System.out.println(list.get(list.size()-i-1));
79+
for (int i = 0; i < k; i++) {
80+
System.out.println(list.get(list.size() - i - 1));
3081
}
3182
}
3283
}
3384

34-
class CountWords {
35-
private String fileName;
36-
37-
public CountWords(String fileName) {
38-
this.fileName = fileName;
39-
}
40-
41-
public Map<String, Integer> getDictionary() {
42-
Map<String, Integer> dictionary = new HashMap<>();
43-
FileInputStream fis = null;
44-
45-
try {
46-
47-
fis = new FileInputStream(fileName); // open the file
48-
int in = 0;
49-
StringBuffer sb = new StringBuffer(); // load the word
50-
in = fis.read(); // read one character
51-
boolean notEnd = true; // signal whether is the end of file
52-
53-
while (notEnd) {
54-
// when in == -1 means get the end of the file
55-
if (-1 == in) {
56-
notEnd = false; //if false, end the while loop
57-
}
58-
if (Character.isLetter((char)in)) {
59-
sb.append((char)in); //if get a letter, put it in StringBuffer
60-
} else {
61-
// this branch means an entire word has just been read
62-
if (sb.length() > 0) {
63-
//see whether word exists in StringBuffer or not
64-
if (dictionary.containsKey(sb.toString())) {
65-
//if exist, count++
66-
dictionary.put(sb.toString(), dictionary.get(sb.toString()) + 1);
67-
} else {
68-
// if not exist, initiate count of this word with 1
69-
dictionary.put(sb.toString(), 1);
70-
}
71-
}
72-
sb = new StringBuffer(); //reload the StringBuffer
73-
}
74-
in = fis.read(); //read the character
75-
}
76-
return dictionary;
77-
}
78-
catch (IOException e) {
79-
e.printStackTrace();
80-
}
81-
finally {
82-
try {
83-
// you always have to close the I/O streams
84-
fis.close();
85-
}
86-
catch (IOException e) {
87-
e.printStackTrace();
88-
}
89-
}
90-
return null;
91-
}
92-
}

0 commit comments

Comments
 (0)