HeadWordExtractor implements FeatureExtractor1<TreebankNode> { public List<Feature> extract(JCas jCas, TreebankNode constituent) throws CleartkExtractorException { TreebankNode headNode = findHead(constituent); List<Feature> features = new ArrayList<Feature>(extractNode(jCas, headNode, false)); if (includePPHead && constituent.getNodeType().equals("PP")) { for (int i = 0; i < constituent.getChildren().size(); i++) { TreebankNode child = constituent.getChildren(i); if (child.getNodeType().equals("NP")) { features = new ArrayList<Feature>(features); features.addAll(extractNode(jCas, findHead(child), true)); break; } } } return features; } HeadWordExtractor(FeatureExtractor1<TreebankNode> subExtractor, boolean includePPHead);  HeadWordExtractor(FeatureExtractor1<TreebankNode> subExtractor); List<Feature> extract(JCas jCas, TreebankNode constituent);  }
@Test public void testNoTokens() throws Throwable { HeadWordExtractor extractor = new HeadWordExtractor(new CoveredTextExtractor<TreebankNode>(), true); jCas.setDocumentText("foo"); TreebankNode node = TreebankNodeUtil.newNode(jCas, 0, 3, "NN"); this.checkFeatures(extractor.extract(jCas, node), "HeadWord", "foo"); }
@Test public void testNoNodeTypes() { try { HeadWordExtractor extractor = new HeadWordExtractor(null); jCas.setDocumentText("foo"); TreebankNode parent = TreebankNodeUtil.newNode( jCas, null, TreebankNodeUtil.newNode(jCas, 0, 3, null)); this.checkFeatures(extractor.extract(jCas, parent)); } catch (NullPointerException e) { return; } catch (CleartkExtractorException e) { return; } Assert.fail("should throw an exception when configuring a null extractor"); }
@Test public void testSimpleSentence() throws Throwable { tokenBuilder.buildTokens(jCas, "I ran home", "I ran home", "PRP VBD NN"); TreebankNode iNode = TreebankNodeUtil.newNode(jCas, 0, 1, "PRP"); TreebankNode ranNode = TreebankNodeUtil.newNode(jCas, 2, 5, "VBD"); TreebankNode homeNode = TreebankNodeUtil.newNode(jCas, 6, 10, "NN"); TreebankNode vpNode = TreebankNodeUtil.newNode(jCas, "VP", ranNode, homeNode); CoveredTextExtractor<TreebankNode> textExtractor = new CoveredTextExtractor<TreebankNode>(); TypePathExtractor<TreebankNode> posExtractor = new TypePathExtractor<TreebankNode>(TreebankNode.class, "nodeType"); HeadWordExtractor extractor = new HeadWordExtractor(textExtractor); this.checkFeatures(extractor.extract(jCas, iNode), "HeadWord", "I"); this.checkFeatures(extractor.extract(jCas, vpNode), "HeadWord", "ran"); this.checkFeatures( new HeadWordExtractor(textExtractor, false).extract(jCas, vpNode), "HeadWord", "ran"); this.checkFeatures( new HeadWordExtractor(posExtractor).extract(jCas, vpNode), "HeadWord_TypePath(NodeType)", "VBD"); }
@Test public void testNPandPP() throws Throwable { tokenBuilder.buildTokens( jCas, "cat's toy under the box", "cat 's toy under the box", "NN POS NN IN DT NN"); TreebankNode catNode = TreebankNodeUtil.newNode(jCas, 0, 3, "NN"); TreebankNode sNode = TreebankNodeUtil.newNode(jCas, 3, 5, "POS"); TreebankNode catsNode = TreebankNodeUtil.newNode(jCas, "NP", catNode, sNode); TreebankNode toyNode = TreebankNodeUtil.newNode(jCas, 6, 9, "NN"); TreebankNode catstoyNode = TreebankNodeUtil.newNode(jCas, "NP", catsNode, toyNode); TreebankNode underNode = TreebankNodeUtil.newNode(jCas, 10, 15, "IN"); TreebankNode theNode = TreebankNodeUtil.newNode(jCas, 16, 19, "DT"); TreebankNode boxNode = TreebankNodeUtil.newNode(jCas, 20, 23, "NN"); TreebankNode theboxNode = TreebankNodeUtil.newNode(jCas, "NP", theNode, boxNode); TreebankNode undertheboxNode = TreebankNodeUtil.newNode(jCas, "PP", underNode, theboxNode); TreebankNode tree = TreebankNodeUtil.newNode(jCas, "NP", catstoyNode, undertheboxNode); CoveredTextExtractor<TreebankNode> textExtractor = new CoveredTextExtractor<TreebankNode>(); TypePathExtractor<TreebankNode> posExtractor = new TypePathExtractor<TreebankNode>(TreebankNode.class, "nodeType"); HeadWordExtractor extractor; extractor = new HeadWordExtractor(posExtractor, true); this.checkFeatures(extractor.extract(jCas, tree), "HeadWord_TypePath(NodeType)", "NN"); this.checkFeatures( extractor.extract(jCas, undertheboxNode), "HeadWord_TypePath(NodeType)", "IN", "PPHeadWord_TypePath(NodeType)", "NN"); extractor = new HeadWordExtractor(textExtractor, true); this.checkFeatures(extractor.extract(jCas, tree), "HeadWord", "toy"); List<Feature> features = extractor.extract(jCas, undertheboxNode); Assert.assertEquals(2, features.size()); Assert.assertEquals("HeadWord", features.get(0).getName()); Assert.assertEquals("under", features.get(0).getValue()); Assert.assertEquals("PPHeadWord", features.get(1).getName()); Assert.assertEquals("box", features.get(1).getValue()); }