Repository navigation
Expand file tree
/
Copy pathFileHandler.java
More file actions
352 lines (269 loc) · 9.31 KB
/
Copy pathFileHandler.java
File metadata and controls
352 lines (269 loc) · 9.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;
public class FileHandler {
/**
* Imports a unigram file, normalises the probabilities, and inserts each row into
* a hashtree using the format key = word, value = normalised probability
* @param fileName - location of the unigram file
* @return hashtable
*/
public Hashtable<String, Double> importUnigram(String fileName) {
Hashtable<String, Double> table = new Hashtable<String, Double>();
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] strLineArray = strLine.split("\\s+");
String key = strLineArray[0];
double prob = Double.parseDouble(strLineArray[1]);
double value = Math.pow(10, prob);
table.put(key, value);
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return table;
}
/**
* Imports a bigram file, normalises the probabilities, and inserts each row into
* a hashtree using the format key = (word1,word2), value = normalised probability
* @param fileName - location of the bigram file
* @return hashtable
*/
public Hashtable<String, Double> importBigram(String fileName) {
Hashtable<String, Double> table = new Hashtable<String, Double>();
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
ArrayList<String[]> tempList = new ArrayList<String[]>();
String strLine;
String w1 = "";
while ((strLine = br.readLine()) != null) {
String[] strLineArray = strLine.split("\\s+");
if(w1.equals("")) {
w1 = strLineArray[0];
tempList.add(strLineArray);
} else if(!w1.equalsIgnoreCase(strLineArray[0])) {
//changed to different i j
//process templist
double sum = 0;
for(String[] s: tempList) {
sum += Math.pow(10, Double.parseDouble(s[2]));
}
for(String[] s: tempList) {
String word1 = s[0]; //word 1
String word2 = s[1]; //word 2
String key = word1+","+word2; //e.g. key = "134,55"
double prob = Double.parseDouble(s[2]);
double value = (Math.pow(10, prob)/sum); //probability
table.put(key, value);
}
//reset sum,list
sum = 0;
tempList.clear();
//mark new start
w1 = strLineArray[0];
//add line
tempList.add(strLineArray);
} else if(w1.equalsIgnoreCase(strLineArray[0])) {
tempList.add(strLineArray);
}
}
//process anything still in tempList
double sum = 0;
for(String[] s: tempList) {
sum += Math.pow(10, Double.parseDouble(s[2]));
}
for(String[] s: tempList) {
String word1 = s[0]; //word 1
String word2 = s[1]; //word 2
String key = word1+","+word2; //e.g. key = "134,55"
double prob = Double.parseDouble(s[2]);
double value = (Math.pow(10, prob)/sum); //probability
table.put(key, value);
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return table;
}
/**
* Imports a trigram file, normalises the probabilities, and inserts each row into
* a hashtree using the format key = (word1,word2,word3), value = normalised probability
* @param fileName - location of the trigram file
* @return hashtable
*/
public Hashtable<String, Double> importTrigram(String fileName) {
Hashtable<String, Double> table = new Hashtable<String, Double>();
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
ArrayList<String[]> tempList = new ArrayList<String[]>();
String strLine;
String w1 = "";
String w2 = "";
while ((strLine = br.readLine()) != null) {
String[] strLineArray = strLine.split("\\s+");
if(w1.equals("") && w2.equals("")) {
w1 = strLineArray[0];
w2 = strLineArray[1];
tempList.add(strLineArray);
} else if(!w1.equalsIgnoreCase(strLineArray[0]) || !w2.equalsIgnoreCase(strLineArray[1])) {
//changed to different i j
//process templist
double sum = 0;
for(String[] s: tempList) {
sum += Math.pow(10, Double.parseDouble(s[3]));
}
for(String[] s: tempList) {
String word1 = s[0]; //word 1
String word2 = s[1]; //word 2
String word3 = s[2]; //word 3
String key = word1+","+word2+","+word3; //e.g. key = "134,55,23"
double prob = Double.parseDouble(s[3]);
double value = (Math.pow(10, prob)/sum); //probability
table.put(key, value);
}
//reset sum,list
sum = 0;
tempList.clear();
//mark new start
w1 = strLineArray[0];
w2 = strLineArray[1];
//add line
tempList.add(strLineArray);
} else if(w1.equalsIgnoreCase(strLineArray[0]) && w2.equalsIgnoreCase(strLineArray[1])) {
tempList.add(strLineArray);
}
}
//process anything still in tempList
double sum = 0;
for(String[] s: tempList) {
sum += Math.pow(10, Double.parseDouble(s[3]));
}
for(String[] s: tempList) {
String word1 = s[0]; //word 1
String word2 = s[1]; //word 2
String word3 = s[2]; //word 3
String key = word1+","+word2+","+word3; //e.g. key = "134,55,23"
double prob = Double.parseDouble(s[3]);
double value = (Math.pow(10, prob)/sum); //probability
table.put(key, value);
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return table;
}
/**
* Returns all words in trigram that can result from x-2, x-1
* @param fileName - location of trigram file
* @param x-2
* @param x-1
* @return list of words
*/
public ArrayList<String> findTri(String fileName, String x_2, String x_1) {
ArrayList<String> list = new ArrayList<String>();
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] strLineArray = strLine.split("\\s+");
String w1 = strLineArray[0];
String w2 = strLineArray[1];
if(x_2.equalsIgnoreCase(w1) && x_1.equalsIgnoreCase(w2)) {
list.add(strLineArray[2]);
}
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return list;
}
/**
* Returns all words in bigram that can result from x-1
* @param fileName - location of bigram file
* @param x-1
* @return list of words
*/
public ArrayList<String> findBi(String fileName, String x_1) {
ArrayList<String> list = new ArrayList<String>();
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] strLineArray = strLine.split("\\s+");
String w1 = strLineArray[0];
if(x_1.equalsIgnoreCase(w1)) {
list.add(strLineArray[1]);
}
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return list;
}
/**
* Returns all words in unigram
* @param fileName - location of unigram file
* @return list of words
*/
public ArrayList<String> findUni(String fileName) {
ArrayList<String> list = new ArrayList<String>();
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] strLineArray = strLine.split("\\s+");
list.add(strLineArray[0]);
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return list;
}
/**
* Returns a hashtable of the vocab file, with format key = number, value = word
* @param fileName - location of the vocab file
* @return hashtable
*/
public Hashtable<String, String> importVocab(String fileName) {
Hashtable<String, String> vocabTable = new Hashtable<String, String>();
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] strLineArray = strLine.split("\\s+");
String key = strLineArray[0]; //number
String value = strLineArray[1]; //word
vocabTable.put(key, value);
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return vocabTable;
}
}