-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFeatGen.java
More file actions
171 lines (156 loc) · 5.39 KB
/
Copy pathFeatGen.java
File metadata and controls
171 lines (156 loc) · 5.39 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
package utils;
import java.util.ArrayList;
import java.util.List;
import edu.illinois.cs.cogcomp.core.datastructures.Pair;
import edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent;
import edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation;
import edu.illinois.cs.cogcomp.sl.util.FeatureVectorBuffer;
import edu.illinois.cs.cogcomp.sl.util.IFeatureVector;
import edu.illinois.cs.cogcomp.sl.util.Lexiconer;
import edu.stanford.nlp.ling.CoreLabel;
public class FeatGen {
public static IFeatureVector getFeatureVectorFromListString(
List<String> features, Lexiconer lm) {
FeatureVectorBuffer fvb = new FeatureVectorBuffer();
for(String feature : features) {
if(!lm.containFeature(feature) && lm.isAllowNewFeatures()) {
lm.addFeature(feature);
}
if(lm.containFeature(feature)) {
fvb.addFeature(lm.getFeatureId(feature), 1.0);
}
}
return fvb.toFeatureVector();
}
public static IFeatureVector getFeatureVectorFromListPair(
List<Pair<String, Double>> features, Lexiconer lm) {
FeatureVectorBuffer fvb = new FeatureVectorBuffer();
for(Pair<String, Double> feature : features) {
if(!lm.containFeature(feature.getFirst()) && lm.isAllowNewFeatures()) {
lm.addFeature(feature.getFirst());
}
if(lm.containFeature(feature.getFirst())) {
fvb.addFeature(lm.getFeatureId(feature.getFirst()), feature.getSecond());
}
}
return fvb.toFeatureVector();
}
public static List<String> getConjunctions(List<String> features) {
List<String> conjunctions = new ArrayList<String>();
for(int i=0; i<features.size(); ++i) {
for(int j=i+1; j<features.size(); ++j) {
conjunctions.add(features.get(i)+"_"+features.get(j));
}
}
return conjunctions;
}
public static List<String> getConjunctions(
List<String> features1, List<String> features2) {
List<String> conjunctions = new ArrayList<String>();
for(String feature1 : features1) {
for(String feature2 : features2) {
conjunctions.add(feature1+"_"+feature2);
}
}
return conjunctions;
}
public static List<Pair<String, Double>> getConjunctionsWithPairs(
List<Pair<String, Double>> features1, List<Pair<String, Double>> features2) {
List<Pair<String, Double>> conjunctions = new ArrayList<Pair<String, Double>>();
for(Pair<String, Double> feature1 : features1) {
for(Pair<String, Double> feature2 : features2) {
conjunctions.add(new Pair<String, Double>(
feature1.getFirst()+"_"+feature2.getFirst(),
feature1.getSecond()*feature2.getSecond()));
}
}
return conjunctions;
}
public static List<String> getUnigramBigramFeatures(
TextAnnotation ta, List<Constituent> posTags, int start, int end) {
List<String> feats = new ArrayList<>();
List<String> tokens = new ArrayList<>();
for(int i=start; i<end; ++i) {
if(posTags.get(i).getLabel().startsWith("N")) {
tokens.add("N");
} else if(posTags.get(i).getLabel().startsWith("CD")) {
tokens.add("CD");
} else {
tokens.add(ta.getToken(i));
}
}
for(String tkn : tokens) {
feats.add("Unigram_"+tkn);
}
for(int i=0; i<tokens.size()-1; ++i) {
feats.add("Bigram_"+tokens.get(i)+"_"+tokens.get(i+1));
}
return feats;
}
public static List<String> getUnigramBigramFeatures(
List<CoreLabel> tokens, int index, int window) {
List<String> feats = new ArrayList<>();
List<String> lemmas = new ArrayList<>();
int start = Math.max(0, index-window);
int end = Math.min(tokens.size()-1, index+window);
for(int i=start; i<=end; ++i) {
// if(tokens.get(i).tag().startsWith("N")) {
// lemmas.add("N");
// } else
if(tokens.get(i).tag().startsWith("CD")) {
lemmas.add("CD");
} else {
lemmas.add(tokens.get(i).lemma());
}
}
for(String tkn : lemmas) {
feats.add("Unigram_"+tkn);
}
for(int i=0; i<lemmas.size()-1; ++i) {
feats.add("Bigram_"+lemmas.get(i)+"_"+lemmas.get(i+1));
}
return feats;
}
public static List<String> getUnigramBigramFeatures(List<CoreLabel> tokens) {
List<String> feats = new ArrayList<>();
List<String> lemmas = new ArrayList<>();
for(int i=0; i<tokens.size(); ++i) {
if(tokens.get(i).tag().startsWith("CD")) {
lemmas.add("CD");
} else {
lemmas.add(tokens.get(i).lemma());
}
}
for(String tkn : lemmas) {
feats.add("Unigram_"+tkn);
}
for(int i=0; i<lemmas.size()-1; ++i) {
feats.add("Bigram_"+lemmas.get(i)+"_"+lemmas.get(i+1));
}
return feats;
}
public static List<String> getNeighborhoodFeatures(
TextAnnotation ta, List<Constituent> posTags, int index, int window) {
List<String> feats = new ArrayList<>();
for(int i=Math.max(0, index-window); i<Math.min(ta.size()-1, index+window); ++i) {
feats.add("Context_"+ta.getToken(i).toLowerCase()+"_"+posTags.get(i+1).getLabel());
feats.add("Context_"+posTags.get(i).getLabel()+"_"+ta.getToken(i+1).toLowerCase());
}
return feats;
}
public static List<String> getNeighborhoodFeatures(List<CoreLabel> tokens, int index, int window) {
List<String> feats = new ArrayList<>();
for(int i=Math.max(0, index-window); i<Math.min(tokens.size()-1, index+window); ++i) {
feats.add("Context_"+tokens.get(i).lemma()+"_"+tokens.get(i+1).tag());
feats.add("Context_"+tokens.get(i).tag()+"_"+tokens.get(i).lemma());
}
return feats;
}
public static List<String> getFeaturesConjWithLabels(List<String> feats, String label) {
List<String> f =new ArrayList<>();
for(String feat : feats) {
f.add(label+"_"+feat);
}
return f;
}
}