SyntacticPathExtractor implements FeatureExtractor2<TreebankNode, TreebankNode> { public List<Feature> extract(JCas view, TreebankNode leftConstituent, TreebankNode rightConstituent) throws CleartkExtractorException { List<TreebankNode> fromStart = TreebankNodeUtil.getPathToRoot(leftConstituent); List<TreebankNode> fromEnd = TreebankNodeUtil.getPathToRoot(rightConstituent); String pathFeatureName = null; String lengthFeatureName = null; fromEnd.remove(fromEnd.size() - 1); while (fromStart.size() > 1 && fromEnd.size() > 0 && fromStart.get(fromStart.size() - 2) == fromEnd.get(fromEnd.size() - 1)) { fromStart.remove(fromStart.size() - 1); fromEnd.remove(fromEnd.size() - 1); } int length = fromStart.size(); if (!isPartial) length += fromEnd.size(); try { ListIterator<TreebankNode> it = fromStart.listIterator(); StringBuffer pathBuffer = new StringBuffer(); boolean first = true; while (it.hasNext()) { Feature feature = this.pathMemberExtractor.extract(view, it.next()).get(0); if (first) { String s = feature.getName(); if (isPartial) { pathFeatureName = Feature.createName(name, "PartialSyntacticPath(" + s + ")"); lengthFeatureName = Feature.createName(name, "PartialSyntacticPath", "Length"); } else { pathFeatureName = Feature.createName(name, "SyntacticPath(" + s + ")"); lengthFeatureName = Feature.createName(name, "SyntacticPath", "Length"); } first = false; } else { pathBuffer.append(this.UP_SEPARATOR); } pathBuffer.append(feature.getValue().toString()); } if (!isPartial) { it = fromEnd.listIterator(fromEnd.size()); while (it.hasPrevious()) { Feature feature = this.pathMemberExtractor.extract(view, it.previous()).get(0); pathBuffer.append(this.DOWN_SEPARATOR); pathBuffer.append(feature.getValue().toString()); } } List<Feature> features = new ArrayList<Feature>(2); features.add(new Feature(pathFeatureName, pathBuffer.toString())); features.add(new Feature(lengthFeatureName, (long) length)); return features; } catch (IndexOutOfBoundsException e) { return new ArrayList<Feature>(0); } } SyntacticPathExtractor(FeatureExtractor1<TreebankNode> pathMemberExtractor, boolean partial);  SyntacticPathExtractor(FeatureExtractor1<TreebankNode> pathMemberExtractor); List<Feature> extract(JCas view, TreebankNode leftConstituent, TreebankNode rightConstituent);  }
@Test public void test() throws Throwable { tokenBuilder.buildTokens(jCas, "I ran home", "I ran home", "PRP VBD NN"); TreebankNode iNode = TreebankNodeUtil.newNode(jCas, 0, 1, "PRP"); TreebankNode inpNode = TreebankNodeUtil.newNode(jCas, "NP", iNode); TreebankNode ranNode = TreebankNodeUtil.newNode(jCas, 2, 5, "VBD"); TreebankNode homeNode = TreebankNodeUtil.newNode(jCas, 6, 10, "NN"); TreebankNode homenpNode = TreebankNodeUtil.newNode(jCas, "NP", homeNode); TreebankNode ranvpNode = TreebankNodeUtil.newNode(jCas, "VP", ranNode, homenpNode); TreebankNode topNode = TreebankNodeUtil.newNode(jCas, "S", inpNode, ranvpNode); CoveredTextExtractor<TreebankNode> textExtractor = new CoveredTextExtractor<TreebankNode>(); TypePathExtractor<TreebankNode> tagExtractor = new TypePathExtractor<TreebankNode>(TreebankNode.class, "nodeType"); SyntacticPathExtractor extractor; List<Feature> features; extractor = new SyntacticPathExtractor(tagExtractor); features = extractor.extract(jCas, iNode, ranNode); Assert.assertEquals(2, features.size()); Assert.assertEquals("SyntacticPath(TypePath(NodeType))", features.get(0).getName()); Assert.assertEquals("PRP::NP::S;;VP;;VBD", features.get(0).getValue()); Assert.assertEquals("SyntacticPath_Length", features.get(1).getName()); Assert.assertEquals(5, ((Long) features.get(1).getValue()).intValue()); extractor = new SyntacticPathExtractor(tagExtractor); features = extractor.extract(jCas, topNode, homenpNode); Assert.assertEquals(2, features.size()); Assert.assertEquals("SyntacticPath(TypePath(NodeType))", features.get(0).getName()); Assert.assertEquals("S;;VP;;NP", features.get(0).getValue()); Assert.assertEquals("SyntacticPath_Length", features.get(1).getName()); Assert.assertEquals(3, ((Long) features.get(1).getValue()).intValue()); extractor = new SyntacticPathExtractor(textExtractor, true); features = extractor.extract(jCas, homeNode, ranNode); Assert.assertEquals(2, features.size()); Assert.assertEquals("PartialSyntacticPath(null)", features.get(0).getName()); Assert.assertEquals("home::home::ran home", features.get(0).getValue()); Assert.assertEquals("PartialSyntacticPath_Length", features.get(1).getName()); Assert.assertEquals(3, ((Long) features.get(1).getValue()).intValue()); }