-
Notifications
You must be signed in to change notification settings - Fork 0
/
nGram.java
285 lines (261 loc) · 10.2 KB
/
nGram.java
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package app;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class nGram
{
static {System.out.println("Starting class: " + MethodHandles.lookup().lookupClass().getCanonicalName());}
List<String> fileList = new ArrayList<String>(); // build command as a list of strings
static final String skipList =
"\\ba\\b"
+ "|\\ban\\b"
+ "|\\band\\b"
+ "|\\bare\\b"
+ "|\\bas\\b"
+ "|\\bat\\b"
+ "|\\bbe\\b"
+ "|\\bbut\\b"
+ "|\\bby\\b"
+ "|\\bfor\\b"
+ "|\\bif\\b"
+ "|\\bin\\b"
+ "|\\binto\\b"
+ "|\\bis\\b"
+ "|\\bit\\b"
+ "|\\bno\\b"
+ "|\\bnot\\b"
+ "|\\bof\\b"
+ "|\\bon\\b"
+ "|\\bor\\b"
+ "|\\bsuch\\b"
+ "|\\bthat\\b"
+ "|\\bthe\\b"
+ "|\\btheir\\b"
+ "|\\bthen\\b"
+ "|\\bthere\\b"
+ "|\\bthese\\b"
+ "|\\bthey\\b"
+ "|\\bthis\\b"
+ "|\\bto\\b"
+ "|\\bwas\\b"
+ "|\\bwill\\b"
+ "|\\bwith\\b"
;
static BufferedReader input;
public PrintWriter output;
static String inFileName;
static String outFileName;
static String sortFileName;
static int countOnlyStr1;
static int countOnlyStr2;
static int countBothStr1Str2;
static nGram b;
public static void main(final String[] args) throws Exception, IOException, FileNotFoundException {
b = new nGram();
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\RANSAC-philipperemy\\src\\RANSAC.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\FindLightStrings\\src\\RANSAC.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\RANSAC-minD3D\\src\\RANSAC.java");
b.fileList.add("C:\\Users\\RKT\\frc\\FRC2018\\RANSAC-minD3D\\averageRANSAC-master\\src\\RANSAC.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\Intermediate Vision Java\\src\\org\\usfirst\\frc\\team4237\\robot\\RANSAC.java");
b.fileList.add("C:\\Users\\RKT\\frc\\FRC2019\\Code\\FindLightString\\src\\org\\usfirst\\frc\\team4237\\robot\\RANSAC.java");
b.fileList.add("C:\\Users\\RKT\\frc\\FRC2018\\RANSAC-minD3D\\averageRANSAC-master\\src\\RansacTest.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\RANSAC-minD3D\\src\\RansacTest.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\RANSAC-philipperemy\\src\\RansacTest.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\Intermediate Vision Java\\src\\org\\usfirst\\frc\\team4237\\robot\\RANSAC_Iterator.java");
b.fileList.add("C:\\Users\\RKT\\frc\\FRC2019\\Code\\FindLightString\\src\\org\\usfirst\\frc\\team4237\\robot\\RANSAC_Iterator.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\Simple Vision\\src\\org\\usfirst\\frc\\team4237\\robot\\camera_process.java");
b.fileList.add("C:\\Users\\RKT\\frc\\FRC2019\\Code\\FindLightString\\src\\org\\usfirst\\frc\\team4237\\robot\\camera_process.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\Intermediate Vision Java\\src\\org\\usfirst\\frc\\team4237\\robot\\camera_process.java");
b.fileList.add("C:\\Users\\RKT\\eclipse-workspace\\FindLightStrings\\src\\Main.java");
b.generateGram();
b.compareFiles();
}
public void generateGram() throws Exception, IOException, FileNotFoundException {
int countFiles = 0;
String str;
loop:
while(true) {
countFiles++;
//inFileName = "in" + countFiles + ".txt";
if ( countFiles > b.fileList.size()) break loop;
inFileName = b.fileList.get(countFiles-1);
System.out.println(inFileName);
outFileName = "out" + countFiles + ".txt";
sortFileName = "sort" + countFiles + ".txt";
{
try {
input = new BufferedReader(new FileReader(inFileName));
output = new PrintWriter(outFileName);
} catch (final FileNotFoundException e)
{
//System.err.println(e);
break loop;
}
while ((str = input.readLine()) != null) {
//System.out.println(">" + str + "<");
// output.println(gram);// -----you had this originally, we don't need this
str = str.toLowerCase();
str = str.replaceAll(skipList, ""); // skip a few common words; short list could be expanded
str = str.replaceAll("\\W", ""); // characters to ignore - all non-words (this is, keep a-z 0-9 _)
// str = str.replaceAll("[\\[\\]\\:\\/\\?\\<\\>\\;\\(\\)\\{\\}\\,\\.\\'\\s]",
// ""); // add stuff to ignore
//System.out.println(">" + str + "<");
final int line = str.length();
final int numGram = 4;
for (int x = 1; x < line - numGram + 2; x++) {
String biGram = str.substring(x - 1, x + numGram - 1);
output.println(biGram);
}
}
input.close();
output.close();
sortFile();
}
}
}
private static void sortFile()
{
try
{
// execute sort command in a cmd (operating system) window
List<String> command = new ArrayList<String>(); // build command as a list of strings
command.add("cmd");
command.add("/c");
command.add("sort");
command.add(outFileName);
command.add("/O");
command.add(sortFileName);
//System.out.println(" Run sort command");
ProcessBuilder pb1 = new ProcessBuilder(command);
Process process1 = pb1.start();
int errCode1 = process1.waitFor();
command.clear();
System.out.println("sort command executed, any errors? " + (errCode1 == 0 ? "No" : "Yes"));
String mountOutput = output(process1.getInputStream());
System.out.println("sort output:" + mountOutput);
System.out.println("sort errors:" + output(process1.getErrorStream()));
}
catch (Exception ex2)
{
System.out.println("Error in sort process " + ex2);
}
}
private static String output(InputStream inputStream) throws IOException
{
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = br.readLine()) != null)
{
sb.append(line + System.getProperty("line.separator"));
}
}
finally
{
br.close();
}
return sb.toString();
}
private void compareFiles() throws IOException {
for (int i = 0; i < b.fileList.size() - 1; i++) {
// get the first filename
String inFileName1 = "sort" + (i+1) + ".txt";
for (int j = i+1; j < b.fileList.size(); j++) {
// compare with all the rest
String inFileName2 = "sort" + (j+1) + ".txt";
System.out.println(inFileName1 + " " + inFileName2);
System.out.println (b.fileList.get(i) + " " + b.fileList.get(j));
mergeFiles(inFileName1, inFileName2);
}
}
}
private void mergeFiles(String inFileName1, String inFileName2) {
SortReader inputSorted1;
SortReader inputSorted2;
String str1="";
String str2="";
countOnlyStr1 = 0;
countOnlyStr2 = 0;
countBothStr1Str2 = 0;
inputSorted1 = b.new SortReader(inFileName1);
inputSorted2 = b.new SortReader(inFileName2);
if (!inputSorted1.endFile()) str1 = inputSorted1.read();
if (!inputSorted2.endFile()) str2 = inputSorted2.read();
while (!inputSorted1.endFile() && !inputSorted2.endFile()) {
//System.out.println(str1 + " " + str2);
// if str1 < str2 str1 not matched and read another str1
// if str1 == str2 they match read str1 and read str2
// if str1 > str2 str2 not matched and read another str2
// if str1 end file run out str2
// if str2 end file run out str1
// The result is a negative integer if this String object lexicographically precedes the argument string.
// The result is a positive integer if this String object lexicographically follows the argument string.
// The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
if ( str1.compareTo(str2) < 0 ) {
countOnlyStr1++;
str1 = inputSorted1.read();
}
else if ( str1.compareTo(str2) > 0 ) {
countOnlyStr2++;
str2 = inputSorted2.read();
}
else { // equal
countBothStr1Str2++;
str1 = inputSorted1.read();
str2 = inputSorted2.read();
}
}
// run out the file that needs it and count the n-grams
if (inputSorted1.endFile()) {
while ( !inputSorted2.endFile() ) {
countOnlyStr2++;
inputSorted2.read();
}
}
else {
while ( !inputSorted1.endFile() ) {
countOnlyStr1++;
inputSorted1.read();
}
}
System.out.println(countOnlyStr1 + " " + countOnlyStr2 + " " + countBothStr1Str2 + " " +
( (float)(2*countBothStr1Str2) / (float)(countOnlyStr1 + countOnlyStr2 + 2*countBothStr1Str2) ) );
}
class SortReader {
private BufferedReader file;
private boolean endFile = false;
private String str = null;
public SortReader(String fileName) {
//System.out.println("opening merge file " + fileName);
try {
file = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
endFile = true;
//e.printStackTrace();
}
}
String read () {
if (!endFile)
try {
str = file.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str == null) endFile = true;
return str;
}
boolean endFile() {
return endFile;
}
}
}