@Override IndexedWord followEdge(SemanticGraphEdge edge) { return edge.getTarget(); }
public IndexedWord getDependent() { return getTarget(); }
/** * 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 void recToString(IndexedWord curr, CoreLabel.OutputFormat wordFormat, StringBuilder sb, int offset, Set<IndexedWord> used) { used.add(curr); List<SemanticGraphEdge> edges = outgoingEdgeList(curr); Collections.sort(edges); for (SemanticGraphEdge edge : edges) { IndexedWord target = edge.getTarget(); sb.append(space(2 * offset)).append("-> ").append(target.toString(wordFormat)).append(" (").append(edge.getRelation()).append(")\n"); if (!used.contains(target)) { // recurse recToString(target, wordFormat, sb, offset + 1, used); } } }
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(); }
/** * 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; }
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(); }
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 list of pairs of a relation name and the child * IndexedFeatureLabel that bears that relation. */ public List<Pair<GrammaticalRelation, IndexedWord>> childPairs(IndexedWord vertex) { if (!containsVertex(vertex)) { throw new IllegalArgumentException(); } List<Pair<GrammaticalRelation, IndexedWord>> childPairs = Generics.newArrayList(); for (SemanticGraphEdge e : outgoingEdgeIterable(vertex)) { childPairs.add(new Pair<>(e.getRelation(), e.getTarget())); } return childPairs; }
/** * Returns a set of all children bearing a certain grammatical relation, or * an empty set if none. */ public Set<IndexedWord> getChildrenWithReln(IndexedWord vertex, GrammaticalRelation reln) { if (vertex.equals(IndexedWord.NO_WORD)) return Collections.emptySet(); if (!containsVertex(vertex)) throw new IllegalArgumentException(); Set<IndexedWord> childList = wordMapFactory.newSet(); for (SemanticGraphEdge edge : outgoingEdgeIterable(vertex)) { if (edge.getRelation().equals(reln)) { childList.add(edge.getTarget()); } } return childList; }
/** * Does the given {@code vertex} have at least one child with the given {@code reln} and the lemma {@code childLemma}? */ public boolean hasChild(IndexedWord vertex, GrammaticalRelation reln, String childLemma) { if (!containsVertex(vertex)) { throw new IllegalArgumentException(); } for (SemanticGraphEdge edge : outgoingEdgeIterable(vertex)) { if (edge.getRelation().equals(reln)) { if (edge.getTarget().get(CoreAnnotations.LemmaAnnotation.class).equals(childLemma)) { return true; } } } return false; }
@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); }
/** * Returns the first IndexedFeatureLabel bearing a certain grammatical * relation, or null if none. */ public IndexedWord getChildWithReln(IndexedWord vertex, GrammaticalRelation reln) { if (vertex.equals(IndexedWord.NO_WORD)) return null; if (!containsVertex(vertex)) throw new IllegalArgumentException(); for (SemanticGraphEdge edge : outgoingEdgeIterable(vertex)) { if (edge.getRelation().equals(reln)) { return edge.getTarget(); } } return null; }
public SemanticGraphEdge(SemanticGraphEdge e) { this(e.getSource(), e.getTarget(), e.getRelation(), e.getWeight(), e.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; }