1   package eu.fbk.dkm.pikes.resources;
2   
3   import java.io.*;
4   import java.nio.charset.Charset;
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   
8   public class PredicateMatrix {
9   
10  
11  	final private int ID_LANG = 0;
12  	final private int ID_POS = 1;
13  	final private int ID_PRED = 2;
14  	final private int ID_ROLE = 3;
15  	final private int VN_CLASS = 4;
16  	final private int VN_CLASS_NUMBER = 5;
17  	final private int VN_SUBCLASS = 6;
18  	final private int VN_SUBCLASS_NUMBER = 7;
19  	final private int VN_LEMA = 8;
20  	final private int VN_ROLE = 9;
21  	final private int WN_SENSE = 10;
22  	final private int MCR_iliOffset = 11;
23  	final private int FN_FRAME = 12;
24  	final private int FN_LE = 13;
25  	final private int FN_FRAME_ELEMENT = 14;
26  	final private int PB_ROLESET = 15;
27  	final private int PB_ARG = 16;
28  	final private int MCR_BC = 17;
29  	final private int MCR_DOMAIN = 18;
30  	final private int MCR_SUMO = 19;
31  	final private int MCR_TO = 20;
32  	final private int MCR_LEXNAME = 21;
33  	final private int MCR_BLC = 22;
34  	final private int WN_SENSEFREC = 23;
35  	final private int WN_SYNSET_REL_NUM = 24;
36  	final private int ESO_CLASS = 25;
37  	final private int ESO_ROLE = 26;
38  	final private int NWREVENT = 27;
39  
40  
41  	private HashMap<String, ArrayList<String>> vnClass = new HashMap<String, ArrayList<String>>();
42  	private HashMap<String, ArrayList<String>> vnSubClass = new HashMap<String, ArrayList<String>>();
43  	private HashMap<String, ArrayList<String>> fnFrame = new HashMap<String, ArrayList<String>>();
44  	private HashMap<String, ArrayList<String>> pbPredicate = new HashMap<String, ArrayList<String>>();
45  	private HashMap<String, ArrayList<String>> esoClass = new HashMap<String, ArrayList<String>>();
46  	private HashMap<String, ArrayList<String>> eventType = new HashMap<String, ArrayList<String>>();
47  	private HashMap<String, ArrayList<String>> wnSense = new HashMap<String, ArrayList<String>>();
48  
49  	private HashMap<String, ArrayList<String>> vnToFn = new HashMap<String, ArrayList<String>>();
50  
51  	private HashMap<String, ArrayList<String>> vnThematicRole = new HashMap<String, ArrayList<String>>();
52  	private HashMap<String, ArrayList<String>> fnFrameElement = new HashMap<String, ArrayList<String>>();
53  	private HashMap<String, ArrayList<String>> pbArgument = new HashMap<String, ArrayList<String>>();
54  	private HashMap<String, ArrayList<String>> esoRole = new HashMap<String, ArrayList<String>>();
55  
56  
57  
58  
59  	public PredicateMatrix(String modelFile) {
60  		try {
61  			BufferedReader pmReader = new BufferedReader(new InputStreamReader(new FileInputStream(modelFile), Charset.forName("UTF-8")));
62  
63  			String pmLine;
64  			String[] pmFields;
65  			
66  			while ((pmLine = pmReader.readLine()) != null) {
67  				pmFields = pmLine.split("\t");
68  
69  //				for (int i=0;i<pmFields.length;i++)
70  				
71  				
72  				
73  //				System.out.println(Arrays.toString(pmFields));
74  				//skip header
75  				if (!pmFields[ID_LANG].equals("1_ID_LANG")) {
76  					//consider only those line for english verbs
77  					if ((pmFields[ID_LANG].equals("id:eng"))&&(pmFields[ID_POS].equals("id:v"))) {
78  
79  						//the entry key for us is the propbank roleset
80  
81  						//remove namespaces
82  						for (int i=2;i<pmFields.length;i++)
83  							pmFields[i]=removePredicateMatrixNamespace(pmFields[i]);
84  
85  						
86  
87  						if (!pmFields[PB_ROLESET].equals("NULL")) {
88  
89  
90  							//VN class
91  
92  							String pb_roleset = pmFields[PB_ROLESET];
93  
94  							if ((!pmFields[VN_CLASS].equals("NULL"))&&(!pmFields[VN_LEMA].equals("NULL"))) {
95  
96  								String vnFull =  pmFields[VN_LEMA]+ "-" +pmFields[VN_CLASS];
97  								ArrayList<String> array = new ArrayList<String>();
98  								if (vnClass.containsKey(pb_roleset)) {
99  									array = vnClass.get(pb_roleset);
100 								}
101 								if (newElement(array, vnFull)) {
102 									array.add(vnFull);
103 								}
104 								vnClass.put(pb_roleset, array);
105 							}
106 
107 							//VN subclass
108 							if ((!pmFields[VN_SUBCLASS].equals("NULL"))&&(!pmFields[VN_LEMA].equals("NULL"))) {
109 
110 								String vnFull =  pmFields[VN_LEMA]+ "-" +pmFields[VN_SUBCLASS];
111 								ArrayList<String> array = new ArrayList<String>();
112 								if (vnSubClass.containsKey(pb_roleset)) {
113 									array = vnSubClass.get(pb_roleset);
114 								}
115 								if (newElement(array, vnFull)) {
116 									array.add(vnFull);
117 								}
118 								vnSubClass.put(pb_roleset, array);
119 
120 								//vn2fn mappings only here because at the level of subclass????
121 								if (!vnToFn.containsKey(vnFull)) {
122 									vnToFn.put(vnFull, new ArrayList<>());
123 								}
124 								if (!vnToFn.get(vnFull).contains(pmFields[FN_FRAME]) && !pmFields[FN_FRAME].equals("NULL")) {
125 									vnToFn.get(vnFull).add(pmFields[FN_FRAME]);
126 									//							System.out.println(pmFields[6]);
127 									//							System.out.println(pmFields[12]);
128 									//							System.out.println();
129 								}
130 							}
131 
132 							//frameNet frames
133 							if (!pmFields[FN_FRAME].equals("NULL")) {
134 								ArrayList<String> array = new ArrayList<String>();
135 								if (fnFrame.containsKey(pb_roleset)) {
136 									array = fnFrame.get(pb_roleset);
137 								}
138 								if (newElement(array, pmFields[FN_FRAME])) {
139 									array.add(pmFields[FN_FRAME]);
140 								}
141 								fnFrame.put(pb_roleset, array);
142 							}
143 
144 
145 							//pb next block is a hack, may not be needed...
146 							if (!pb_roleset.equals("NULL")) {
147 								ArrayList<String> array = new ArrayList<String>();
148 								if (pbPredicate.containsKey(pb_roleset)) {
149 									array = pbPredicate.get(pb_roleset);
150 								}
151 								if (newElement(array, pb_roleset)) {
152 									array.add(pb_roleset);
153 								}
154 								pbPredicate.put(pb_roleset, array);
155 							}
156 
157 							//ESO
158 							if (!pmFields[ESO_CLASS].equals("NULL")) {
159 								ArrayList<String> array = new ArrayList<String>();
160 								if (esoClass.containsKey(pb_roleset)) {
161 									array = esoClass.get(pb_roleset);
162 								}
163 								if (newElement(array, pmFields[ESO_CLASS])) {
164 									array.add(pmFields[ESO_CLASS]);
165 								}
166 								esoClass.put(pb_roleset, array);
167 							}
168 
169 
170 //							not any more in PM 1.3
171 //							if (!pmFields[NWREVENT].equals("NULL")) {
172 //								ArrayList<String> array = new ArrayList<String>();
173 //								if (eventType.containsKey(pmFields[2])) {
174 //									array = eventType.get(pmFields[2]);
175 //								}
176 //								if (newElement(array, pmFields[27])) {
177 //									array.add(pmFields[27]);
178 //								}
179 //								eventType.put(pmFields[2], array);
180 //							}
181 
182 							if (!pmFields[MCR_iliOffset].equals("NULL")) {
183 								ArrayList<String> array = new ArrayList<String>();
184 								if (wnSense.containsKey(pb_roleset)) {
185 									array = wnSense.get(pb_roleset);
186 								}
187 								if (newElement(array, pmFields[MCR_iliOffset])) {
188 									array.add(pmFields[MCR_iliOffset]);
189 								}
190 								wnSense.put(pb_roleset, array);
191 							}
192 
193 
194 							//ROLES
195 							//PB_ARG has to be not null
196 							if (!pmFields[PB_ARG].equals("NULL")) {
197 
198 								String pb_arg = fixPbArg(pmFields[PB_ARG]);
199 
200 
201 
202 
203 								//wn thematic role
204 								if (!pmFields[VN_ROLE].equals("NULL")) {
205 									ArrayList<String> array = new ArrayList<String>();
206 									if (vnThematicRole.containsKey(pb_roleset + "@"
207 											+ pb_arg)) {
208 										array = vnThematicRole.get(pb_roleset + "@"
209 												+ pb_arg);
210 									}
211 
212 									//add both to VN_CLASS and VN_SUBCLASS
213 
214 									if ((!pmFields[VN_CLASS].equals("NULL"))&&(!pmFields[VN_LEMA].equals("NULL"))) {
215 										String vnFull =  pmFields[VN_LEMA]+ "-" +pmFields[VN_CLASS];
216 										if (newElement(array, vnFull + "@"
217 												+ pmFields[VN_ROLE])) {
218 											array.add(vnFull + "@" + pmFields[VN_ROLE]);
219 										}
220 									}
221 
222 									if ((!pmFields[VN_SUBCLASS].equals("NULL"))&&(!pmFields[VN_LEMA].equals("NULL"))) {
223 										String vnFull =  pmFields[VN_LEMA]+ "-" +pmFields[VN_SUBCLASS];
224 										if (newElement(array, vnFull + "@"
225 												+ pmFields[VN_ROLE])) {
226 											array.add(vnFull + "@" + pmFields[VN_ROLE]);
227 										}
228 									}
229 
230 									vnThematicRole.put(pb_roleset + "@"
231 											+ pb_arg, array);
232 								}
233 
234 								//fn fe
235 								if (!pmFields[FN_FRAME_ELEMENT].equals("NULL")) {
236 									ArrayList<String> array = new ArrayList<String>();
237 									if (fnFrameElement.containsKey(pb_roleset + "@"
238 											+ pb_arg)) {
239 										array = fnFrameElement.get(pb_roleset + "@"
240 												+ pb_arg);
241 									}
242 
243 									if (!pmFields[FN_FRAME].equals("NULL")) {
244 
245 										if (newElement(array, pmFields[FN_FRAME] + "@"
246 												+ pmFields[FN_FRAME_ELEMENT])) {
247 											array.add(pmFields[FN_FRAME] + "@"
248 													+ pmFields[FN_FRAME_ELEMENT]);
249 										}
250 
251 									}
252 
253 									fnFrameElement.put(pb_roleset + "@"
254 											+ pb_arg, array);
255 								}
256 
257 
258 
259 
260 
261 								if (!pb_arg.equals("NULL")) {
262 									ArrayList<String> array = new ArrayList<String>();
263 									if (pbArgument.containsKey(pb_roleset + "@"
264 											+ pb_arg)) {
265 										array = pbArgument.get(pb_roleset + "@"
266 												+ pb_arg);
267 									}
268 									if (newElement(array, pb_roleset + "@"
269 											+ pb_arg)) {
270 										array.add(pb_roleset + "@"
271 												+ pb_arg);
272 									}
273 									pbArgument.put(pb_roleset + "@"
274 											+ pb_arg, array);
275 								}
276 
277 								//eso
278 								if (!pmFields[ESO_ROLE].equals("NULL")) {
279 									ArrayList<String> array = new ArrayList<String>();
280 									if (esoRole.containsKey(pb_roleset + "@"
281 											+ pb_arg)) {
282 										array = esoRole.get(pb_roleset + "@"
283 												+ pb_arg);
284 									}
285 									if (!pmFields[ESO_CLASS].equals("NULL")) {
286 										if (newElement(array, pmFields[ESO_CLASS] + "@" + pmFields[ESO_ROLE])) {
287 											array.add(pmFields[ESO_CLASS] + "@" + pmFields[ESO_ROLE]);
288 										}
289 									}
290 									esoRole.put(pb_roleset + "@"
291 											+ pb_arg, array);
292 								}
293 							}
294 						}
295 					}
296 				}
297 			}
298 
299 			pmReader.close();
300 
301 		} catch (FileNotFoundException e) {
302 			// TODO Auto-generated catch block
303 			e.printStackTrace();
304 		} catch (IOException e) {
305 			e.printStackTrace();
306 		}
307 	}
308 
309 	public ArrayList<String> getVNClasses(String PBSense) {
310 		ArrayList<String> array = new ArrayList<String>();
311 		if (vnClass.containsKey(PBSense)) {
312 			array = vnClass.get(PBSense);
313 		}
314 		return array;
315 	}
316 
317 	public ArrayList<String> getVNClassesToFN(String vnSense) {
318 		ArrayList<String> array = new ArrayList<String>();
319 		if (vnToFn.containsKey(vnSense)) {
320 			array = vnToFn.get(vnSense);
321 		}
322 		return array;
323 	}
324 
325 	public ArrayList<String> getVNSubClasses(String PBSense) {
326 		ArrayList<String> array = new ArrayList<String>();
327 		if (vnSubClass.containsKey(PBSense)) {
328 			array = vnSubClass.get(PBSense);
329 		}
330 		return array;
331 	}
332 
333 	public ArrayList<String> getFNFrames(String PBSense) {
334 		ArrayList<String> array = new ArrayList<String>();
335 		if (fnFrame.containsKey(PBSense)) {
336 			array = fnFrame.get(PBSense);
337 		}
338 		return array;
339 	}
340 
341 	public ArrayList<String> getPBPredicates(String PBSense) {
342 		ArrayList<String> array = new ArrayList<String>();
343 		if (pbPredicate.containsKey(PBSense)) {
344 			array = pbPredicate.get(PBSense);
345 		}
346 		return array;
347 	}
348 
349 	public ArrayList<String> getESOClasses(String PBSense) {
350 		ArrayList<String> array = new ArrayList<String>();
351 		if (esoClass.containsKey(PBSense)) {
352 			array = esoClass.get(PBSense);
353 		}
354 		return array;
355 	}
356 
357 	public ArrayList<String> getEventTypes(String PBSense) {
358 		ArrayList<String> array = new ArrayList<String>();
359 		if (eventType.containsKey(PBSense)) {
360 			array = eventType.get(PBSense);
361 		}
362 		return array;
363 	}
364 
365 	public ArrayList<String> getWNSenses(String PBSense) {
366 		ArrayList<String> array = new ArrayList<String>();
367 		if (wnSense.containsKey(PBSense)) {
368 			array = wnSense.get(PBSense);
369 		}
370 		return array;
371 	}
372 
373 	public ArrayList<String> getVNThematicRoles(String PBSenseArgument) {
374 		ArrayList<String> array = new ArrayList<String>();
375 		if (vnThematicRole.containsKey(PBSenseArgument)) {
376 			array = vnThematicRole.get(PBSenseArgument);
377 		}
378 		return array;
379 	}
380 
381 	public ArrayList<String> getFNFrameElements(String PBSenseArgument) {
382 		ArrayList<String> array = new ArrayList<String>();
383 		if (fnFrameElement.containsKey(PBSenseArgument)) {
384 			array = fnFrameElement.get(PBSenseArgument);
385 		}
386 		return array;
387 	}
388 
389 	public ArrayList<String> getPBArguments(String PBSenseArgument) {
390 		ArrayList<String> array = new ArrayList<String>();
391 		if (pbArgument.containsKey(PBSenseArgument)) {
392 			array = pbArgument.get(PBSenseArgument);
393 		}
394 		return array;
395 	}
396 
397 	public ArrayList<String> getESORoles(String PBSenseArgument) {
398 		ArrayList<String> array = new ArrayList<String>();
399 		if (esoRole.containsKey(PBSenseArgument)) {
400 			array = esoRole.get(PBSenseArgument);
401 		}
402 		return array;
403 	}
404 
405 	private boolean newElement(ArrayList<String> array, String element) {
406 		boolean isNew = true;
407 		for (int i = 0; i < array.size(); i++) {
408 			if (array.get(i).toString().equals(element)) {
409 				isNew = false;
410 			}
411 		}
412 		return isNew;
413 	}
414 
415 
416 	public static String removePredicateMatrixNamespace(String entry) {
417 
418 		if (!entry.equals("NULL")) {
419 			int index = entry.indexOf( ':' );
420 			entry=entry.substring(index+1);
421 		}
422 		return entry;
423 
424 	}
425 
426 	public static String fixPbArg(String entry) {
427 
428 
429 		entry=entry.replace("C-","").replace("R-","");
430 		if (entry.equals("DV")) entry="ADV";
431 		if (entry.length()==1) entry="A"+entry;  //0,1,...,5 cases
432 		if (entry.length()==3) entry="AM-"+entry;  //DIS,TMP,MNR,... cases
433 		return entry;
434 
435 	}
436 
437 }