-
Notifications
You must be signed in to change notification settings - Fork 0
/
NgramSenseRelateReader.java
88 lines (76 loc) · 2.19 KB
/
NgramSenseRelateReader.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
import java.io.*;
import java.util.*;
import java.util.regex.*;
/* This is the class that handles the combined output
* from the merging of the N-gram output and the
* SenseRelate output files.
*/
public class NgramSenseRelateReader {
private BufferedReader br;
private ArrayList<String> words;
private HashMap<String,ArrayList<String>> instances = new HashMap<String,ArrayList<String>>();
public NgramSenseRelateReader(String file) {
try {
br = new BufferedReader(new FileReader(file));
String line = null;
String id = null;
while((line = br.readLine()) != null) {
if (line.matches("^[\\d]*:")) {
//System.out.println(line);
id = line.substring(0,line.length() - 1); //ommit the :
words = new ArrayList<String>();
} else {
Scanner sc = new Scanner(line);
String trip = sc.findInLine(Pattern.compile("[a-zA-Z]+#[a-z]+[#]?[0-9]*"));
sc.close();
if (trip != null) {
// We need the whole line not just the triplet
words.add(line.trim());
instances.put(id, words);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getPolarityFor(String id, String word) {
ArrayList<String> wdList = instances.get(id);
String result = null;
for(String triplet : wdList) {
if (triplet.contains(word)) {
result = triplet.substring(triplet.length() - 3);
}
}
return result;
}
public String getPosFor(String id, String word) {
ArrayList<String> wdList = instances.get(id);
String result = null;
for(String triplet : wdList) {
if (triplet.contains(word)) {
result = triplet.split("#")[1];
}
}
return result;
}
public boolean isValence(String id, String word) {
ArrayList<String> wdList = instances.get(id);
boolean result = false;
for(String triplet : wdList) {
if (triplet.contains(word)) {
result = triplet.contains("valence");
}
}
return result;
}
public HashMap<String,ArrayList<String>> getSentences() {
return instances;
}
}