-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathStanfordProblem.java
More file actions
213 lines (197 loc) · 6.61 KB
/
Copy pathStanfordProblem.java
File metadata and controls
213 lines (197 loc) · 6.61 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
package structure;
import edu.illinois.cs.cogcomp.annotation.AnnotatorException;
import edu.illinois.cs.cogcomp.core.datastructures.IntPair;
import edu.illinois.cs.cogcomp.core.datastructures.Pair;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.util.CoreMap;
import reader.Reader;
import utils.Tools;
import java.util.*;
public class StanfordProblem {
public int id;
public String question;
public Double answer;
public List<QuantSpan> quantities;
public Node expr;
public List<List<CoreLabel>> tokens;
public List<SemanticGraph> dependencies;
public List<StanfordSchema> schema;
public StanfordSchema questionSchema;
public Map<Pair<String, String>, String> wordnetRelations;
public List<Integer> rates;
@Override
public String toString() {
String str = "";
str += "\nQuestion : "+question+"\nAnswer : "+answer +
"\nQuantities : "+Arrays.asList(quantities) +
"\nExpression : "+expr;
return str;
}
public StanfordProblem(int id, String q, double a) throws AnnotatorException {
this.id = id;
question = q;
answer = a;
quantities = new ArrayList<>();
}
public void extractQuantities() throws Exception {
quantities = Tools.quantifier.getSpans(question);
}
public void extractAnnotations() throws Exception {
List<CoreMap> sentences = Tools.annotateWithStanfordCoreNLP(question);
tokens = new ArrayList<>();
dependencies = new ArrayList<>();
for(CoreMap sentence: sentences) {
tokens.add(sentence.get(CoreAnnotations.TokensAnnotation.class));
dependencies.add(sentence.get(SemanticGraphCoreAnnotations.
CollapsedCCProcessedDependenciesAnnotation.class));
}
schema = new ArrayList<>();
for(int i=0; i<quantities.size(); ++i) {
StanfordSchema ss = new StanfordSchema(this, i);
if(ss.unit.equals(ss.rate)) {
ss.rate = new IntPair(-1, -1);
}
if(ss.unit.equals(ss.object)) {
ss.object = new IntPair(-1, -1);
}
if(ss.unit.equals(ss.subject)) {
ss.subject = new IntPair(-1, -1);
}
schema.add(ss);
}
questionSchema = getQuestionSchema(this);
if(questionSchema.unit.equals(questionSchema.rate)) {
questionSchema.unit = new IntPair(-1, -1);
}
wordnetRelations = getWordnetRelations();
}
public StanfordSchema getQuestionSchema(StanfordProblem prob) {
StanfordSchema schema = new StanfordSchema();
List<String> whWords = Arrays.asList("what", "how", "when");
schema.tokens = prob.tokens;
List<CoreLabel> tokens = null;
for(int i=0; i<prob.tokens.size(); ++i) {
List<CoreLabel> sentence = prob.tokens.get(i);
for(CoreLabel token : sentence) {
if(token.word().equals("?") || whWords.contains(token.lemma())) {
tokens = sentence;
schema.sentId = i;
break;
}
}
}
if (tokens == null) {
return schema;
}
IntPair quesSpan = getQuestionSpan(tokens);
SemanticGraph dependency = prob.dependencies.get(schema.sentId);
schema.verb = schema.getDependentVerb(tokens, dependency, quesSpan.getFirst());
schema.rate = getRateForQuestion(tokens, quesSpan.getFirst());
schema.subject = schema.getSubject(tokens, dependency, schema.verb);
schema.object = schema.getObject(tokens, dependency, schema.verb);
Pair<IntPair, IntPair> unitPair = schema.getUnit(tokens, quesSpan.getFirst());
schema.unit = unitPair.getFirst();
if(unitPair.getSecond().getFirst() >= 0) {
schema.object = unitPair.getSecond();
}
Pair<Integer, IntPair> mathPair = schema.getMath(tokens, quesSpan.getFirst(), true);
if(mathPair.getFirst() >= 0) {
schema.math = mathPair.getFirst();
schema.object = mathPair.getSecond();
}
return schema;
}
public static IntPair getQuestionSpan(List<CoreLabel> tokens) {
List<String> whWords = Arrays.asList("what", "how", "when");
int start = 0, end = tokens.size();
for(int i=0; i<tokens.size(); ++i) {
if(whWords.contains(tokens.get(i).lemma())) {
start = i;
}
}
for(int i=start+1; i<tokens.size(); ++i) {
if(tokens.get(i).word().equals("?") || tokens.get(i).word().equals("if") ||
tokens.get(i).word().equals(",")) {
end = i;
}
}
return new IntPair(start, end);
}
public Map<Pair<String, String>, String> getWordnetRelations() {
Map<Pair<String, String>, String> map = new HashMap<>();
for(List<CoreLabel> sent1 : tokens) {
for(CoreLabel word1 : sent1) {
for (List<CoreLabel> sent2 : tokens) {
for (CoreLabel word2 : sent2) {
if(word1.lemma().equals(word2.lemma())) {
continue;
}
String wn = Tools.wordNetIndicator(
word1.lemma(), word2.lemma(), word1.tag(), word2.tag());
if(wn != null) {
map.put(new Pair<>(word1.lemma(), word2.lemma()), wn);
}
}
}
}
}
return map;
}
public static void main(String args[]) throws Exception {
Tools.initStanfordTools();
List<StanfordProblem> probs =
Reader.readStanfordProblemsFromJson();
for(StanfordProblem prob : probs) {
System.out.println(prob.id);
System.out.println(prob);
for(List<CoreLabel> tokens : prob.tokens) {
for(CoreLabel token : tokens) {
System.out.print("["+token.tag()+"_"+token.lemma()+"]");
}
}
System.out.println();
for(StanfordSchema schema : prob.schema) {
System.out.println(schema);
}
System.out.println(prob.questionSchema);
System.out.println();
}
}
public static IntPair getRateForQuestion(List<CoreLabel> tokens, int tokenId) {
for(int i=tokenId+1; i<tokens.size(); ++i) {
if(tokens.get(i).word().equals("if") || tokens.get(i).word().equals("and") ||
tokens.get(i).word().equals(",") || tokens.get(i).word().equals(";")) {
break;
}
if (tokens.get(i).lemma().equals("per") ||
tokens.get(i).lemma().equals("every") ||
tokens.get(i).lemma().equals("each") ||
tokens.get(i).lemma().equals("1.0")) {
for(int j=i+1; j<tokens.size(); ++j) {
if(tokens.get(j).word().equals("if") || tokens.get(j).word().equals("and")
|| tokens.get(j).word().equals(",") || tokens.get(j).word().equals(";")
|| tokens.get(j).tag().equals("CD")) {
break;
}
if (tokens.get(j).tag().startsWith("N") ||
tokens.get(j).tag().startsWith("PRP")) {
return Tools.getMaximalNounPhraseSpan(tokens, j);
}
}
for(int j=tokenId-1; j>=0; --j) {
if (tokens.get(j).word().equals("if") || tokens.get(j).word().equals("and")
|| tokens.get(j).word().equals(",") || tokens.get(j).word().equals(";")) {
break;
}
if (tokens.get(j).tag().startsWith("N")) {
return Tools.getMaximalNounPhraseSpan(tokens, j);
}
}
}
}
return new IntPair(-1, -1);
}
}