/** * 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; }
/** * 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; }
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 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; }
public String getStringRepresentation() if (node.getInfo()!=null) id = node.getInfo().getId(); boolean hasAntecedent = false; String antecedentId = null; if (node.getAntecedent()!=null) Info infoAntecedent = node.getAntecedent().getInfo(); if (infoAntecedent!=null) antecedentId = node.getAntecedent().getInfo().getId(); else antecedentId = "?"; String lemma = InfoGetFields.getLemma(node.getInfo()); String pos = InfoGetFields.getPartOfSpeech(node.getInfo()); String relation = InfoGetFields.getRelation(node.getInfo()); String corefGroupId = ExtendedInfoGetFields.getCorefGroupId(node.getInfo()); ExtendedInfo contentAncestor = node.getInfo().getAdditionalNodeInformation().getContentAncestor(); if (contentAncestor!=null)
/** * @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; }
private boolean filterAntecedent(S begin, S end, S common) { if ( (begin.getAntecedent()==end) || (end.getAntecedent()==begin) ) { 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 <I extends Info, N extends AbstractNode<I, N>, CN extends AbstractConstructionNode<I, CN>, A extends RuleAnnotations> N convertAnnotationTreeToRegularNodes(CN oldLhs, Map<CN, A> oldAnnotationAlignments, Map<N, A> newAnnotationAlignments, AnnotationRuleCompileServices<I, N, CN, A> compilationServices) { N newLhs = compilationServices.newNode(oldLhs.getInfo()); newAnnotationAlignments.put(newLhs, oldAnnotationAlignments.get(oldLhs)); if (oldLhs.hasChildren()) for (CN child : oldLhs.getChildren()) { N newChild = convertAnnotationTreeToRegularNodes(child, oldAnnotationAlignments, newAnnotationAlignments, compilationServices); newLhs.addChild(newChild); newChild.setAntecedent(newLhs); } return newLhs; } }
public void generate() { TreeCopier<I, S, I, S> copier = new TreeCopier<I, S, I, S>( originalTree, new TreeCopier.InfoConverter<S, I>() { @Override public I convert(S os) { return os.getInfo(); } }, nodeConstructor ); copier.copy(); generatedTree = copier.getGeneratedTree(); generatedTree.addChild(treeToPatch); BidirectionalMap<S, S> copierMap = copier.getNodesMap(); mapOriginalToGenerated = new SimpleValueSetMap<>(); for (S node : copierMap.leftSet()) { mapOriginalToGenerated.put(node, copierMap.leftGet(node)); } }
/** * 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; }
private List<S> generateAllNodeList() throws CoreferenceResolutionException { List<S> allNodesList = new LinkedList<>(); for (S tree : trees) { Map<Integer, S> mapBySerial = new LinkedHashMap<>(); for (S node : TreeIterator.iterableTree(tree)) { if (node.getAntecedent()==null) { int serial = InfoGetFields.getSerial(node.getInfo()); if (mapBySerial.containsKey(serial)) {throw new CoreferenceResolutionException("Malformed input. The parse tree has more than one node with the serial "+serial);} mapBySerial.put(serial, node); } } List<Integer> serials = new ArrayList<>(mapBySerial.size()); serials.addAll(mapBySerial.keySet()); Collections.sort(serials); for (Integer serialKey : serials) { allNodesList.add(mapBySerial.get(serialKey)); } } return allNodesList; }
if (node.hasChildren()) nextLevel.addAll(node.getChildren());
/** * Returns the "real" node in the parse-tree that is the antecedent of * the given node. * <P> * If the given node has no antecedent - then the method returns the node itself. * Otherwise, it finds the antecedent of the antecedent of the antecedent of the ... that * is the "real" node (i.e., has no antecedent). * * @param node * @return */ public static <I extends Info, S extends AbstractNode<I, S>> S getDeepAntecedent(S node) { S ret = node; while (ret.getAntecedent()!=null) { ret = ret.getAntecedent(); } return ret; }
/** * @param rhs * @param helpMap * @param newMapping * @return */ private N convertRhsToNNodes( CN rhs, BidirectionalMap<N, CN> helpMap, BidirectionalMap<N, N> newMapping) { N newRhs = compilationServices.newNode(rhs.getInfo()); if (rhs.getChildren() != null) for (CN child : rhs.getChildren()) { N newChild = convertRhsToNNodes(child, helpMap, newMapping); newRhs.addChild(newChild); newChild.setAntecedent(newRhs); } // update the new mapping if necessary if (helpMap.rightContains(rhs)) newMapping.put(helpMap.rightGet(rhs), newRhs); return newRhs; }
/** * 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; } }
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; }