public SemanticGraphEdge(SemanticGraphEdge e) { this(e.getSource(), e.getTarget(), e.getRelation(), e.getWeight(), e.isExtra()); }
retSg.addEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra());
IndexedWord newGov = oldToNewVertices.get(edge.getGovernor()); IndexedWord newDep = oldToNewVertices.get(edge.getDependent()); nsg.addEdge(newGov, newDep, edge.getRelation(), edge.getWeight(), edge.isExtra());
toModify.addEdge(edge.getGovernor(), edge.getDependent(), 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()); });
throw new AssertionError("Counting problem (or broken edge)"); newGraph.addEdge(gov, dep, 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()); } }
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());
newDep = edge.getDependent(); SemanticGraphEdge newEdge = new SemanticGraphEdge(newGov, newDep, edge.getRelation(), edge.getWeight(), edge.isExtra()); retList.add(newEdge);
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()); } }
/** * 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()); } } } }