use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent in project cogcomp-nlp by CogComp.
the class TestContextFeatureExtractor method testGetFeaturesIndexWithConstituent.
@Test
public void testGetFeaturesIndexWithConstituent() throws EdisonException {
ContextFeatureExtractor fex = new ContextFeatureExtractor(2, true, false);
fex.addFeatureExtractor(new WordFeatureExtractor() {
@Override
public Set<Feature> getWordFeatures(TextAnnotation ta, int wordPosition) throws EdisonException {
String s = WordHelpers.getWord(ta, wordPosition).toLowerCase();
Set<Feature> ss = new HashSet<>();
ss.add(DiscreteFeature.create(s));
return ss;
}
});
TextAnnotation ta = TextAnnotationUtilities.createFromTokenizedString("This is a test for the feature extractor .");
Constituent c1 = new Constituent("", "", ta, 2, 3);
Set<String> c1fs = new HashSet<>();
c1fs.addAll(Arrays.asList("context-2:#word#:this", "context-1:#word#:is", "context*:#word#:a", "context1:#word#:test", "context2:#word#:for"));
Set<Feature> c1f = FeatureUtilities.getFeatures(c1fs);
Set<Feature> features = fex.getFeatures(c1);
c1f.removeAll(features);
assertEquals(0, c1f.size());
Constituent c2 = new Constituent("", "", ta, 2, 4);
Set<String> c2fs = new HashSet<>();
c2fs.addAll(Arrays.asList("context-2:#word#:this", "context-1:#word#:is", "context*:#word#:a", "context*:#word#:test", "context1:#word#:for", "context2:#word#:the"));
Set<Feature> c2f = FeatureUtilities.getFeatures(c2fs);
c2f.removeAll(fex.getFeatures(c2));
assertEquals(0, c2f.size());
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent in project cogcomp-nlp by CogComp.
the class TestChunkFeatures method testFex.
private void testFex(FeatureExtractor fex, boolean printBoth, String... viewNames) throws EdisonException {
for (TextAnnotation ta : tas) {
for (String viewName : viewNames) if (ta.hasView(viewName))
logger.info(ta.getView(viewName).toString());
if (!ta.hasView(ViewNames.SRL_VERB))
continue;
PredicateArgumentView pav = (PredicateArgumentView) ta.getView(ViewNames.SRL_VERB);
for (Constituent predicate : pav.getPredicates()) {
Constituent p = predicate.cloneForNewView("dummy");
for (Relation argument : pav.getArguments(predicate)) {
Constituent c = argument.getTarget().cloneForNewView("dummy");
Relation r = new Relation("", p, c, 1);
logger.info((printBoth ? r : c) + "\t" + fex.getFeatures(c));
}
}
}
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent in project cogcomp-nlp by CogComp.
the class TestVerbClassFeatures method test.
@Test
public final void test() throws Exception {
TextAnnotation ta = tas.get(tas.size() - 1);
PredicateArgumentView pav = (PredicateArgumentView) ta.getView(ViewNames.SRL_VERB);
for (Constituent predicate : pav.getPredicates()) {
Constituent p = predicate.cloneForNewView("dummy");
String response = p + "\t" + LevinVerbClassFeature.instance.getFeatures(p);
assertTrue(correctResponses.contains(response));
}
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent in project cogcomp-nlp by CogComp.
the class TestWordFeatureFactory method testFeatureCollection.
@Test
public final void testFeatureCollection() throws Exception {
FeatureCollection f = new FeatureCollection("features");
f.addFeatureExtractor(WordFeatureExtractorFactory.conflatedPOS);
f.addFeatureExtractor(WordFeatureExtractorFactory.gerundMarker);
f.addFeatureExtractor(WordFeatureExtractorFactory.nominalizationMarker);
logger.info("\tTesting feature collection");
Map<Integer, String> map = IOUtils.readObjectAsResource(TestWordFeatureFactory.class, "feature.collection.test");
for (TextAnnotation ta : tas) {
for (int tokenId = 0; tokenId < ta.size(); tokenId++) {
Constituent c = new Constituent("", "", ta, tokenId, tokenId + 1);
Set<Feature> features = f.getFeatures(c);
if (features.size() > 0) {
String id = ta.getTokenizedText() + ":" + tokenId;
assertEquals(map.get(id.hashCode()), features.toString());
}
}
}
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent in project cogcomp-nlp by CogComp.
the class TextStatistics method consume.
@Override
protected void consume(TextAnnotation ta) {
for (Constituent c : constituentGenerator.transform(ta)) {
try {
Set<Feature> feats = fex.getFeatures(c);
for (Feature feat : feats) {
count(feat);
}
constituentCounter.incrementAndGet();
} catch (EdisonException e) {
e.printStackTrace();
}
}
textCounter.incrementAndGet();
}
Aggregations