/** * return true iff the node is negated * @param node * @return */ public static <T extends Info, S extends AbstractNode<T,S>> boolean isNegated(S node) { if (node.getChildren() != null) for( S child : node.getChildren()) if (NEG_WORDS.contains(child.getInfo().getNodeInfo().getWordLemma())) return true; return false; }
/** * @param parseTree any type of parse-tree. * @param prefix string to print before the current node (for indentation). * @param infoToStringConverter used for printing the info of each node. * @return a multi-line indented presentation of the given parse tree. Subroutine of {@link #toIndentedString(AbstractNode)} */ protected static <T extends Info,S extends AbstractNode<T,S>> String toIndentedString(S parseTree, String prefix, Transformer<T, String> infoToStringConverter) { if (parseTree==null) return "null"; StringBuffer sb = new StringBuffer() .append(prefix) .append(infoToStringConverter.transform(parseTree.getInfo())) .append("\r\n"); if (parseTree.getChildren()!=null) for (S child: parseTree.getChildren()) sb.append(toIndentedString(child, prefix+" ", infoToStringConverter)); return sb.toString(); }
/** * @param root the root of a parse-tree. * @return a list of all dependency paths, in string form, according to UNARY_BINC definition. */ @Override public Set<String> stringDependencyPaths(S root) { Set<String> result = new HashSet<String>(); // Right-to-left paths are dependency paths where the unknown is the left leaf (usually a noun). // The root of the tree (usually a verb) is at the right. // Optionally, to the right of the root, there is another leaf, with full lemma. List<S> paths = DPFT.dependencyPaths(root); for (S dependencyPath: paths) { if (dependencyPath.getChildren().size()==2) { // If there are two branches - add them twice in both directions: result.add(AbstractNodeDependencyPathsUtils.toDependencyPath(dependencyPath, 2, Direction.RIGHT_TO_LEFT, /*writeRootLemma=*/true, /*writeLeftLeafLemma=*/writeLeftLemma, /*writeRightLeafLemma=*/writeRightLemma)); // create a new children list in reverse order: List<S> theNewChildrenList = new ArrayList<S>(2); theNewChildrenList.add(dependencyPath.getChildren().get(1)); theNewChildrenList.add(dependencyPath.getChildren().get(0)); result.add(AbstractNodeDependencyPathsUtils.toDependencyPath(nodeConstructor.newNode(dependencyPath.getInfo(), theNewChildrenList), 2, Direction.RIGHT_TO_LEFT, /*writeRootLemma=*/true, /*writeLeftLeafLemma=*/writeLeftLemma, /*writeRightLeafLemma=*/writeRightLemma)); } } return result; }
/** * @param node * @param mapNodeToId * @param nodeToConllStringConvertor * @return * @throws ConllConverterException */ private static <I extends Info, N extends AbstractNode<I, N>> StringBuilder nodeToConll(N node, Map<N, Integer> mapNodeToId, Map<N, N> nodeToParentMap, TreeConllStringConverter<I, N> nodeToConllStringConvertor) throws ConllConverterException { StringBuilder conllStr = new StringBuilder(); conllStr.append(nodeToConllStringConvertor.convert(node, mapNodeToId, nodeToParentMap.get(node))).append('\n'); if (node.hasChildren()) for (N child : node.getChildren()) conllStr.append(nodeToConll(child, mapNodeToId, nodeToParentMap, nodeToConllStringConvertor)); return conllStr; }
/** * Add all words and lemmas of the given tree to the given set. * @return the same set. */ protected static <T extends Info, S extends AbstractNode<T,S>> HashSet<String> addWordsAndLemmasToSet(AbstractNode<T,S> parseTree, HashSet<String> theSet, boolean withWords, boolean withLemmas) { if (withWords) theSet.add(InfoGetFields.getWord(parseTree.getInfo())); if (withLemmas) theSet.add(InfoGetFields.getLemma(parseTree.getInfo())); if (parseTree.getChildren()!=null) for (AbstractNode<T,S> child: parseTree.getChildren()) addWordsAndLemmasToSet(child, theSet, withWords, withLemmas); return theSet; } }
@Override public Bag<String> transform(AbstractNode<? extends Info,? extends AbstractNode> parseTree) { Bag<String> words = new HashBag<String> (); if (parseTree.getChildren()==null) { words.add(InfoGetFields.getWord(parseTree.getInfo())); } else { for (AbstractNode child: parseTree.getChildren()) { words.addAll(mapNodeToBagOfWords.get(child)); } } return words; } }
result.add(AbstractNodeDependencyPathsUtils.toDependencyPath(dependencyPath, 2, Direction.RIGHT_TO_LEFT, /*writeRootLemma=*/true, /*writeLeftLeafLemma=*/writeLeftLemma, /*writeRightLeafLemma=*/writeRightLemma)); if (dependencyPath.getChildren().size()==2) { // If there are two branches - add them again in reverse order: theNewChildrenList.add(dependencyPath.getChildren().get(1)); theNewChildrenList.add(dependencyPath.getChildren().get(0)); result.add(AbstractNodeDependencyPathsUtils.toDependencyPath(nodeConstructor.newNode(dependencyPath.getInfo(), theNewChildrenList), 2, Direction.RIGHT_TO_LEFT, /*writeRootLemma=*/true, /*writeLeftLeafLemma=*/writeLeftLemma, /*writeRightLeafLemma=*/writeRightLemma));
private boolean hasCopula(S node) { if (node.hasChildren()) { for (S child : node.getChildren()) { String relation = InfoGetFields.getRelation(child.getInfo()); if (COPULA_RELATION.equals(relation)) { return true; } } } return false; }
/** * @param root the root of a parse-tree. * @return a list of all dependency paths, in string form, according to [Snow 2005] definition. */ @Override public Set<String> stringDependencyPaths(S root) { Set<String> result = new HashSet<String>(); for (S dependencyPath: oneBranchDPFT.dependencyPaths(root)) result.add(AbstractNodeDependencyPathsUtils.toDependencyPath(dependencyPath, 1, Direction.RIGHT_TO_LEFT, /*writeRootLemma=*/true, /*writeLeftLeafLemma=*/true, /*writeRightLeafLemma=*/true)); if (USE_TWO_BRANCH_DPFT) for (S dependencyPath: twoBranchDPFT.dependencyPaths(root)) if (dependencyPath.getChildren().size()==2) // limit to exactly 2 noun-leafs result.add(AbstractNodeDependencyPathsUtils.toDependencyPath(dependencyPath, 2, Direction.RIGHT_TO_LEFT, /*writeRootLemma=*/true, /*writeLeftLeafLemma=*/true, /*writeRightLeafLemma=*/true)); return result; } }
private static <T extends Info,S extends AbstractNode<T,S>> List<String> getEntailedModifiersOfNonNegatedNode(S node) throws ParseTreeException { if (node.getChildren() != null) return getEntailedModifiersOfNonNegatedParent(node); // recurse // else { // this is a leaf, return only the leaf's word List<String> selectOffspring = new Vector<String>(); String parentWord = node.getInfo().getNodeInfo().getWord(); selectOffspring.add(parentWord); return selectOffspring; } }
/** * @param node * @param mapNodeToId * @param id * @param nodeToConllStringConvertor * @param variableId * @return * @throws ConllConverterException */ private static <I extends Info, N extends AbstractNode<I, N>> StringBuilder nodeToConll(N node, BidirectionalMap<N, N> alignments, Map<N, Integer> mapNodeToId, RuleConllStringConverter<I, N> nodeToConllStringConvertor) throws ConllConverterException { StringBuilder conllStr = new StringBuilder(); int variableId = -1; // -1 means no alignment if (alignments.leftContains(node)) variableId = mapNodeToId.get(alignments.leftGet(node)); else if (alignments.rightContains(node)) variableId = mapNodeToId.get(alignments.rightGet(node)); conllStr.append(nodeToConllStringConvertor.convert(node, mapNodeToId, variableId)).append('\n'); if (node.hasChildren()) for (N child : node.getChildren()) conllStr.append(nodeToConll(child, alignments , mapNodeToId, nodeToConllStringConvertor)); return conllStr; }
private Set<S> getPredicateInternalNodes(S node) { Set<S> ret = new LinkedHashSet<S>(); ret.add(node); if (node.hasChildren()) { for (S child : node.getChildren()) { String relation = InfoGetFields.getRelation(child.getInfo()); if (RelationTypes.getSemanticInternalFacetRelations().contains(relation)) { ret.addAll(getPredicateInternalNodes(child)); } } } return ret; }
protected ST copyNonRuleSubTree(ST subtree) { ST ret = nodeConstructor.newNode(subtree.getInfo()); if (subtree.getChildren()!=null) { for (ST child : subtree.getChildren()) { if (!mapRuleLhsToTree.rightContains(child)) { ret.addChild(copyNonRuleSubTree(child)); } } } mapOrigToGenerated.put(subtree, ret); return ret; }
private Set<S> getPredicateInternalNodes(S node, Set<S> excludedDirectChildren) { Set<S> ret = new LinkedHashSet<S>(); ret.add(node); if (node.hasChildren()) { for (S child : node.getChildren()) { if (!excludedDirectChildren.contains(child)) { String relation = InfoGetFields.getRelation(child.getInfo()); if (RelationTypes.getSemanticInternalFacetRelations().contains(relation)) { ret.addAll(getPredicateInternalNodes(child)); } } } } return ret; }
public static <I extends Info, S extends AbstractNode<I, S>> Set<S> getInternalNodes(S node) { Set<S> ret = new LinkedHashSet<S>(); ret.add(node); if (node.hasChildren()) { for (S child : node.getChildren()) { String relation = InfoGetFields.getRelation(child.getInfo()); if (RelationTypes.getSemanticInternalFacetRelations().contains(relation)) { ret.addAll(getInternalNodes(child)); } } } return ret; }
public ExtendedNode newNode(ExtendedInfo info, List<? extends AbstractNode<ExtendedInfo, ?>> children) { ExtendedNode ret = new ExtendedNode(info); if (children != null) { for (AbstractNode<ExtendedInfo, ?> node : children) { ret.addChild(newNode(node.getInfo(),node.getChildren())); } } return ret; }
/** * skip the artificial "ROOT" node * @param tree * @return * @throws AnnotatorException */ public static <N extends AbstractNode<? extends Info, N>> N skipRoot(N tree) throws AnnotatorException { if (tree == null) throw new AnnotatorException("got null tree"); N nodeToAnnotate; if (tree.getInfo().getId().equals(AbstractMiniparParser.ROOT_NODE_ID)) // skip the artificial "ROOT" node if (tree.hasChildren()) nodeToAnnotate = tree.getChildren().get(0); else throw new AnnotatorException("got a sentence that is an empty root: " + tree); else nodeToAnnotate = tree; return nodeToAnnotate; }
/** * Adds a list of all dependency paths from the given node or a descendant of it, to a descendant node. * @param root [INPUT] the common ancestor of all the dependency paths that will be created. * @param currentDependencyPaths [OUTPUT] the dependency paths. */ protected void addDependencyPaths(S node, List<S> currentDependencyPaths) { // 1. Add all dependency paths from the given root: if (startNodePredicate.evaluate(node.getInfo())) currentDependencyPaths.addAll(dependencyPathsFromNode(node)); // 2. Recursively add all dependency paths from each child of root: if (!AbstractNodeUtils.isLeaf(node)) for (S child: node.getChildren()) addDependencyPaths(child, currentDependencyPaths); }
/** * Given a node in the parse-tree, this method finds a direct-child of that node * that is connected to it via "ref" relation, and also has an antecedent. * * @param node * @return */ @StandardSpecific("stanford-dependencies") @ParserSpecific("easyfirst") public static <I extends Info, S extends AbstractNode<I, S>> S getRefWithAntecedent(S node) { S refWithAntecedent = null; if (node.hasChildren()) { for (S child : node.getChildren()) { if ("ref".equals(InfoGetFields.getRelation(child.getInfo()))) { if (child.getAntecedent()!=null) { refWithAntecedent = child; break; } } } } return refWithAntecedent; }
/** * make sure each node under the root has a relation and a POS * @param root * @throws CompilationException */ private static <I extends Info, N extends AbstractNode<I,N>> void sanityCheckChildren(N root) throws CompilationException { if (root.hasChildren()) for (N child : root.getChildren()) { // make sure each node under the root has a relation if (ExtendedInfoGetFields.getRelation(child .getInfo(), null) == null) throw new CompilationException("This node has no relation: " + child ); checkPos(child ); sanityCheckChildren(child); } }