/** * Format the TreebankNode as a Penn-Treebank-style parenthesized string. * * @param node * The TreebankNode to be formatted. * @return A parenthesized Penn-Treebank-style string. */ public static String toTreebankString(TreebankNode node) { StringBuilder builder = new StringBuilder(); builder.append('(').append(node.getNodeType()); if (node.getLeaf()) { builder.append(' ').append(node.getCoveredText()); } else { for (TreebankNode child : JCasUtil.select(node.getChildren(), TreebankNode.class)) { builder.append(' ').append(toTreebankString(child)); } } builder.append(')'); return builder.toString(); }
private static String print(TreebankNode node, int tabs) { StringBuffer returnValue = new StringBuffer(); String tabString = getTabs(tabs); returnValue.append(tabString + node.getNodeType()); if (node.getNodeValue() != null) returnValue.append(":" + node.getNodeValue() + "\n"); else { returnValue.append(":" + node.getCoveredText() + "\n"); } if (node.getChildren().size() > 0) { Collection<TreebankNode> children = JCasUtil.select(node.getChildren(), TreebankNode.class); for (TreebankNode child : children) { returnValue.append(print(child, (tabs + 1))); } } return returnValue.toString(); }
private void collectHeads(TreebankNode node, List<TreebankNode> heads) { if (node.getLeaf()) { heads.add(node); } String[] headTypes = VerbClauseTemporalAnnotator.headMap.get(node.getNodeType()); if (headTypes != null) { for (String headType : headTypes) { boolean foundChildWithHeadType = false; for (int i = 0; i < node.getChildren().size(); i++) { TreebankNode child = node.getChildren(i); if (child.getNodeType().equals(headType)) { String text = child.getCoveredText(); if (!VerbClauseTemporalAnnotator.stopWords.contains(text)) { this.collectHeads(child, heads); foundChildWithHeadType = true; } } } if (foundChildWithHeadType) { break; } } } }
@Override public List<Feature> extract(JCas view, T focusAnnotation) { List<Feature> features = new ArrayList<Feature>(); TreebankNode node = TreebankNodeUtil.selectMatchingLeaf(view, focusAnnotation); if (node != null) { node = node.getParent(); } if (node != null) { node = node.getParent(); } if (node != null) { node = node.getChildren(0); } if (node != null && node.getLeaf()) { features.add(new Feature(this.featureName, node.getCoveredText())); } return features; } }
@Override public List<Feature> extract(JCas view, Token token) throws CleartkExtractorException { TreebankNode node = TreebankNodeUtil.selectMatchingLeaf(view, token); List<Feature> features = new ArrayList<Feature>(); if (node != null) { TreebankNode parent = node.getParent(); if (parent != null) { features.add(new Feature("ParentNodeType", parent.getNodeType())); TreebankNode firstSibling = parent.getChildren(0); if (firstSibling != node && firstSibling.getLeaf()) { features.add(new Feature("FirstSiblingText", firstSibling.getCoveredText())); } } } return features; } }