Refine search
public double subjDeletionProbability(SemanticGraphEdge edge, Iterable<SemanticGraphEdge> neighbors) { // Get information about the neighbors // (in a totally not-creepy-stalker sort of way) for (SemanticGraphEdge neighbor : neighbors) { if (neighbor != edge) { String neighborRel = neighbor.getRelation().toString(); if (neighborRel.contains("subj")) { return 1.0; } } } return 0.0; }
private static final boolean isModifier(SemanticGraphEdge edge) { return MODIFIER_PATTERN.matcher(edge.getRelation().toString()).matches(); }
@Override public String toString() { if (!printOnlyRelation) { return getSource() + " -> " + getTarget() + " (" + getRelation() + ")"; } else { return getRelation().toString(); } }
/** * 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; }
/** * Backtrace from a search state, collecting all of the deleted edges used to get there. * @param state The final search state. * @param justDeleted The edges we have just deleted. * @param otherEdges Other deletions we want to register * @return A list of deleted edges for that search state. */ private static List<String> aggregateDeletedEdges(SearchState state, Iterable<SemanticGraphEdge> justDeleted, Iterable<String> otherEdges) { List<String> rtn = new ArrayList<>(); for (SemanticGraphEdge edge : justDeleted) { rtn.add(edge.getRelation().toString()); } for (String edge : otherEdges) { rtn.add(edge); } while (state != null) { if (state.lastDeletedEdge != null) { rtn.add(state.lastDeletedEdge); } state = state.source; } return rtn; } }
private static final boolean isArgument(SemanticGraph sg, SemanticGraphEdge edge) { boolean matches = ARGUMENT_PATTERN.matcher(edge.getRelation().toString()).matches(); if (matches) { for (SemanticGraphEdge edge2 : sg.outgoingEdgeIterable(edge.getDependent())) { if (edge2.getRelation().equals(UniversalEnglishGrammaticalRelations.ORPHAN)) { return false; } } return true; } else { return false; } }
private static final boolean isClausalArgument(SemanticGraph sg, SemanticGraphEdge edge) { boolean matches = CLAUSAL_ARGUMENT_PATTERN.matcher(edge.getRelation().toString()).matches(); if (matches) { for (SemanticGraphEdge edge2 : sg.outgoingEdgeIterable(edge.getDependent())) { if (edge2.getRelation().equals(UniversalEnglishGrammaticalRelations.ORPHAN)) { return false; } } return true; } else { return false; } }
/** * Since graphs can be have preps collapsed, finds all the immediate children of this node * that are linked by a collapsed preposition edge. */ public static List<IndexedWord> getChildrenWithPrepC(SemanticGraph sg, IndexedWord vertex) { List<IndexedWord> ret = new ArrayList<>(); // Collection<GrammaticalRelation> prepCs = EnglishGrammaticalRelations.getPrepsC(); // for (SemanticGraphEdge edge : sg.outgoingEdgesOf(vertex)) { // if (prepCs.contains(edge.getRelation())) for (SemanticGraphEdge edge : sg.outgoingEdgeIterable(vertex)) { if (edge.getRelation().toString().startsWith("prep")) ret.add(edge.getDependent()); } return ret; }
/** * Pretty printers */ public static String printEdges(Iterable<SemanticGraphEdge> edges) { StringWriter buf = new StringWriter(); for (SemanticGraphEdge edge : edges) { buf.append("\t"); buf.append(edge.getRelation().toString()); buf.append("("); buf.append(edge.getGovernor().toString()); buf.append(", "); buf.append(edge.getDependent().toString()); buf.append(")\n"); } return buf.toString(); }
@Override public boolean prerequisitesMet(SemanticGraph originalTree, SemanticGraphEdge edge) { // Only valid if there's a single nontrivial outgoing edge from a node. Otherwise it's a whole can of worms. Iterator<SemanticGraphEdge> iter = originalTree.outgoingEdgeIterable(edge.getGovernor()).iterator(); if (!iter.hasNext()) { return false; // what? } boolean nontrivialEdge = false; while (iter.hasNext()) { SemanticGraphEdge outEdge = iter.next(); switch (outEdge.getRelation().toString()) { case "nn": case "amod": break; default: if (nontrivialEdge) { return false; } nontrivialEdge = true; } } return true; }
@Override void advance() { if (node.equals(IndexedWord.NO_WORD)) { next = null; return; } while (!searchStack.isEmpty()) { if (neighborIterator == null || !neighborIterator.hasNext()) { IndexedWord search = searchStack.pop(); neighborIterator = neighborIterator(sg, search); } while (neighborIterator.hasNext()) { SemanticGraphEdge edge = neighborIterator.next(); IndexedWord otherEnd = followEdge(edge); if (!searchedNodes.contains(otherEnd)) { searchStack.push(otherEnd); searchedNodes.add(otherEnd); } if (type.test(edge.getRelation().toString()) && !matchedNodes.contains(otherEnd)) { matchedNodes.add(otherEnd); next = otherEnd; relation = edge.getRelation().toString(); return; } } } // oh well, fell through with no results next = null; } };
private void descendantsWithReln(SemanticGraph g, IndexedWord w, String relation, List<IndexedWord> seenNodes, List<IndexedWord> descendantSet) { if (seenNodes.contains(w)) return; seenNodes.add(w); if (descendantSet.contains(w)) return; if (ignoreCommonTags && ignoreTags.contains(w.tag().trim())) return; for (IndexedWord child : g.getChildren(w)) { for (SemanticGraphEdge edge : g.getAllEdges(w, child)) { if (edge.getRelation().toString().equals(relation)) { descendantSet.add(child); } } descendantsWithReln(g, child, relation, seenNodes, descendantSet); } }
private String toXMLString() { StringBuilder buf = new StringBuilder("<dependencies style=\"typed\">\n"); for (SemanticGraphEdge edge : this.edgeListSorted()) { String reln = edge.getRelation().toString(); String gov = (edge.getSource()).word(); int govIdx = (edge.getSource()).index(); String dep = (edge.getTarget()).word(); int depIdx = (edge.getTarget()).index(); buf.append(" <dep type=\"").append(reln).append("\">\n"); buf.append(" <governor idx=\"").append(govIdx).append("\">").append(gov).append("</governor>\n"); buf.append(" <dependent idx=\"").append(depIdx).append("\">").append(dep).append("</dependent>\n"); buf.append(" </dep>\n"); } buf.append("</dependencies>\n"); return buf.toString(); }
@Override public boolean prerequisitesMet(SemanticGraph originalTree, SemanticGraphEdge edge) { // Don't split into anything but verbs or nouns char tag = edge.getDependent().tag().charAt(0); if (tag != 'V' && tag != 'N') { return false; } for (SemanticGraphEdge grandchild : originalTree.outgoingEdgeIterable(edge.getDependent())) { if (grandchild.getRelation().toString().contains("subj")) { return false; } } return true; }
private String toReadableString() { StringBuilder buf = new StringBuilder(); buf.append(String.format("%-20s%-20s%-20s%n", "dep", "reln", "gov")); buf.append(String.format("%-20s%-20s%-20s%n", "---", "----", "---")); for (IndexedWord root : getRoots()) { buf.append(String.format("%-20s%-20s%-20s%n", root.toString(CoreLabel.OutputFormat.VALUE_TAG_INDEX), "root", "root")); } for (SemanticGraphEdge edge : this.edgeListSorted()) { buf.append(String.format("%-20s%-20s%-20s%n", edge.getTarget().toString(CoreLabel.OutputFormat.VALUE_TAG_INDEX), edge.getRelation().toString(), edge.getSource().toString(CoreLabel.OutputFormat.VALUE_TAG_INDEX))); } return buf.toString(); }
public int compare(SemanticGraphEdge o1, SemanticGraphEdge o2) { int targetVal = o1.getTarget().compareTo(o2.getTarget()); if (targetVal != 0) { return targetVal; } int sourceVal = o1.getSource().compareTo(o2.getSource()); if (sourceVal != 0) { return sourceVal; } return o1.getRelation().toString().compareTo(o2.getRelation().toString()); // todo: cdm: surely we shouldn't have to do toString() now? }
@Override public boolean prerequisitesMet(SemanticGraph originalTree, SemanticGraphEdge edge) { // Don't split into anything but verbs or nouns char tag = edge.getDependent().tag().charAt(0); if (tag != 'V' && tag != 'N') { return false; } for (SemanticGraphEdge grandchild : originalTree.outgoingEdgeIterable(edge.getDependent())) { if (grandchild.getRelation().toString().contains("subj")) { return false; } } return true; }
/** * Returns a list of all children bearing a grammatical relation starting with the given string, relnPrefix */ public static List<IndexedWord> getChildrenWithRelnPrefix(SemanticGraph graph, IndexedWord vertex, String relnPrefix) { if (vertex.equals(IndexedWord.NO_WORD)) return new ArrayList<>(); if (!graph.containsVertex(vertex)) { throw new IllegalArgumentException(); } List<IndexedWord> childList = new ArrayList<>(); for (SemanticGraphEdge edge : graph.outgoingEdgeIterable(vertex)) { if (edge.getRelation().toString().startsWith(relnPrefix)) { childList.add(edge.getTarget()); } } return childList; }
@Override public void advance() { if (node.equals(IndexedWord.NO_WORD)) { next = null; return; } if (iterator == null) { iterator = sg.outgoingEdgeIterator(node); } while (iterator.hasNext()) { SemanticGraphEdge edge = iterator.next(); relation = edge.getRelation().toString(); if (!type.test(relation)) { continue; } this.next = edge.getTarget(); return; } this.next = null; } };
/** * Returns a list of all children bearing a grammatical relation starting with the given set of relation prefixes */ public static List<IndexedWord> getChildrenWithRelnPrefix(SemanticGraph graph, IndexedWord vertex, Collection<String> relnPrefixes) { if (vertex.equals(IndexedWord.NO_WORD)) return new ArrayList<>(); if (!graph.containsVertex(vertex)) { throw new IllegalArgumentException(); } List<IndexedWord> childList = new ArrayList<>(); for (SemanticGraphEdge edge : graph.outgoingEdgeIterable(vertex)) { String edgeString = edge.getRelation().toString(); for (String relnPrefix : relnPrefixes) { if (edgeString.startsWith(relnPrefix)) { childList.add(edge.getTarget()); break; } } } return childList; }