-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
282 lines (256 loc) · 7.43 KB
/
Copy pathUtil.java
File metadata and controls
282 lines (256 loc) · 7.43 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
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
/*
* File: Util.java
* Author: Jackson Davenport
*
* Utility class to provide common space for simple tasks such as opening,
* reading, writing, (de)serializing data to files.
*
*
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.Scanner;
import java.util.Hashtable;
import java.util.Enumeration;
public class Util{
public static final String TRAINING_FILES = "Training_Files";
public static final String ROOT_PATH = System.getProperty("user.dir")+System.getProperty("file.separator");
public static long prevTime;
public Util(){
}
/* printBigramDistribution
* Take the hashtable for the complete unigram distribution and print
* it out nicely to a file
*/
public static void printUnigramDistribution(Hashtable<String, Integer> uni, BufferedWriter outFile){
Enumeration words = uni.keys();
while(words.hasMoreElements()){
String key = (String) words.nextElement();
int value = uni.get(key);
String toWrite = (key + "\t" + value);
// Write to the file
try{
outFile.write(toWrite);
if(words.hasMoreElements()){
outFile.newLine();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
/* printBigramDistribution
* Take the hashtable for the complete bigram distribution and print
* it out nicely to a file
*/
public static void printBigramDistribution(Hashtable<String, BigramElement> bd, BufferedWriter outFile){
Enumeration firstWord = bd.keys();
while(firstWord.hasMoreElements()){
String firstKey = (String) firstWord.nextElement();
Enumeration secondWord = bd.get(firstKey).getKeys();
while(secondWord.hasMoreElements()){
String secondKey = (String) secondWord.nextElement();
int value = bd.get(firstKey).getCount(secondKey);
String toWrite = (firstKey + "\t" + secondKey + "\t" + value);
// Write to the file
try{
outFile.write(toWrite);
if(firstWord.hasMoreElements() || secondWord.hasMoreElements()){
outFile.newLine();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
/* printBigramDistribution
* Take the hashtable for the complete bigram distribution and print
* it out nicely to console
*/
public static void printBigramDistribution(Hashtable<String, BigramElement> bd){
Enumeration firstWord = bd.keys();
while(firstWord.hasMoreElements()){
String firstKey = (String) firstWord.nextElement(); //
Enumeration secondWord = bd.get(firstKey).getKeys();
while(secondWord.hasMoreElements()){
String secondKey = (String) secondWord.nextElement();
int value = bd.get(firstKey).getCount(secondKey);
System.out.println(firstKey + "\t" + secondKey + "\t" + value);
}
}
}
/* getScanner
* Find the file and create a scanner object, handle the error
*/
public static Scanner getScanner(String fileName){
Scanner newScanner = null;
String directoryPath = "";
try{
directoryPath = ROOT_PATH + TRAINING_FILES;
File directory = new File(directoryPath);
File file = new File(directory, fileName);
newScanner = new Scanner(new FileReader(file));
}
catch (FileNotFoundException e){
System.err.println("Scanner did not find file: " + fileName);
System.err.println(e);
System.exit(0);
}
return newScanner;
}
/* openWriteFile
* Open up a file to overwrite on, create it if it doesn't exist
*/
public static BufferedWriter openWriteFile(String fileName){
try{
String directoryPath = ROOT_PATH + TRAINING_FILES;
File directory = new File(directoryPath);
File file = new File(directory, fileName);
if(!file.exists()){
file.createNewFile();
}
return (new BufferedWriter(new FileWriter(file.getAbsoluteFile())));
}
catch(IOException e){
e.printStackTrace();
System.exit(0);
return null;
}
}
/* openReadFile
* Open up a file to overwrite on, create it if it doesn't exist
*/
public static BufferedReader openReadFile(String fileName){
String directoryPath = ROOT_PATH + TRAINING_FILES;
try{
File directory = new File(directoryPath);
File file = new File(directory, fileName);
if(!file.exists()){
throw new FileNotFoundException();
}
return (new BufferedReader(new FileReader(file.getAbsoluteFile())));
}
catch(IOException e){
System.err.println("Error:"+ directoryPath + "\\" + fileName + " - not found");
e.printStackTrace();
System.exit(0);
}
return null;
}
/* getNextLine
* Get the next line in the BufferedReader, return null when at end
* of file
*/
public static String getNextLine(BufferedReader br){
try{
String s = br.readLine();
return s;
}
catch(IOException e){
return null;
}
}
/* closeFile
* Close the file and print the stack trace as necessary
*/
public static void closeFile(BufferedReader br){
try{
br.close();
}
catch(IOException e){
e.printStackTrace();
}
}
/* closeFile
* Close the file and print the stack trace as necessary
*/
public static void closeFile(BufferedWriter br){
try{
br.close();
}
catch(IOException e){
e.printStackTrace();
}
}
/* serializeDistribution
* Take the distribution object and serialize it into memory to the
* given filename within the Training_Files directory
*/
public static void serializeDistribution(Distributions d, String fileName){
// Output the distribution to serializable memory
try{
// Get and if necessary create the file
String directoryPath = ROOT_PATH + TRAINING_FILES;
File directory = new File(directoryPath);
File file = new File(directory, fileName);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fileOut = new FileOutputStream(file);
// Serialize it and output it
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(d);
out.close();
fileOut.close();
}catch(IOException i){
i.printStackTrace();
System.exit(0);
}
}
/* deserializeDistribution
* Take the serialized distribution from the given filename located
* within the Training_Files directory
*/
public static Distributions deserializeDistribution(String fileName){
Distributions dist = null;
try {
String directoryPath = ROOT_PATH + TRAINING_FILES;
File directory = new File(directoryPath);
File file = new File(directory, fileName);
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fileIn);
dist = (Distributions) in.readObject();
in.close();
fileIn.close();
}
catch(IOException i) {
System.out.println("IO Exception on file: " + fileName);
i.printStackTrace();
return null;
}
catch(ClassNotFoundException c) {
System.out.println("Distribution class not found");
c.printStackTrace();
return null;
}
return dist;
}
/* initTime
* Initialize the start time
*/
public static void initTime(){
prevTime = System.nanoTime();
}
/* logTime
* Print the phrase of the log time
*/
public static void logTime(String endPhrase){
System.out.println("Took " + ((System.nanoTime() - prevTime)/1000000) + " " + endPhrase);
prevTime = System.nanoTime();
}
}