/** * Used to make a mapping that lets you create "anonymous" versions of shared nodes between two * graphs (given in the arg) using the shared prefix. */ public static Map<IndexedWord, IndexedWord> makeGenericVertices(Iterable<IndexedWord> verts) { return anonymyizeNodes(verts, SHARED_NODE_ANON_PREFIX); }
/** * Returns the set of incoming edges for the given node that have the given * relation. * * Because certain edges may remain in string form (prepcs), check for both * string and object form of relations. */ public static List<SemanticGraphEdge> incomingEdgesWithReln(IndexedWord node, SemanticGraph sg, GrammaticalRelation reln) { return edgesWithReln(sg.incomingEdgeIterable(node), reln); }
public static void enRepairEdges(SemanticGraph sg) { enRepairEdges(sg, false); }
} else if (useWordAsLabel) { buf.append("="); buf.append(sanitizeForSemgrexName(vertice.word())); buf.append(" "); buf.append(semgrexFromGraphHelper(tgtVert, sg, tabu, seenEdges, useWordAsLabel, nameEdges, wildcardNodes, nodeNameMap, orderedNodes, nodeValuesTransformation)); if (applyParens)
/** * Given a SemanticGraph, creates a SemgrexPattern string based off of this graph. * NOTE: the word() value of the vertice is the name to reference * NOTE: currently presumes there is only one root in this graph. * TODO: see if Semgrex can allow multiroot patterns * @param sg SemanticGraph to base this pattern on. */ public static String semgrexFromGraph(SemanticGraph sg, boolean matchTag, boolean matchWord, Map<IndexedWord, String> nodeNameMap) throws Exception { return semgrexFromGraph(sg, null, matchTag, matchWord, nodeNameMap); }
/** * Given a starting vertice, grabs the subtree encapsulated by portion of the semantic graph, excluding * a given edge. A tabu list is maintained, in order to deal with cyclical relations (such as between a * rcmod (relative clause) and its nsubj). * */ public static Set<SemanticGraphEdge> getSubTreeEdges(IndexedWord vertice, SemanticGraph sg, SemanticGraphEdge excludedEdge) { Set<SemanticGraphEdge> tabu = Generics.newHashSet(); tabu.add(excludedEdge); getSubTreeEdgesHelper(vertice, sg, tabu); tabu.remove(excludedEdge); // Do not want this in the returned edges return tabu; }
public static Collection<SemanticGraph> removeDuplicates(Collection<SemanticGraph> graphs, SemanticGraph tabuGraph) { Collection<SemanticGraph> tabuSet = Generics.newHashSet(); tabuSet.add(tabuGraph); return removeDuplicates(graphs, tabuSet); }
public static String printVertices(SemanticGraph sg) { return printVertices(sg, new PrintVerticeParams()); }
/** * GIven a graph, returns a new graph with the the new sentence index enforced. * NOTE: new vertices are inserted. * TODO: is this ok? rewrite this? */ public static SemanticGraph setSentIndex(SemanticGraph sg, int newSentIndex) { SemanticGraph newGraph = new SemanticGraph(sg); List<IndexedWord> prevRoots = new ArrayList<>(newGraph.getRoots()); List<IndexedWord> newRoots = new ArrayList<>(); // TODO: we are using vertexListSorted here because we're changing // vertices while iterating. Perhaps there is a better way to do it. for (IndexedWord node : newGraph.vertexListSorted()) { IndexedWord newWord = new IndexedWord(node); newWord.setSentIndex(newSentIndex); SemanticGraphUtils.replaceNode(newWord, node, newGraph); if (prevRoots.contains(node)) newRoots.add(newWord); } newGraph.setRoots(newRoots); return newGraph; }
public String printSemanticGraph(SemanticGraph sg, boolean unescapeParenthesis) { boolean isTree = SemanticGraphUtils.isTree(sg);
} else if (useWordAsLabel) { buf.append("="); buf.append(sanitizeForSemgrexName(vertice.word())); buf.append(" "); buf.append(semgrexFromGraphHelper(tgtVert, sg, tabu, seenEdges, useWordAsLabel, nameEdges, wildcardNodes, nodeNameMap, orderedNodes, nodeValuesTransformation)); if (applyParens)
/** * Given a set of edges that form a rooted and connected graph, returns a Semgrex pattern * corresponding to it. * @throws Exception */ public static String semgrexFromGraph(Iterable<SemanticGraphEdge> edges, boolean matchTag, boolean matchWord, Map<IndexedWord, String> nodeNameMap) throws Exception { SemanticGraph sg = SemanticGraphFactory.makeFromEdges(edges); return semgrexFromGraph(sg, matchTag, matchWord, nodeNameMap); }
public static void getSubTreeEdgesHelper(IndexedWord vertice, SemanticGraph sg, Set<SemanticGraphEdge> tabuEdges) { for (SemanticGraphEdge edge : sg.outgoingEdgeIterable(vertice)) { if (!tabuEdges.contains(edge)) { IndexedWord dep = edge.getDependent(); tabuEdges.add(edge); getSubTreeEdgesHelper(dep, sg, tabuEdges); } } }
/** * Similar to the expandFromPatterns, but performs an exhaustive * search, performing simplifications on the graphs until exhausted. * * TODO: ensure cycles do not occur * NOTE: put in an arbitrary depth limit of 3, to prevent churning way too much (heuristic) * */ public Collection<SemanticGraph> exhaustFromPatterns(List<SsurgeonPattern> patternList, SemanticGraph sg) throws Exception { Collection<SemanticGraph> generated = exhaustFromPatterns(patternList, sg, 1); if (generated.size() > 1) { if (log != null) log.info("Before remove dupe, size="+generated.size()); generated = SemanticGraphUtils.removeDuplicates(generated, sg); if (log != null) log.info("AFTER remove dupe, size="+generated.size()); } return generated; } private List<SemanticGraph> exhaustFromPatterns(List<SsurgeonPattern> patternList, SemanticGraph sg, int depth) throws Exception {
public static String printVertices(SemanticGraph sg) { return printVertices(sg, new PrintVerticeParams()); }
/** * GIven a graph, returns a new graph with the the new sentence index enforced. * NOTE: new vertices are inserted. * TODO: is this ok? rewrite this? */ public static SemanticGraph setSentIndex(SemanticGraph sg, int newSentIndex) { SemanticGraph newGraph = new SemanticGraph(sg); List<IndexedWord> prevRoots = new ArrayList<>(newGraph.getRoots()); List<IndexedWord> newRoots = new ArrayList<>(); // TODO: we are using vertexListSorted here because we're changing // vertices while iterating. Perhaps there is a better way to do it. for (IndexedWord node : newGraph.vertexListSorted()) { IndexedWord newWord = new IndexedWord(node); newWord.setSentIndex(newSentIndex); SemanticGraphUtils.replaceNode(newWord, node, newGraph); if (prevRoots.contains(node)) newRoots.add(newWord); } newGraph.setRoots(newRoots); return newGraph; }
public String printSemanticGraph(SemanticGraph sg, boolean unescapeParenthesis) { boolean isTree = SemanticGraphUtils.isTree(sg);
} else if (useWordAsLabel) { buf.append("="); buf.append(sanitizeForSemgrexName(vertice.word())); buf.append(" "); buf.append(semgrexFromGraphHelper(tgtVert, sg, tabu, seenEdges, useWordAsLabel, nameEdges, wildcardNodes, nodeNameMap, orderedNodes, nodeValuesTransformation)); if (applyParens)
public static String semgrexFromGraph(SemanticGraph sg, Collection<IndexedWord> wildcardNodes, boolean useTag, boolean useWord, Map<IndexedWord, String> nodeNameMap) throws Exception { Function<IndexedWord, String> transformNode = o ->{ String str = ""; if(useWord) str = "{word: /" + Pattern.quote(o.word()) + "/"; if(useTag){ if(!str.isEmpty()) str += "; "; str = "tag: " + o.tag(); } if(!str.isEmpty()) str += "}"; return str; }; return semgrexFromGraph(sg, wildcardNodes, nodeNameMap, transformNode); }
/** * Used to assign generic labels to the nodes in the "blanket" for a set of vertices in a graph. Here, a "blanket" node is * similar to nodes in a Markov Blanket, i.e. nodes that are one edge away from a set of asserted vertices in a * SemanticGraph. */ public static Map<IndexedWord, IndexedWord> makeBlanketVertices(Iterable<IndexedWord> verts) { return anonymyizeNodes(verts, BLANKET_NODE_ANON_PREFIX); }