private static void createMultiWordExpression(SemanticGraph sg, IndexedWord gov, GrammaticalRelation reln, IndexedWord... words) { if (sg.getRoots().isEmpty() || gov == null || words.length < 1) { return; } boolean first = true; IndexedWord mweHead = null; for (IndexedWord word : words) { IndexedWord wordGov = sg.getParent(word); if (wordGov != null) { SemanticGraphEdge edge = sg.getEdge(wordGov, word); if (edge != null) { sg.removeEdge(edge); } } if (first) { sg.addEdge(gov, word, reln, Double.NEGATIVE_INFINITY, false); mweHead = word; first = false; } else { sg.addEdge(mweHead, word, MULTI_WORD_EXPRESSION, Double.NEGATIVE_INFINITY, false); } } }
/** * A helper to add a single word to a given dependency tree * @param toModify The tree to add the word to. * @param root The root of the tree where we should be adding the word. * @param rel The relation to add the word with. * @param coreLabel The word to add. */ @SuppressWarnings("UnusedDeclaration") private static void addWord(SemanticGraph toModify, IndexedWord root, String rel, CoreLabel coreLabel) { IndexedWord dependent = new IndexedWord(coreLabel); toModify.addVertex(dependent); toModify.addEdge(root, dependent, GrammaticalRelation.valueOf(Language.English, rel), Double.NEGATIVE_INFINITY, false); }
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)); }
if (edge != null) { sg.removeEdge(edge); sg.addEdge(nmodGovCopy, conjDep, rel, Double.NEGATIVE_INFINITY, false); sg.addEdge(conjGov, nmodGovCopy, CONJUNCT, Double.NEGATIVE_INFINITY, false); newConjDeps.add(nmodGovCopy); if (ccEdge != null) { sg.removeEdge(ccEdge); sg.addEdge(nmodGovCopy, ccDep, COORDINATION, Double.NEGATIVE_INFINITY, false);
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; }
/** * This method will collapse a referent relation such as follows. e.g.: * "The man that I love ... " ref(man, that) dobj(love, that) -> ref(man, that) dobj(love, * man) */ private static void collapseReferent(SemanticGraph sg) { // find typed deps of form ref(gov, dep) // put them in a List for processing List<SemanticGraphEdge> refs = new ArrayList<>(sg.findAllRelns(REFERENT)); SemanticGraph sgCopy = sg.makeSoftCopy(); // now substitute target of referent where possible for (SemanticGraphEdge ref : refs) { IndexedWord dep = ref.getDependent();// take the relative word IndexedWord ant = ref.getGovernor();// take the antecedent for (Iterator<SemanticGraphEdge> iter = sgCopy.incomingEdgeIterator(dep); iter.hasNext(); ) { SemanticGraphEdge edge = iter.next(); // the last condition below maybe shouldn't be necessary, but it has // helped stop things going haywire a couple of times (it stops the // creation of a unit cycle that probably leaves something else // disconnected) [cdm Jan 2010] if (edge.getRelation() != REFERENT && ! edge.getGovernor().equals(ant)) { sg.removeEdge(edge); sg.addEdge(edge.getGovernor(), ant, edge.getRelation(), Double.NEGATIVE_INFINITY, true); } } } }
/** * 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()); } }
/** * This is the constructor used by the parser. */ public SemanticGraph(Collection<TypedDependency> dependencies) { graph = new DirectedMultiGraph<>(outerMapFactory, innerMapFactory); roots = wordMapFactory.newSet(); for (TypedDependency d : dependencies) { IndexedWord gov = d.gov(); IndexedWord dep = d.dep(); GrammaticalRelation reln = d.reln(); if (reln != ROOT) { // the root relation only points to the root: the governor is a fake node that we don't want to add in the graph // It is unnecessary to call addVertex, since addEdge will // implicitly add vertices if needed //addVertex(gov); //addVertex(dep); addEdge(gov, dep, reln, Double.NEGATIVE_INFINITY, d.extra()); } else { //it's the root and we add it addVertex(dep); roots.add(dep); } } // there used to be an if clause that filtered out the case of empty // dependencies. However, I could not understand (or replicate) the error // it alluded to, and it led to empty dependency graphs for very short // fragments, // which meant they were ignored by the RTE system. Changed. (pado) // See also SemanticGraphFactory.makeGraphFromTree(). }
public static void collapseReferent(SemanticGraph sg) { // find typed deps of form ref(gov, dep) // put them in a List for processing List<SemanticGraphEdge> refs = new ArrayList<>(sg.findAllRelns("ref")); SemanticGraph sgCopy = sg.makeSoftCopy(); // now substitute target of referent where possible for (SemanticGraphEdge ref : refs) { IndexedWord dep = ref.getDependent();// take the relative word IndexedWord ant = ref.getGovernor();// take the antecedent for (Iterator<SemanticGraphEdge> iter = sgCopy.incomingEdgeIterator(dep); iter.hasNext(); ) { SemanticGraphEdge edge = iter.next(); // the last condition below maybe shouldn't be necessary, but it has // helped stop things going haywire a couple of times (it stops the // creation of a unit cycle that probably leaves something else // disconnected) [cdm Jan 2010] if (! edge.getRelation().getShortName().equals("ref") && ! edge.getGovernor().equals(ant)) { sg.removeEdge(edge); GrammaticalRelation reln = edge.getRelation(); if (edge.getRelation().getShortName().equals("obj")) { reln = RELATIVE_OBJECT; } else if (edge.getRelation().getShortName().equals("nsubj")) { reln = RELATIVE_NOMINAL_SUBJECT; } else if (edge.getRelation().getShortName().equals("nsubj:pass")) { reln = RELATIVE_NOMINAL_PASSIVE_SUBJECT; } sg.addEdge(edge.getGovernor(), ant, reln, RELCL_EDGE_WEIGHT, true); } } } }
@Override public void evaluate(SemanticGraph sg, SemgrexMatcher sm) { IndexedWord govNode = getNamedNode(govName, sm); IndexedWord depNode = getNamedNode(depName, sm); SemanticGraphEdge existingEdge = sg.getEdge(govNode, depNode, relation); if (existingEdge == null) { // When adding the edge, check to see if the gov/dep nodes are presently in the graph. // if (!sg.containsVertex(govNode)) sg.addVertex(govNode); if (!sg.containsVertex(depNode)) sg.addVertex(depNode); sg.addEdge(govNode, depNode, relation, weight,false ); } }
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 void readDep(IndexedWord gov, String reln) { readWhiteSpace(); if (!isLeftBracket(peek())) { // it's a leaf String label = readName(); IndexedWord dep = makeVertex(label); sg.addVertex(dep); if (gov == null) sg.roots.add(dep); sg.addEdge(gov, dep, GrammaticalRelation.valueOf(this.language, reln), Double.NEGATIVE_INFINITY, false); } else { readLeftBracket(); String label = readName(); IndexedWord dep = makeVertex(label); sg.addVertex(dep); if (gov == null) sg.roots.add(dep); if (gov != null && reln != null) { sg.addEdge(gov, dep, GrammaticalRelation.valueOf(this.language, reln), Double.NEGATIVE_INFINITY, false); } readWhiteSpace(); while (!isRightBracket(peek()) && !isEOF) { reln = readName(); readRelnSeparator(); readDep(dep, reln); readWhiteSpace(); } readRightBracket(); } }
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; }
/** * 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; }
/** * 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"); } }
/** * TODO: figure out how to specify where in the sentence this node goes. * TODO: determine if we should be copying an IndexedWord, or working just with a FeatureLabel. * TODO: bombproof if this gov, dep, and reln already exist. */ @Override public void evaluate(SemanticGraph sg, SemgrexMatcher sm) { IndexedWord govNode = sm.getNode(govNodeName); IndexedWord newNode = new IndexedWord(newNodePrototype); int newIndex = SemanticGraphUtils.leftMostChildVertice(govNode, sg).index(); // cheap En-specific hack for placing copula (beginning of governing phrase) newNode.setDocID(govNode.docID()); newNode.setIndex(newIndex); newNode.setSentIndex(govNode.sentIndex()); sg.addVertex(newNode); sg.addEdge(govNode, newNode, relation, weight,false); }
/** * 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()); } } } }