/** * Returns the yield of a node, i.e., all descendents of the node. * * @param word The word acting as the root of the constituent we are finding. */ public List<IndexedWord> yield(IndexedWord word) { List<IndexedWord> yield = new LinkedList<>(); Stack<IndexedWord> fringe = new Stack<>(); fringe.push(word); while ( ! fringe.isEmpty()) { IndexedWord parent = fringe.pop(); yield.add(parent); for (SemanticGraphEdge edge : outgoingEdgeIterable(parent)) { if (!edge.isExtra()) { fringe.push(edge.getDependent()); } } } Collections.sort(yield); return yield; }
/** * Returns the span of the subtree yield of this node. That is, the span of all the nodes under it. * In the case of projective graphs, the words in this span are also the yield of the constituent rooted * at this node. * * @param word The word acting as the root of the constituent we are finding. * @return A span, represented as a pair of integers. The span is zero indexed. The begin is inclusive and the end is exclusive. */ public Pair<Integer, Integer> yieldSpan(IndexedWord word) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; Stack<IndexedWord> fringe = new Stack<>(); fringe.push(word); while (!fringe.isEmpty()) { IndexedWord parent = fringe.pop(); min = Math.min(min, parent.index() - 1); max = Math.max(max, parent.index()); for (SemanticGraphEdge edge : outgoingEdgeIterable(parent)) { if (!edge.isExtra()) { fringe.push(edge.getDependent()); } } } return Pair.makePair(min, max); }
public SemanticGraphEdge(SemanticGraphEdge e) { this(e.getSource(), e.getTarget(), e.getRelation(), e.getWeight(), e.isExtra()); }
IndexedWord newGov = oldToNewVertices.get(edge.getGovernor()); IndexedWord newDep = oldToNewVertices.get(edge.getDependent()); nsg.addEdge(newGov, newDep, edge.getRelation(), edge.getWeight(), edge.isExtra());
public SemanticGraph makeSoftCopy() { SemanticGraph newSg = new SemanticGraph(); if ( ! this.roots.isEmpty()) newSg.setRoot(this.getFirstRoot()); for (SemanticGraphEdge edge : this.edgeIterable()) { newSg.addEdge(edge.getSource(), edge.getTarget(), edge.getRelation(), edge.getWeight(), edge.isExtra()); } return newSg; }
SemanticGraphEdge edge = tree.incomingEdgeIterable(vertex).iterator().next(); fragmentTree.addEdge(fragmentTree.getFirstRoot(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra()); });
/** * Copies a the current graph, but also sets the mapping from the old to new * graph. */ public SemanticGraph(SemanticGraph g, Map<IndexedWord, IndexedWord> prevToNewMap) { graph = new DirectedMultiGraph<>(outerMapFactory, innerMapFactory); if (prevToNewMap == null) { prevToNewMap = wordMapFactory.newMap(); } Set<IndexedWord> vertexes = g.vertexSet(); for (IndexedWord vertex : vertexes) { IndexedWord newVertex = new IndexedWord(vertex); newVertex.setCopyCount(vertex.copyCount()); addVertex(newVertex); prevToNewMap.put(vertex, newVertex); } roots = wordMapFactory.newSet(); for (IndexedWord oldRoot : g.getRoots()) { roots.add(prevToNewMap.get(oldRoot)); } for (SemanticGraphEdge edge : g.edgeIterable()) { IndexedWord newGov = prevToNewMap.get(edge.getGovernor()); IndexedWord newDep = prevToNewMap.get(edge.getDependent()); addEdge(newGov, newDep, edge.getRelation(), edge.getWeight(), edge.isExtra()); } }
/** Returns a list of TypedDependency in the graph. * This method goes through all SemanticGraphEdge and converts them * to TypedDependency. * * @return A List of TypedDependency in the graph */ public Collection<TypedDependency> typedDependencies() { Collection<TypedDependency> dependencies = new ArrayList<>(); IndexedWord root = null; for (IndexedWord node : roots) { if (root == null) { root = new IndexedWord(node.docID(), node.sentIndex(), 0); root.setValue("ROOT"); } TypedDependency dependency = new TypedDependency(ROOT, root, node); dependencies.add(dependency); } for (SemanticGraphEdge e : this.edgeIterable()){ TypedDependency dependency = new TypedDependency(e.getRelation(), e.getGovernor(), e.getDependent()); if (e.isExtra()) { dependency.setExtra(); } dependencies.add(dependency); } return dependencies; }
private static void demoteQmodParentHelper(SemanticGraph sg, IndexedWord gov, IndexedWord oldHead) { if (!sg.getRoots().contains(oldHead)) { IndexedWord parent = sg.getParent(oldHead); if (parent == null) { return; } SemanticGraphEdge edge = sg.getEdge(parent, oldHead); sg.addEdge(parent, gov, edge.getRelation(), edge.getWeight(), edge.isExtra()); sg.removeEdge(edge); } else { sg.getRoots().remove(oldHead); sg.addRoot(gov); } //temporary relation to keep the graph connected sg.addEdge(gov, oldHead, DEPENDENT, Double.NEGATIVE_INFINITY, false); sg.removeEdge(sg.getEdge(oldHead, gov)); }
public SemanticGraphEdge addEdge(SemanticGraphEdge edge) { SemanticGraphEdge newEdge = new SemanticGraphEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra()); graph.add(edge.getGovernor(), edge.getDependent(), newEdge); return newEdge; }
public static void copyEmptyNodes(SemanticGraph source, SemanticGraph target) { for (IndexedWord node : source.vertexSet()) { if (isEmptyNode(node)) { target.addVertex(node); System.err.println("added vertex" + node); } } //remove all orphan dependencies for (SemanticGraphEdge edge: target.edgeListSorted()) { if (edge.getRelation().getShortName().equals("orphan")) { target.removeEdge(edge); System.err.println("removed edge" + edge); } } for (SemanticGraphEdge edge : source.edgeIterable()) { if (edge.getRelation().getShortName().equals("orphan") || isEmptyNode(edge.getDependent()) || isEmptyNode(edge.getGovernor())) { target.addEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra()); System.err.println("added edge" + edge); } } }
sg.addEdge(edge.getGovernor(), newNode, edge.getRelation(), edge.getWeight(), edge.isExtra());
private static void fixCCAttachment(SemanticGraph sg) { HashMap<SemanticGraphEdge, Integer> newHeads = new HashMap<>(); for (SemanticGraphEdge edge : sg.edgeIterable()) { if (edge.getRelation().equals(UniversalEnglishGrammaticalRelations.COORDINATION)) { // check if "cc" is attached to previous conjunct if (edge.getGovernor().index() < edge.getDependent().index()) { Set<IndexedWord> conjuncts = sg.getChildrenWithReln(edge.getGovernor(), UniversalEnglishGrammaticalRelations.CONJUNCT); int newHead = Integer.MAX_VALUE; for (IndexedWord conjunct : conjuncts) { if (conjunct.index() < newHead && conjunct.index() > edge.getDependent().index()) { newHead = conjunct.index(); } } if (newHead < Integer.MAX_VALUE) { newHeads.put(edge, newHead); } } } } for (SemanticGraphEdge edge : newHeads.keySet()) { IndexedWord newGovernor = sg.getNodeByIndex(newHeads.get(edge)); sg.removeEdge(edge); sg.addEdge(newGovernor, edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra()); } }
Integer sourceCopy = edge.getSource().copyCount(); Integer targetCopy = edge.getTarget().copyCount(); boolean isExtra = edge.isExtra();
/** * Given a list of edges, attempts to create and return a rooted SemanticGraph. * * TODO: throw Exceptions, or flag warnings on conditions for concern (no root, etc) */ public static SemanticGraph makeFromEdges(Iterable<SemanticGraphEdge> edges) { // Identify the root(s) of this graph SemanticGraph sg = new SemanticGraph(); Collection<IndexedWord> vertices = getVerticesFromEdgeSet(edges); for (IndexedWord vertex : vertices) { sg.addVertex(vertex); } for (SemanticGraphEdge edge : edges) { sg.addEdge(edge.getSource(),edge.getTarget(), edge.getRelation(), edge.getWeight(), edge.isExtra()); } sg.resetRoots(); return sg; }
/** * Given a list of graphs, constructs a new graph combined from the * collection of graphs. Original vertices are used, edges are * copied. Graphs are ordered by the sentence index and index of * the original vertices. Intent is to create a "mega graph" * similar to the graphs used in the RTE problem. * <br> * This method only works if the indexed words have different * sentence ids, as otherwise the maps used will confuse several of * the IndexedWords. */ public static SemanticGraph makeFromGraphs(Collection<SemanticGraph> sgList) { SemanticGraph sg = new SemanticGraph(); Collection<IndexedWord> newRoots = Generics.newHashSet(); for (SemanticGraph currSg : sgList) { newRoots.addAll(currSg.getRoots()); for (IndexedWord currVertex : currSg.vertexSet()) sg.addVertex(currVertex); for (SemanticGraphEdge currEdge : currSg.edgeIterable()) sg.addEdge(currEdge.getGovernor(), currEdge.getDependent(), currEdge.getRelation(), currEdge.getWeight(), currEdge.isExtra()); } sg.setRoots(newRoots); return sg; }
/** * Replaces a node in the given SemanticGraph with the new node, * replacing its position in the node edges. */ public static void replaceNode(IndexedWord newNode, IndexedWord oldNode, SemanticGraph sg) { // Obtain the edges where the old node was the governor and the dependent. // Remove the old node, insert the new, and re-insert the edges. // Save the edges in a list so that remove operations don't affect // the iterator or our ability to find the edges in the first place List<SemanticGraphEdge> govEdges = sg.outgoingEdgeList(oldNode); List<SemanticGraphEdge> depEdges = sg.incomingEdgeList(oldNode); boolean oldNodeRemoved = sg.removeVertex(oldNode); if (oldNodeRemoved) { // If the new node is not present, be sure to add it in. if (!sg.containsVertex(newNode)) { sg.addVertex(newNode); } for (SemanticGraphEdge govEdge : govEdges) { sg.removeEdge(govEdge); sg.addEdge(newNode, govEdge.getDependent(), govEdge.getRelation(), govEdge.getWeight(), govEdge.isExtra()); } for (SemanticGraphEdge depEdge : depEdges) { sg.removeEdge(depEdge); sg.addEdge(depEdge.getGovernor(), newNode, depEdge.getRelation(), depEdge.getWeight(), depEdge.isExtra()); } } else { log.info("SemanticGraphUtils.replaceNode: previous node does not exist"); } }
/** * This creates a new graph based off the given, but uses the existing nodes objects. */ public static SemanticGraph duplicateKeepNodes(SemanticGraph sg) { SemanticGraph retSg = new SemanticGraph(); for (IndexedWord node : sg.vertexSet()) { retSg.addVertex(node); } retSg.setRoots(sg.getRoots()); for (SemanticGraphEdge edge : sg.edgeIterable()) { retSg.addEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra()); } return retSg; }
/** * Given a graph, ensures all edges are EnglishGrammaticalRelations. * NOTE: this is English specific. * NOTE: currently EnglishGrammaticalRelations does not link collapsed prep string forms * back to their object forms, for its valueOf relation. This may need to be repaired if * generated edges indeed do have collapsed preps as strings. */ private static void enRepairEdges(SemanticGraph sg, boolean verbose) { for (SemanticGraphEdge edge : sg.edgeIterable()) { if (edge.getRelation().isFromString()) { GrammaticalRelation newReln = EnglishGrammaticalRelations.valueOf(edge.getRelation().toString()); if (newReln != null) { IndexedWord gov = edge.getGovernor(); IndexedWord dep = edge.getDependent(); double weight = edge.getWeight(); boolean isExtra = edge.isExtra(); sg.removeEdge(edge); sg.addEdge(gov, dep, newReln, weight, isExtra); } else { if (verbose) log.info("Warning, could not find matching GrammaticalRelation for reln=" + edge.getRelation()); } } } }
.setTarget(edge.getTarget().index()) .setDep(edge.getRelation().toString()) .setIsExtra(edge.isExtra()) .setSourceCopy(edge.getSource().copyCount()) .setTargetCopy(edge.getTarget().copyCount())