/** * @param thePath an ordered list of node infos. * @param theNodeConstructor for constructing nodes. * @return a tree where the first item in the list is the only leaf, and the last item is the root. * <br>null if the path is empty. */ public static <T extends Info,S extends AbstractNode<T,S>> S buildTreeFromLeafToRoot( List<T> thePath, AbstractNodeConstructor<T, S> theNodeConstructor) { if (thePath.isEmpty()) return null; S theTree = null; List<S> theChildren = Collections.emptyList(); for (T theNextInfo: thePath) { theTree = theNodeConstructor.newNode(theNextInfo, theChildren); theChildren = new ArrayList<S>(); theChildren.add(theTree); } return theTree; } }
/** * @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; }
theNewChildrenList.add(newDependencyPath.getChildren().get(0)); allChildrenDependencyPaths.add( nodeConstructor.newNode( otherDependencyPath.getInfo(), theNewChildrenList
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));
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; }
ret = nodeConstructor.newNode(subtree.getInfo()); if (subtree.getChildren()!=null)
ST ret = nodeConstructor.newNode(retInfo); affectedNodes.add(ret);