/** * Returns an ordered list of edges in the graph. * This creates and sorts a list, so prefer edgeIterable(). * * @return A ordered list of edges in the graph. */ public List<SemanticGraphEdge> edgeListSorted() { ArrayList<SemanticGraphEdge> edgeList = new ArrayList<>(); for (SemanticGraphEdge edge : edgeIterable()) { edgeList.add(edge); } edgeList.sort(SemanticGraphEdge.orderByTargetComparator()); return edgeList; }
/** * Given a semantic graph, and a relation prefix, returns a list of all relations (edges) * that start with the given prefix (e.g., prefix "prep" gives you all the prep relations: prep_by, pref_in,etc.) * */ public static List<SemanticGraphEdge> findAllRelnsWithPrefix(SemanticGraph sg, String prefix) { ArrayList<SemanticGraphEdge> relns = new ArrayList<>(); for (SemanticGraphEdge edge : sg.edgeIterable()) { GrammaticalRelation edgeRelation = edge.getRelation(); if (edgeRelation.toString().startsWith(prefix)) { relns.add(edge); } } return relns; }
/** * Given a semantic graph, and the short name of a target relation, returns a list of all * relations (edges) matching. * */ public List<SemanticGraphEdge> findAllRelns(String tgtRelationShortname) { ArrayList<SemanticGraphEdge> relns = new ArrayList<>(); for (SemanticGraphEdge edge : edgeIterable()) { GrammaticalRelation edgeRelation = edge.getRelation(); if ((edgeRelation != null) && (edgeRelation.getShortName().equals(tgtRelationShortname))) { relns.add(edge); } } return relns; }
/** * Given a semantic graph, and a target relation, returns a list of all * relations (edges) matching. * */ public List<SemanticGraphEdge> findAllRelns(GrammaticalRelation tgtRelation) { ArrayList<SemanticGraphEdge> relns = new ArrayList<>(); for (SemanticGraphEdge edge : edgeIterable()) { GrammaticalRelation edgeRelation = edge.getRelation(); if ((edgeRelation != null) && (edgeRelation.equals(tgtRelation))) { relns.add(edge); } } return relns; }
this.featurizer = featurizer; this.tree.edgeIterable().forEach(edgeToIndex::addToIndex);
for (SemanticGraphEdge edge : graph.edgeIterable()) { if(! first) pw.print("\t"); String rel = edge.getRelation().toString();
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; }
for (SemanticGraphEdge edge : sg.edgeIterable()) { IndexedWord newGov = oldToNewVertices.get(edge.getGovernor()); IndexedWord newDep = oldToNewVertices.get(edge.getDependent());
/** * Strip away case edges, if the incoming edge is a preposition. * This replicates the behavior of the old Stanford dependencies on universal dependencies. * @param tree The tree to modify in place. */ public static void stripPrepCases(SemanticGraph tree) { // Find incoming case edges that have an 'nmod' incoming edge List<SemanticGraphEdge> toClean = new ArrayList<>(); for (SemanticGraphEdge edge : tree.edgeIterable()) { if ("case".equals(edge.getRelation().toString())) { boolean isPrepTarget = false; for (SemanticGraphEdge incoming : tree.incomingEdgeIterable(edge.getGovernor())) { if ("nmod".equals(incoming.getRelation().getShortName())) { isPrepTarget = true; break; } } if (isPrepTarget && !tree.outgoingEdgeIterator(edge.getDependent()).hasNext()) { toClean.add(edge); } } } // Delete these edges for (SemanticGraphEdge edge : toClean) { tree.removeEdge(edge); tree.removeVertex(edge.getDependent()); assert isTree(tree); } }
newWords.put(newVertex.index(), newVertex); for (SemanticGraphEdge edge : graph.edgeIterable()) { IndexedWord gov = newWords.get(edge.getGovernor().index() + vertexOffset);
/** * 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; }
/** * Post-process graph and copy over case markers in case the PP-complement * is conjoined. * @param sg */ public static void addCaseMarkerForConjunctions(SemanticGraph sg) { SemanticGraph sgCopy = sg.makeSoftCopy(); for (SemanticGraphEdge edge : sgCopy.edgeIterable()) { String relnName = edge.getRelation().toString(); if (relnName.equals("nmod") || relnName.equals("obl") || relnName.equals("acl") || relnName.equals("advcl")) { Set<IndexedWord> conjParents = sg.getParentsWithReln(edge.getDependent(), "conj"); for (IndexedWord conjParent : conjParents) { List<SemanticGraphEdge> conjParentIncomingEdges = sg.getIncomingEdgesSorted(conjParent); boolean changed = false; for (SemanticGraphEdge edge1: conjParentIncomingEdges) { if (edge1.getRelation().toString().startsWith(relnName) && edge1.getRelation().getSpecific() != null) { changed = true; sg.getEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation()).setRelation(edge1.getRelation()); break; } } if (changed) { break; } } } } }
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); } } }
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()); } }
for (SemanticGraphEdge dep : sg.edgeIterable()) {
/** * 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 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; }
/** * 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()); } } } }
for (SemanticGraphEdge edge : graph.edgeIterable()) {