-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBMMODEL1.java
More file actions
281 lines (246 loc) · 9.6 KB
/
IBMMODEL1.java
File metadata and controls
281 lines (246 loc) · 9.6 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
/**
* File : IBMMODEL1.java
* Author : Arpit Sharma
* Functionality : This class is an implementation of IBM language translation model 1
*/
package ibmModel1;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class IBMMODEL1 {
private String corpusFileGer = "./short.de";
private String corpusFileEng = "./short.en";
private String testFileloc = "./testWords";
public static void main(String[] args) throws ClassNotFoundException, IOException {
IBMMODEL1 ibm = new IBMMODEL1();
if(args.length!=0){
ibm.corpusFileGer = args[0];
ibm.corpusFileEng = args[1];
ibm.testFileloc = args[2];
}
// Start recording time
final long startTime = System.currentTimeMillis();
// Call the model() function to train and fing the probabilities of translations
ibm.model();
// End recording time and report it
final long endTime = System.currentTimeMillis();
System.out.println("Total execution time: " + (double)(endTime - startTime)/1000 + " seconds");
}
/**
* Function to train using IBM model1 algorithm using "corpusFileGer" as foreign language training corpus file and
* "corpusFileEng" as English language training corpus file.
* And finally printing the top five most probable translations of the words given in test file "testFileloc"
* The 5 most probable word translations are arranged in ascending order of probabilities.
*/
private void model(){
try{
String gerline = "";
String engline = "";
int numberOfGerWords = 0;
BufferedReader germanFileTemp = new BufferedReader(new FileReader(corpusFileGer));
while((gerline=germanFileTemp.readLine())!=null){
ArrayList<String> gerWords = new ArrayList<String>();
for(String tempGer : gerline.split(" ")){
if(!tempGer.equalsIgnoreCase(",") && !tempGer.equalsIgnoreCase(".") &&
!tempGer.equalsIgnoreCase(";")&& !tempGer.equalsIgnoreCase("?")
&& !tempGer.equalsIgnoreCase("\"") && !tempGer.equalsIgnoreCase("\'")){
if(!gerWords.contains(tempGer)){
gerWords.add(tempGer);
numberOfGerWords++;
}
}
}
}
germanFileTemp.close();
HashMap<String,Double> probsT = new HashMap<String,Double>();
BufferedReader germanFile = new BufferedReader(new FileReader(corpusFileGer));
BufferedReader englishFile = new BufferedReader(new FileReader(corpusFileEng));
while((gerline=germanFile.readLine())!=null){
ArrayList<String> gerWords = new ArrayList<String>();
ArrayList<String> engWords = new ArrayList<String>();
for(String tempEng : gerline.split(" ")){
if(!tempEng.equalsIgnoreCase(",") && !tempEng.equalsIgnoreCase(".") &&
!tempEng.equalsIgnoreCase(";")&& !tempEng.equalsIgnoreCase("?")
&& !tempEng.equalsIgnoreCase("\"") && !tempEng.equalsIgnoreCase("\'")){
gerWords.add(tempEng);
}
}
engline = englishFile.readLine();
for(String tempEng : engline.split(" ")){
if(!tempEng.equalsIgnoreCase(",") && !tempEng.equalsIgnoreCase(".") &&
!tempEng.equalsIgnoreCase(";")&& !tempEng.equalsIgnoreCase("?")
&& !tempEng.equalsIgnoreCase("\"") && !tempEng.equalsIgnoreCase("\'")){
engWords.add(tempEng);
}
}
engWords.add("NULL");
for(int i=0;i<gerWords.size();++i){
for(int j=0;j<engWords.size();++j){
String temp = gerWords.get(i) + "|"+engWords.get(j);
probsT.put(temp, (double)1/numberOfGerWords);
}
}
}
germanFile.close();
englishFile.close();
// System.out.println("Size of probs :-" + probs.size());
// Starting EM here:::::::::::
for(int iter=0;iter<10;++iter){
// Initializing Count(f|e)::::::::;
HashMap<String,Double> count = new HashMap<String,Double>();
HashMap<String,Double> total = new HashMap<String,Double>();
BufferedReader germanFile1 = new BufferedReader(new FileReader(corpusFileGer));
BufferedReader englishFile1 = new BufferedReader(new FileReader(corpusFileEng));
gerline = "";
engline = "";
while((gerline=germanFile1.readLine())!=null){
ArrayList<String> gerWords = new ArrayList<String>();
ArrayList<String> engWords = new ArrayList<String>();
for(String tempEng : gerline.split(" ")){
if(!tempEng.equalsIgnoreCase(",") && !tempEng.equalsIgnoreCase(".") &&
!tempEng.equalsIgnoreCase(";")&& !tempEng.equalsIgnoreCase("?")
&& !tempEng.equalsIgnoreCase("\"") && !tempEng.equalsIgnoreCase("\'")){
gerWords.add(tempEng);
}
}
engline = englishFile1.readLine();
for(String tempEng : engline.split(" ")){
if(!tempEng.equalsIgnoreCase(",") && !tempEng.equalsIgnoreCase(".") &&
!tempEng.equalsIgnoreCase(";")&& !tempEng.equalsIgnoreCase("?")
&& !tempEng.equalsIgnoreCase("\"") && !tempEng.equalsIgnoreCase("\'")){
engWords.add(tempEng);
}
}
engWords.add("NULL");
for(int i=0;i<gerWords.size();++i){
for(int j=0;j<engWords.size();++j){
String temp = gerWords.get(i) + "|"+engWords.get(j);
count.put(temp, 0.0);
total.put(engWords.get(j), 0.0);
}
}
}
germanFile1.close();
englishFile1.close();
BufferedReader germanFile2 = new BufferedReader(new FileReader(corpusFileGer));
BufferedReader englishFile2 = new BufferedReader(new FileReader(corpusFileEng));
while((gerline=germanFile2.readLine())!=null){
engline = englishFile2.readLine();
ArrayList<String> gerWords = new ArrayList<String>();
for(String temp : gerline.split(" ")){
if(!temp.equalsIgnoreCase(",") && !temp.equalsIgnoreCase(".") &&
!temp.equalsIgnoreCase(";")&& !temp.equalsIgnoreCase("?")
&& !temp.equalsIgnoreCase("\"") && !temp.equalsIgnoreCase("\'")){
gerWords.add(temp);
}
}
ArrayList<String> engWords = new ArrayList<String>();
for(String temp : engline.split(" ")){
if(!temp.equalsIgnoreCase(",") && !temp.equalsIgnoreCase(".") &&
!temp.equalsIgnoreCase(";")&& !temp.equalsIgnoreCase("?")
&& !temp.equalsIgnoreCase("\"") && !temp.equalsIgnoreCase("\'")){
engWords.add(temp);
}
}
engWords.add("NULL");
HashMap<String,Double> sumTotalGer = new HashMap<String,Double>();
for(int i=0;i<gerWords.size();++i){
sumTotalGer.put(gerWords.get(i), 0.0);
for(int j=0;j<engWords.size();++j){
String tempKey = gerWords.get(i)+"|"+engWords.get(j);
double tempVal = probsT.get(tempKey);
sumTotalGer.put(gerWords.get(i),sumTotalGer.get(gerWords.get(i))+tempVal);
}
}
for(int i=0;i<gerWords.size();++i){
for(int j=0;j<engWords.size();++j){
String tempKey = gerWords.get(i)+"|"+engWords.get(j);
double tempValT = probsT.get(tempKey);
double tempValC = count.get(tempKey);
double tempValTOTAL = total.get(engWords.get(j));
double tempNew = tempValT/sumTotalGer.get(gerWords.get(i));
count.put(tempKey, tempValC+tempNew);
total.put(engWords.get(j), tempValTOTAL+tempNew);
}
}
}
germanFile2.close();
englishFile2.close();
for(String key : count.keySet()){
String temp = key.substring(key.indexOf("|")+1,key.length());
double tmp = total.get(temp);
double tempVal = count.get(key)/tmp;
probsT.put(key, tempVal);
}
}
ArrayList<String> testArray = new ArrayList<String>();
BufferedReader testFile = new BufferedReader(new FileReader(testFileloc));
String line = "";
while((line=testFile.readLine())!=null){
testArray.add(line.trim());
}
testFile.close();
HashMap<String,HashMap<String,Double>> maps = new HashMap<String,HashMap<String,Double>>();
for(int i=0;i<testArray.size();++i){
HashMap<String,Double> temp = new HashMap<String,Double>();
maps.put(testArray.get(i), temp);
}
for(String k : probsT.keySet()){
if(testArray.contains(k.substring(k.indexOf('|')+1, k.length()))){
String german = k.substring(0,k.indexOf('|'));
double probValue = probsT.get(k);
HashMap<String,Double> temp2 = maps.get(k.substring(k.indexOf('|')+1, k.length()));
temp2.put(german, probValue);
maps.put(k.substring(k.indexOf('|')+1, k.length()), temp2);
}
}
IBMMODEL1 ib = new IBMMODEL1();
for(String k : maps.keySet()){
HashMap<String,Double> te = maps.get(k);
HashMap<String,Double> newMap = ib.sortByValue(te);
maps.put(k, newMap);
}
for(String k : maps.keySet()){
System.out.println("For "+k);
HashMap<String,Double> te = maps.get(k);
int counter = 0;
System.out.println("TRANSLATION\tPROBABILITY");
for(String ke : te.keySet()){
if (counter >= te.size()-5){
System.out.println(ke + " \t " + te.get(ke));
}
counter++;
}
System.out.println("\n");
}
}catch(IOException e){e.printStackTrace();}
}
/**
* Function to sort a HashMap according to values
* @param map
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private HashMap<String,Double> sortByValue(Map<String,Double> map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
HashMap result = new LinkedHashMap<String,Double>();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}