-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordReference.java
More file actions
75 lines (64 loc) · 2.04 KB
/
Copy pathWordReference.java
File metadata and controls
75 lines (64 loc) · 2.04 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
import java.util.*;
import java.io.*;
/**
* @author Mike Zastre, UVic
*
* Part of CSC 115, Summer 2015, Assignment #5
*/
public class WordReference {
public static BSTRefBased rawRefs(String filename) {
BSTRefBased result = new BSTRefBased();
try {
Scanner scanner = new Scanner(new File(filename));
int lineNum = 1;
while (scanner.hasNext()) {
String line = scanner.nextLine();
line = line.trim();
line = line.replaceAll("\\p{Punct}+", "");
if (line.equals("")) {
continue;
}
String words[] = line.split("\\s+");
for (String w : words) {
String ww = w.toLowerCase();
WordRefs wf = result.retrieve(ww);
if (wf == null) {
result.insert(ww);
wf = result.retrieve(ww);
}
wf.addLine(lineNum);
}
lineNum++;
}
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
System.exit(1);
} catch (NullPointerException npe) {
System.err.println("One of the tree routines created a " +
"null pointer exception");
System.exit(2);
}
return result;
}
public static void filterRefs(BSTRefBased tree, String words[]) {
for (String w : words) {
tree.delete(w);
}
}
public static void reportRefs(BSTRefBased tree) {
for (WordRefs wf : tree) {
System.out.println(wf.toString());
}
}
public static void main(String args[]) {
BSTRefBased t;
String exclude[] = {"a", "the", "i", "it", "is"};
if (args.length < 1) {
System.err.println("Usage: provide an input filename");
System.exit(1);
}
t = rawRefs(args[0]);
filterRefs(t, exclude);
reportRefs(t);
}
}