@Test public void testGetFeaturesNoIndexWithConstituent() throws EdisonException { ContextFeatureExtractor fex = new ContextFeatureExtractor(2, false, false); fex.addFeatureExtractor(new WordFeatureExtractor() { @Override public Set<Feature> getWordFeatures(TextAnnotation ta, int wordPosition) throws EdisonException { String s = WordHelpers.getWord(ta, wordPosition).toLowerCase(); Set<String> ss = new HashSet<>(); ss.add(s); return FeatureUtilities.getFeatures(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:#word#:this", "context:#word#:is", "context:#word#:a", "context:#word#:test", "context:#word#:for")); Set<Feature> c1f = FeatureUtilities.getFeatures(c1fs); c1f.removeAll(fex.getFeatures(c1)); assertEquals(0, c1f.size()); Constituent c2 = new Constituent("", "", ta, 2, 4); Set<String> c2fs = new HashSet<>(); c2fs.addAll(Arrays.asList("context:#word#:this", "context:#word#:is", "context:#word#:a", "context:#word#:test", "context:#word#:for", "context:#word#:the")); Set<Feature> c2f = FeatureUtilities.getFeatures(c2fs); c2f.removeAll(fex.getFeatures(c2)); assertEquals(0, c2f.size()); }
@Override public Set<Feature> getFeatures(Constituent c) throws EdisonException { TextAnnotation ta = c.getTextAnnotation(); int start = c.getStartSpan() - contextSize; int end = c.getEndSpan() + contextSize; if (start < 0) start = 0; if (end >= ta.size()) end = ta.size(); Set<Feature> features = new LinkedHashSet<>(); for (int i = start; i < end; i++) { if (ignoreConstituent) if (c.getStartSpan() <= i && i < c.getEndSpan()) continue; for (FeatureExtractor f : this.generators) { Constituent neighbor = new Constituent("TMP", "TMP", ta, i, i + 1); Set<Feature> feats = f.getFeatures(neighbor); for (Feature feat : feats) { String preamble = "context"; if (specifyIndex) { String index = "*"; if (i < c.getStartSpan()) index = (i - c.getStartSpan()) + ""; else if (i >= c.getEndSpan()) index = (i - c.getEndSpan() + 1) + ""; preamble += index; } preamble += ":"; features.add(feat.prefixWith(preamble + f.getName())); } } } return features; }
ContextFeatureExtractor extends FeatureCollection { @Override public Set<Feature> getFeatures(Constituent c) throws EdisonException { TextAnnotation ta = c.getTextAnnotation(); int start = c.getStartSpan() - contextSize; int end = c.getEndSpan() + contextSize; if (start < 0) start = 0; if (end >= ta.size()) end = ta.size(); Set<Feature> features = new LinkedHashSet<>(); for (int i = start; i < end; i++) { if (ignoreConstituent) if (c.getStartSpan() <= i && i < c.getEndSpan()) continue; for (FeatureExtractor f : this.generators) { Constituent neighbor = new Constituent("TMP", "TMP", ta, i, i + 1); Set<Feature> feats = f.getFeatures(neighbor); for (Feature feat : feats) { String preamble = "context"; if (specifyIndex) { String index = "*"; if (i < c.getStartSpan()) index = (i - c.getStartSpan()) + ""; else if (i >= c.getEndSpan()) index = (i - c.getEndSpan() + 1) + ""; preamble += index; } preamble += ":"; features.add(feat.prefixWith(preamble + f.getName())); } } } return features; } }
ContextFeatureExtractor extends FeatureCollection { @Override public Set<Feature> getFeatures(Constituent c) throws EdisonException { TextAnnotation ta = c.getTextAnnotation(); int start = c.getStartSpan() - contextSize; int end = c.getEndSpan() + contextSize; if (start < 0) start = 0; if (end >= ta.size()) end = ta.size(); Set<Feature> features = new LinkedHashSet<>(); for (int i = start; i < end; i++) { if (ignoreConstituent) if (c.getStartSpan() <= i && i < c.getEndSpan()) continue; for (FeatureExtractor f : this.generators) { Constituent neighbor = new Constituent("TMP", "TMP", ta, i, i + 1); Set<Feature> feats = f.getFeatures(neighbor); for (Feature feat : feats) { String preamble = "context"; if (specifyIndex) { String index = "*"; if (i < c.getStartSpan()) index = (i - c.getStartSpan()) + ""; else if (i >= c.getEndSpan()) index = (i - c.getEndSpan() + 1) + ""; preamble += index; } preamble += ":"; features.add(feat.prefixWith(preamble + f.getName())); } } } return features; } ContextFeatureExtractor(int contextSize, boolean specifyIndex, boolean ignoreConstituent);  ContextFeatureExtractor(int contextSize, boolean specifyIndex,
            boolean ignoreConstituent, WordFeatureExtractor... fex); }
ContextFeatureExtractor extends FeatureCollection { @Override public Set<Feature> getFeatures(Constituent c) throws EdisonException { TextAnnotation ta = c.getTextAnnotation(); int start = c.getStartSpan() - contextSize; int end = c.getEndSpan() + contextSize; if (start < 0) start = 0; if (end >= ta.size()) end = ta.size(); Set<Feature> features = new LinkedHashSet<>(); for (int i = start; i < end; i++) { if (ignoreConstituent) if (c.getStartSpan() <= i && i < c.getEndSpan()) continue; for (FeatureExtractor f : this.generators) { Constituent neighbor = new Constituent("TMP", "TMP", ta, i, i + 1); Set<Feature> feats = f.getFeatures(neighbor); for (Feature feat : feats) { String preamble = "context"; if (specifyIndex) { String index = "*"; if (i < c.getStartSpan()) index = (i - c.getStartSpan()) + ""; else if (i >= c.getEndSpan()) index = (i - c.getEndSpan() + 1) + ""; preamble += index; } preamble += ":"; features.add(feat.prefixWith(preamble + f.getName())); } } } return features; } ContextFeatureExtractor(int contextSize, boolean specifyIndex, boolean ignoreConstituent);  ContextFeatureExtractor(int contextSize, boolean specifyIndex,
            boolean ignoreConstituent, WordFeatureExtractor... fex); @Override Set<Feature> getFeatures(Constituent c); @Override String getName(); }
ContextFeatureExtractor extends FeatureCollection { @Override public Set<Feature> getFeatures(Constituent c) throws EdisonException { TextAnnotation ta = c.getTextAnnotation(); int start = c.getStartSpan() - contextSize; int end = c.getEndSpan() + contextSize; if (start < 0) start = 0; if (end >= ta.size()) end = ta.size(); Set<Feature> features = new LinkedHashSet<>(); for (int i = start; i < end; i++) { if (ignoreConstituent) if (c.getStartSpan() <= i && i < c.getEndSpan()) continue; for (FeatureExtractor f : this.generators) { Constituent neighbor = new Constituent("TMP", "TMP", ta, i, i + 1); Set<Feature> feats = f.getFeatures(neighbor); for (Feature feat : feats) { String preamble = "context"; if (specifyIndex) { String index = "*"; if (i < c.getStartSpan()) index = (i - c.getStartSpan()) + ""; else if (i >= c.getEndSpan()) index = (i - c.getEndSpan() + 1) + ""; preamble += index; } preamble += ":"; features.add(feat.prefixWith(preamble + f.getName())); } } } return features; } ContextFeatureExtractor(int contextSize, boolean specifyIndex, boolean ignoreConstituent);  ContextFeatureExtractor(int contextSize, boolean specifyIndex,
            boolean ignoreConstituent, WordFeatureExtractor... fex); @Override Set<Feature> getFeatures(Constituent c); @Override String getName();  }