/** * Same as above, but returns {@code null} if the index does not exist * (instead of throwing an exception). */ public IndexedWord getNodeByIndexAndCopyCountSafe(int index, int copyCount) { for (IndexedWord vertex : vertexSet()) { if (vertex.index() == index && vertex.copyCount() == copyCount) { return vertex; } } return null; }
@SuppressWarnings("RedundantIfStatement") public boolean isCopy(IndexedWord otherWord) { Integer myInd = get(CoreAnnotations.IndexAnnotation.class); Integer otherInd = otherWord.get(CoreAnnotations.IndexAnnotation.class); if ( ! Objects.equals(myInd, otherInd)) { return false; } Integer mySentInd = get(CoreAnnotations.SentenceIndexAnnotation.class); Integer otherSentInd = otherWord.get(CoreAnnotations.SentenceIndexAnnotation.class); if ( ! Objects.equals(mySentInd, otherSentInd)) { return false; } String myDocID = getString(CoreAnnotations.DocIDAnnotation.class); String otherDocID = otherWord.getString(CoreAnnotations.DocIDAnnotation.class); if ( ! Objects.equals(myDocID, otherDocID)) { return false; } if (copyCount() == 0 || otherWord.copyCount() != 0) { return false; } return true; }
return false; if (dep.copyCount() > 0) { return false;
if(node.copyCount() > 0){ pw.print("-"); pw.print(node.copyCount()); if (node.copyCount() > 0) { pw.print("-R"); } else { pw.print(" "); pw.print(edge.getTarget().index()); if (edge.isExtra() || edge.getSource().copyCount() > 0 || edge.getTarget().copyCount() > 0) { pw.print(" "); pw.print(edge.isExtra()); pw.print(" "); pw.print(edge.getSource().copyCount()); pw.print(" "); pw.print(edge.getTarget().copyCount());
/** * 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()); } }
public CoreNLPProtos.IndexedWord createIndexedWordProtoFromIW(IndexedWord iw) { CoreNLPProtos.IndexedWord.Builder builder = CoreNLPProtos.IndexedWord.newBuilder(); if (iw == null) { builder.setSentenceNum(-1); builder.setTokenIndex(-1); } else { builder.setSentenceNum(iw.get(SentenceIndexAnnotation.class) - 1); builder.setTokenIndex(iw.get(IndexAnnotation.class) - 1); builder.setCopyCount(iw.copyCount()); } return builder.build(); }
.setSentenceIndex(node.get(SentenceIndexAnnotation.class)) .setIndex(node.index()); if (node.copyCount() > 0) { nodeBuilder.setCopyAnnotation(node.copyCount()); .setDep(edge.getRelation().toString()) .setIsExtra(edge.isExtra()) .setSourceCopy(edge.getSource().copyCount()) .setTargetCopy(edge.getTarget().copyCount()) .setLanguage(toProto(edge.getRelation().getLanguage())));
HashMap<IndexedWord, IndexedWord> copiedNodes = new HashMap<>(); IndexedWord conjGovCopy = conjGov.makeSoftCopy(); conjGovCopy.setPseudoPosition(conjGovCopy.pseudoPosition() + conjGovCopy.copyCount() / 10.0); SemanticGraphEdge edge = sg.getEdge(conjGov, orphanGov); sg.removeEdge(edge); if (sourceNode == null) { IndexedWord copyNode = parallelEdge.getGovernor().makeSoftCopy(); copyNode.setPseudoPosition(copyNode.pseudoPosition() + copyNode.copyCount() / 10.0); copiedNodes.put(parallelEdge.getGovernor(), copyNode); newCopyNode = true; copyNode.setPseudoPosition(copyNode.pseudoPosition() + copyNode.copyCount() / 10.0); copiedNodes.put(parallelEdge.getDependent(), copyNode); newCopyNode = true;
private static Object getNodes(SemanticGraph graph) { if(graph != null) { List<IndexedWord> vertexList = graph.vertexListSorted(); int maxIndex = vertexList.get(vertexList.size() - 1).index(); return vertexList.stream().map( (IndexedWord token) -> (Consumer<Writer>) node -> { if (token.copyCount() == 0) { node.set("id", getNodeIndex(token, maxIndex)); node.set("start", token.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class)); node.set("end", token.get(CoreAnnotations.CharacterOffsetEndAnnotation.class)); } else { node.set("id", getNodeIndex(token, maxIndex)); node.set("source", token.index()); } node.set("form", token.word()); if (graph.getRoots().contains(token)) node.set("top", true); node.set("properties", (Consumer<Writer>) propertiesWriter -> { propertiesWriter.set("xpos", token.tag()); propertiesWriter.set("upos", token.get(CoreAnnotations.CoarseTagAnnotation.class)); propertiesWriter.set("lemma", token.lemma()); }); node.set("edges", graph.getOutEdgesSorted(token).stream().map( (SemanticGraphEdge dep) -> (Consumer<Writer>) edge -> { edge.set("target", getNodeIndex(dep.getDependent(), maxIndex)); edge.set("label", dep.getRelation().toString()); })); } ); } else { return null; } }
nodeMap.put(word.index(), word.copyCount(), word); graph.addVertex(word); if (in.isRoot) {
private static IndexedWord getToken(List<IndexedWord> sortedTokens, int index, int copyCount) { int tokenLength = sortedTokens.size(); for (int i = index - 1 ; i < tokenLength; i++) { IndexedWord token = sortedTokens.get(i); if (token.index() == index && token.copyCount() == copyCount) { return token; } } return null; }
private static IndexedWord getToken(List<IndexedWord> sortedTokens, int index, int copyCount) { int tokenLength = sortedTokens.size(); for (int i = index - 1 ; i < tokenLength; i++) { IndexedWord token = sortedTokens.get(i); if (token.index() == index && token.copyCount() == copyCount) { return token; } } return null; }
/** * Same as above, but returns {@code null} if the index does not exist * (instead of throwing an exception). */ public IndexedWord getNodeByIndexAndCopyCountSafe(int index, int copyCount) { for (IndexedWord vertex : vertexSet()) { if (vertex.index() == index && vertex.copyCount() == copyCount) { return vertex; } } return null; }
/** * Same as above, but returns {@code null} if the index does not exist * (instead of throwing an exception). */ public IndexedWord getNodeByIndexAndCopyCountSafe(int index, int copyCount) { for (IndexedWord vertex : vertexSet()) { if (vertex.index() == index && vertex.copyCount() == copyCount) { return vertex; } } return null; }
public CoreNLPProtos.IndexedWord createIndexedWordProtoFromIW(IndexedWord iw) { CoreNLPProtos.IndexedWord.Builder builder = CoreNLPProtos.IndexedWord.newBuilder(); if (iw == null) { builder.setSentenceNum(-1); builder.setTokenIndex(-1); } else { builder.setSentenceNum(iw.get(SentenceIndexAnnotation.class) - 1); builder.setTokenIndex(iw.get(IndexAnnotation.class) - 1); builder.setCopyCount(iw.copyCount()); } return builder.build(); }