@Override IndexedWord followEdge(SemanticGraphEdge edge) { return edge.getSource(); }
public IndexedWord getGovernor() { return getSource(); }
/** * Similar to toList(), but uses POS tags instead of word and index. */ public String toPOSList() { StringBuilder buf = new StringBuilder(); for (SemanticGraphEdge edge : this.edgeListSorted()) { buf.append(edge.getRelation()).append("("); buf.append(edge.getSource()).append(","); buf.append(edge.getTarget()).append(")\n"); } return buf.toString(); }
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(); }
public boolean containsEdge(SemanticGraphEdge edge) { return containsEdge(edge.getSource(), edge.getTarget()); }
public boolean removeEdge(SemanticGraphEdge e) { return graph.removeEdge(e.getSource(), e.getTarget(), e); }
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 String toDotFormat(String graphname, CoreLabel.OutputFormat indexedWordFormat) { StringBuilder output = new StringBuilder(); output.append("digraph " + graphname + " {\n"); for (IndexedWord word : graph.getAllVertices()) { output.append(" N_" + word.index() + " [label=\"" + word.toString(indexedWordFormat) + "\"];\n"); } for (SemanticGraphEdge edge : graph.edgeIterable()) { output.append(" N_" + edge.getSource().index() + " -> N_" + edge.getTarget().index() + " [label=\"" + edge.getRelation() + "\"];\n"); } output.append("}\n"); return output.toString(); }
/** * Returns a set of all parents bearing a certain grammatical relation, or an * empty set if none. */ public Set<IndexedWord> getParentsWithReln(IndexedWord vertex, String relnName) { if (vertex.equals(IndexedWord.NO_WORD)) return Collections.emptySet(); if (!containsVertex(vertex)) throw new IllegalArgumentException(); Set<IndexedWord> parentList = wordMapFactory.newSet(); for (SemanticGraphEdge edge : incomingEdgeIterable(vertex)) { if (edge.getRelation().toString().equals(relnName)) { parentList.add(edge.getSource()); } } return parentList; }
/** * Returns a list of pairs of a relation name and the parent * IndexedFeatureLabel to which we bear that relation. */ public List<Pair<GrammaticalRelation, IndexedWord>> parentPairs(IndexedWord vertex) { if (!containsVertex(vertex)) { throw new IllegalArgumentException(); } List<Pair<GrammaticalRelation, IndexedWord>> parentPairs = Generics.newArrayList(); for (SemanticGraphEdge e : incomingEdgeIterable(vertex)) { parentPairs.add(new Pair<>(e.getRelation(), e.getSource())); } return parentPairs; }
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? }
/** * Returns a set of all parents bearing a certain grammatical relation, or an * empty set if none. */ public Set<IndexedWord> getParentsWithReln(IndexedWord vertex, GrammaticalRelation reln) { if (vertex.equals(IndexedWord.NO_WORD)) return Collections.emptySet(); if (!containsVertex(vertex)) throw new IllegalArgumentException(); Set<IndexedWord> parentList = wordMapFactory.newSet(); for (SemanticGraphEdge edge : incomingEdgeIterable(vertex)) { if (edge.getRelation().equals(reln)) { parentList.add(edge.getSource()); } } return parentList; }
@Override public String toString() { if (!printOnlyRelation) { return getSource() + " -> " + getTarget() + " (" + getRelation() + ")"; } else { return getRelation().toString(); } }
/** Compares SemanticGraphEdges. * Warning: compares on the sources, targets, and then the STRINGS of the relations. * @param other Edge to compare to * @return Whether this is smaller, same, or larger */ public int compareTo(SemanticGraphEdge other) { int sourceVal = getSource().compareTo(other.getSource()); if (sourceVal != 0) { return sourceVal; } int targetVal = getTarget().compareTo(other.getTarget()); if (targetVal !=0 ) { return targetVal; } String thisRelation = getRelation().toString(); String thatRelation = other.getRelation().toString(); return thisRelation.compareTo(thatRelation); }
public SemanticGraphEdge(SemanticGraphEdge e) { this(e.getSource(), e.getTarget(), e.getRelation(), e.getWeight(), e.isExtra()); }
/** * Given a governor, dependent, and the relation between them, returns the * SemanticGraphEdge object of that arc if it exists, otherwise returns null. */ public SemanticGraphEdge getEdge(IndexedWord gov, IndexedWord dep, GrammaticalRelation reln) { Collection<SemanticGraphEdge> edges = getAllEdges(gov, dep); if (edges != null) { for (SemanticGraphEdge edge : edges) { if (!edge.getSource().equals(gov)) continue; if ((edge.getRelation().equals(reln))) { return edge; } } } return null; }
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; }
private void addDependencyFeatures(Counter<String> features, String prefix, SemanticGraphEdge e, boolean addWord) { if (e == null) { features.incrementCount("no-" + prefix); return; } IndexedWord parent = e.getSource(); String parentPOS = parent.tag(); String parentWord = parent.word(); String parentRelation = e.getRelation().toString(); //String parentDir = e.getSource().beginPosition() < e.getTarget().beginPosition() // ? "right" : "left"; if (addWord) { features.incrementCount(prefix + "-word=" + wordIndicator(parentWord, parentPOS)); } features.incrementCount(prefix + "-POS=" + parentPOS); features.incrementCount(prefix + "-relation=" + parentRelation); //features.incrementCount(prefix + "-direction=" + parentDir); }
/** * 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; }