-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonWords.java
More file actions
52 lines (46 loc) · 1.31 KB
/
Copy pathCommonWords.java
File metadata and controls
52 lines (46 loc) · 1.31 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
import edu.duke.*;
public class CommonWords
{
public String[] getCommon(){
FileResource resource = new FileResource();// add your txt file there (FileResource("data/common.txt"))
String[] common = new String[20];
int index = 0;
for(String s : resource.words()){
common[index] = s;
index += 1;
}
return common;
}
public int indexOf(String[] list, String word) {
for (int k=0; k<list.length; k++) {
if (list[k].equals(word)) {
return k;
}
}
return -1;
}
public void countWords(FileResource resource, String[] common, int[] counts){
for(String word : resource.words()){
word = word.toLowerCase();
int index = indexOf(common,word);
if (index != -1) {
counts[index] += 1;
}
}
}
void countShakespeare(){
String[] plays = {"caesar.txt", "errors.txt", "hamlet.txt",
"likeit.txt", "macbeth.txt", "romeo.txt"};
//String[] plays = {"small.txt"};
String[] common = getCommon();
int[] counts = new int[common.length];
for(int k=0; k < plays.length; k++){
FileResource resource = new FileResource("data/" + plays[k]);
countWords(resource,common,counts);
System.out.println("done with " + plays[k]);
}
for(int k=0; k < common.length; k++){
System.out.println(common[k] + "\t" + counts[k]);
}
}
}