-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtra.java
More file actions
114 lines (98 loc) · 3.62 KB
/
Copy pathExtra.java
File metadata and controls
114 lines (98 loc) · 3.62 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
/**
* Student ID: V00816592
* Date: 07/31/15
* Program Name: Extra.java
* Program Description: Extra contains algorithm which runs WordReferences with all of the provided
* testcases from tests.txt and compares
* computed output against the expected results (results.txt)
* @Author Isaac Sahle
*/
//Sample command-line input .. java Extra --input tests/in01.txt --output tests/out01.txt
import java.io.*;
import java.util.*;
public class Extra{
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 main(String[] args) {
String infileName1 = null;
String infileName2 = null;
boolean passed = true;
try{
//process command line arguments
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--input")) {
infileName1 = args[i+1];
} else if (args[i].equals("--output")) {
infileName2 = args[i+1];
}
}
if (infileName1 == null) {
System.out.println("Usage: please provide the first input filename");
}
if (infileName2 == null) {
System.out.println("Usage: please provide the second input filename");
}
if (infileName1 == null || infileName2 == null) {
System.exit(1);
}
// Processing can begin, file names have been set
Scanner infileScanner1 = new Scanner(new File(infileName1));
Scanner infileScanner2 = new Scanner(new File(infileName2));
String exclude[] = {"a", "the", "i", "it", "is"};
BSTRefBased result = rawRefs(infileName1);
filterRefs(result,exclude);
//for each WordRefs in the bst, compare each string and count to expected output
for (WordRefs wf : result) {
if(!(wf.toString().equals(infileScanner2.nextLine()))){
passed = false;
}
}
}
catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Probably not enough arguments provided.\n");
System.exit(1);
}
System.out.println("Results are the same: " + passed);
}
}