-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRelFeatGen.java
More file actions
212 lines (201 loc) · 7.19 KB
/
Copy pathRelFeatGen.java
File metadata and controls
212 lines (201 loc) · 7.19 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
package relevance;
import java.io.Serializable;
import java.util.*;
import structure.QuantSpan;
import structure.QuantitySchema;
import utils.FeatGen;
import utils.Tools;
import edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent;
import edu.illinois.cs.cogcomp.sl.core.AbstractFeatureGenerator;
import edu.illinois.cs.cogcomp.sl.core.IInstance;
import edu.illinois.cs.cogcomp.sl.core.IStructure;
import edu.illinois.cs.cogcomp.sl.util.IFeatureVector;
import edu.illinois.cs.cogcomp.sl.util.Lexiconer;
public class RelFeatGen extends AbstractFeatureGenerator implements Serializable{
private static final long serialVersionUID = -5902462551801564955L;
public Lexiconer lm = null;
public RelFeatGen(Lexiconer lm) {
this.lm = lm;
}
@Override
public IFeatureVector getFeatureVector(IInstance prob, IStructure struct) {
RelX x = (RelX) prob;
RelY y = (RelY) struct;
List<String> featuresWithPrefix = new ArrayList<String>();
String prefix = y.relevance;
featuresWithPrefix.add(prefix);
List<String> features = getFeatures(x);
features.addAll(FeatGen.getConjunctions(features));
for(String feature : features) {
featuresWithPrefix.add(prefix + "_" + feature);
}
return FeatGen.getFeatureVectorFromListString(featuresWithPrefix, lm);
}
public static List<String> getFeatures(RelX x) {
List<String> features = new ArrayList<String>();
features.addAll(unitFeatures(x));
features.addAll(miscFeatures(x));
features.addAll(connectedNPFeatures(x));
return features;
}
public static List<String> unitFeatures(RelX x) {
List<String> features = new ArrayList<String>();
QuantitySchema qSchema = x.schema.quantSchemas.get(x.quantIndex);
int bestMatchQuestion = 0, bestMatchQuestionIndex = 0, numBestMatches = 0;
bestMatchQuestionIndex = Tools.getNumTokenMatches(
x.schema.questionTokens,
Arrays.asList(qSchema.unit.split(" ")));
for(QuantitySchema qs : x.schema.quantSchemas) {
int numMatches = Tools.getNumTokenMatches(
x.schema.questionTokens,Arrays.asList(qs.unit.split(" ")));
if(bestMatchQuestion < numMatches) {
bestMatchQuestion = numMatches;
numBestMatches = 1;
} else if(bestMatchQuestion == numMatches) {
numBestMatches++;
}
}
if(bestMatchQuestionIndex == 0) {
features.add("UnitFoundInQuestion");
}
if(bestMatchQuestion == bestMatchQuestionIndex) {
features.add("BestQuantUnitMatchInQuestion");
}
if(numBestMatches > 1) {
features.add("MultipleQuantUnitBestMatchInQuestion");
}
for(int i=0; i<x.schema.quantSchemas.size(); ++i) {
if(i!=x.quantIndex && qSchema.unit.trim().equals(
x.schema.quantSchemas.get(i).unit.trim())) {
features.add("ExactMatchUnit");
break;
}
}
for(int i=0; i<x.schema.quantSchemas.size(); ++i) {
for(int j=i+1; j<x.schema.quantSchemas.size(); ++j) {
if(i!=x.quantIndex && j!=x.quantIndex &&
x.schema.quantSchemas.get(i).unit.trim().equals(
x.schema.quantSchemas.get(j).unit.trim())) {
features.add("OtherPairExactMatchUnit");
break;
}
}
}
int bestMatch = 0;
for(int i=0; i<x.schema.quantSchemas.size(); ++i) {
for(int j=i+1; j<x.schema.quantSchemas.size(); ++j) {
String unit1 = x.schema.quantSchemas.get(i).unit.trim();
String unit2 = x.schema.quantSchemas.get(j).unit.trim();
int match = Tools.getNumTokenMatches(Arrays.asList(unit1.split(" ")),
Arrays.asList(unit2.split(" ")));
if(match > bestMatch) {
bestMatch = match;
}
}
}
int bestMatchForIndex = 0;
for(int i=0; i<x.schema.quantSchemas.size(); ++i) {
if(i==x.quantIndex) continue;
String unit1 = x.schema.quantSchemas.get(i).unit.trim();
String unit2 = qSchema.unit.trim();
int match = Tools.getNumTokenMatches(Arrays.asList(unit1.split(" ")),
Arrays.asList(unit2.split(" ")));
if(match > bestMatchForIndex) {
bestMatchForIndex = match;
}
}
if(bestMatchForIndex == 0) {
features.add("NoMatchWithOtherQuantUnits");
}
if(bestMatch == bestMatchForIndex) {
features.add("BestMatchAmongQuantUnit");
}
return features;
}
public static List<String> connectedNPFeatures(RelX x) {
List<String> features = new ArrayList<String>();
QuantitySchema qSchema = x.schema.quantSchemas.get(x.quantIndex);
int bestMatchQuestion = 0, bestMatchQuestionIndex = 0;
List<String> allQuantTokens = new ArrayList<String>(
Arrays.asList(qSchema.unit.split(" ")));
for(Constituent c : qSchema.connectedNPs) {
allQuantTokens.addAll(Tools.getTokensList(c));
}
bestMatchQuestionIndex = Tools.getNumTokenMatches(
x.schema.questionTokens, allQuantTokens);
for(QuantitySchema qs : x.schema.quantSchemas) {
allQuantTokens = new ArrayList<String>(Arrays.asList(qs.unit.split(" ")));
for(Constituent c : qs.connectedNPs) {
allQuantTokens.addAll(Tools.getTokensList(c));
}
bestMatchQuestion = Tools.getNumTokenMatches(x.schema.questionTokens, allQuantTokens);
}
if(bestMatchQuestionIndex == 0) {
features.add("UnitNPFoundInQuestion");
}
if(bestMatchQuestion == bestMatchQuestionIndex) {
features.add("BestQuantUnitNPMatchInQuestion");
}
int bestMatch = 0;
for(int i=0; i<x.schema.quantSchemas.size(); ++i) {
for(int j=i+1; j<x.schema.quantSchemas.size(); ++j) {
QuantitySchema qs1 = x.schema.quantSchemas.get(i);
QuantitySchema qs2 = x.schema.quantSchemas.get(j);
List<String> allQuantTokens1 = new ArrayList<String>(
Arrays.asList(qs1.unit.split(" ")));
for(Constituent c : qs1.connectedNPs) {
allQuantTokens1.addAll(Tools.getTokensList(c));
}
List<String> allQuantTokens2 = new ArrayList<String>(
Arrays.asList(qs2.unit.split(" ")));
for(Constituent c : qs2.connectedNPs) {
allQuantTokens2.addAll(Tools.getTokensList(c));
}
int match = Tools.getNumTokenMatches(allQuantTokens1, allQuantTokens2);
if(match > bestMatch) {
bestMatch = match;
}
}
}
int bestMatchForIndex = 0;
for(int i=0; i<x.schema.quantSchemas.size(); ++i) {
if(i==x.quantIndex) continue;
QuantitySchema qs1 = x.schema.quantSchemas.get(i);
QuantitySchema qs2 = qSchema;
List<String> allQuantTokens1 = new ArrayList<String>(
Arrays.asList(qs1.unit.split(" ")));
for(Constituent c : qs1.connectedNPs) {
allQuantTokens1.addAll(Tools.getTokensList(c));
}
List<String> allQuantTokens2 = new ArrayList<String>(
Arrays.asList(qs2.unit.split(" ")));
for(Constituent c : qs2.connectedNPs) {
allQuantTokens2.addAll(Tools.getTokensList(c));
}
int match = Tools.getNumTokenMatches(allQuantTokens1, allQuantTokens2);
if(match > bestMatchForIndex) {
bestMatchForIndex = match;
}
}
if(bestMatchForIndex == 0) {
features.add("NoMatchNPWithOtherQuantUnits");
}
if(bestMatch == bestMatchForIndex) {
features.add("BestMatchNPAmongQuantUnit");
}
return features;
}
public static List<String> miscFeatures(RelX x) {
QuantSpan qs = x.quantities.get(x.quantIndex);
List<String> features = new ArrayList<String>();
if(x.quantities.size() == 2) {
features.add("Two_quantities");
}
if(Tools.safeEquals(qs.val, 1.0) || Tools.safeEquals(qs.val, 2.0)) {
features.add("One_or_Two");
}
features.addAll(FeatGen.getNeighborhoodFeatures(x.ta, x.posTags,
x.ta.getTokenIdFromCharacterOffset(qs.start), 2));
return features;
}
}