/** * Create a new triple with known values for the subject, relation, and object. * For example, "(cats, play with, yarn)" * * @param subject The subject of this triple; e.g., "cats". * @param relation The relation of this triple; e.g., "play with". * @param object The object of this triple; e.g., "yarn". * @param tree The tree this extraction was created from; we create a deep copy of the tree. */ public WithTree(List<CoreLabel> subject, List<CoreLabel> relation, List<CoreLabel> object, SemanticGraph tree, double confidence) { super(subject, relation, object, confidence); this.sourceTree = new SemanticGraph(tree); }
public SentenceFragment(SemanticGraph tree, boolean assumedTruth, boolean copy) { if (copy) { this.parseTree = new SemanticGraph(tree); } else { this.parseTree = tree; } this.assumedTruth = assumedTruth; words.addAll(this.parseTree.vertexListSorted().stream().map(IndexedWord::backingLabel).collect(Collectors.toList())); }
@Override protected void correctDependencies(List<TypedDependency> list) { SemanticGraph sg = new SemanticGraph(list); correctDependencies(sg); list.clear(); list.addAll(sg.typedDependencies()); Collections.sort(list); }
@Override protected void postProcessDependencies(List<TypedDependency> list) { SemanticGraph sg = new SemanticGraph(list); postProcessDependencies(sg); list.clear(); list.addAll(sg.typedDependencies()); }
/** * Parse a given constituency tree into a dependency graph. * * @param tree The constituency tree, in Penn Treebank style. * @return The dependency graph for the tree. */ private static SemanticGraph parse(Tree tree) { return new SemanticGraph(parser.newGrammaticalStructure(tree).typedDependenciesCollapsed()); }
/** * Given a collection of nodes from srcGraph, generates a new * SemanticGraph based off the subset represented by those nodes. * This uses the same vertices as in the original graph, which * allows for equality and comparisons between the two graphs. */ public static SemanticGraph makeGraphFromNodes(Collection<IndexedWord> nodes, SemanticGraph srcGraph) { if (nodes.size() == 1) { SemanticGraph retSg = new SemanticGraph(); for (IndexedWord node :nodes) retSg.addVertex(node); return retSg; } if (nodes.isEmpty()) { return null; } // TODO: if any nodes are not connected to edges in the original // graph, this will leave them out List<SemanticGraphEdge> edges = new ArrayList<>(); for (IndexedWord nodeG : nodes) { for (IndexedWord nodeD: nodes) { Collection<SemanticGraphEdge> existingEdges = srcGraph.getAllEdges(nodeG, nodeD); if (existingEdges != null) { edges.addAll(existingEdges); } } } return SemanticGraphFactory.makeFromEdges(edges); }
Optional<Function<Triple<ClauseSplitterSearchProblem.State,ClauseSplitterSearchProblem.Action,ClauseSplitterSearchProblem.State>,Counter<String>>> featurizer ) { this.tree = new SemanticGraph(tree); this.assumedTruth = assumedTruth; this.isClauseClassifier = isClauseClassifier;
/** * 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; }
@Override protected void postProcessDependencies(List<TypedDependency> list) { if (DEBUG) { printListSorted("At postProcessDependencies:", list); } SemanticGraph sg = new SemanticGraph(list); correctWHAttachment(sg); list.clear(); list.addAll(sg.typedDependencies()); if (DEBUG) { printListSorted("After correcting WH movement", list); } convertRel(list); if (DEBUG) { printListSorted("After converting rel:", list); } }
/** * Executes the Ssurgeon edit, but with the given Semgrex Pattern, instead of the one attached to this * pattern. * * NOTE: Predicate tests are still active here, and any named nodes required for evaluation must be * present. */ public Collection<SemanticGraph> execute(SemanticGraph sg, SemgrexPattern overridePattern) throws Exception { SemgrexMatcher matcher = overridePattern.matcher(sg); Collection<SemanticGraph> generated = new ArrayList<>(); while (matcher.find()) { if (predicateTest != null) { if (!predicateTest.test(matcher)) continue; } // We reset the named node map with each edit set, since these edits // should exist in a separate graph for each unique Semgrex match. nodeMap = Generics.newHashMap(); SemanticGraph tgt = new SemanticGraph(sg); for (SsurgeonEdit edit : editScript) { edit.evaluate(tgt, matcher); } generated.add(tgt); } return generated; }
@Override protected void getExtras(List<TypedDependency> list) { SemanticGraph sg = new SemanticGraph(list); addRef(sg); if (DEBUG) { printListSorted("After adding ref:", sg.typedDependencies()); } addExtraNSubj(sg); if (DEBUG) { printListSorted("After adding extra nsubj:", sg.typedDependencies()); } list.clear(); list.addAll(sg.typedDependencies()); }
/** * Mostly just an alias, but make sure our featurizer is serializable! */ public interface Featurizer extends Function<Triple<ClauseSplitterSearchProblem.State, ClauseSplitterSearchProblem.Action, ClauseSplitterSearchProblem.State>, Counter<String>>, Serializable { boolean isSimpleSplit(Counter<String> feats); }
SemanticGraph retSg = new SemanticGraph(); for (IndexedWord node : nodesToAdd) { retSg.addVertex(node);
SemanticGraph nsg = new SemanticGraph(); List<IndexedWord> vertices = sg.vertexListSorted(); int index = 1;
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; }
@Override public SemanticGraph parse() { sg = new SemanticGraph(); try { readWhiteSpace(); if (!isLeftBracket(peek())) return null; readDep(null, null); return sg; } catch (ParserException e) { log.info("SemanticGraphParser warning: " + e.getMessage()); return null; } }
SemanticGraph enhanced = new SemanticGraph(basic.typedDependencies());
/** * 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; }
/** * Given a list of graphs, constructs a new graph combined from the * collection of graphs. Original vertices are used, edges are * copied. Graphs are ordered by the sentence index and index of * the original vertices. Intent is to create a "mega graph" * similar to the graphs used in the RTE problem. * <br> * This method only works if the indexed words have different * sentence ids, as otherwise the maps used will confuse several of * the IndexedWords. */ public static SemanticGraph makeFromGraphs(Collection<SemanticGraph> sgList) { SemanticGraph sg = new SemanticGraph(); Collection<IndexedWord> newRoots = Generics.newHashSet(); for (SemanticGraph currSg : sgList) { newRoots.addAll(currSg.getRoots()); for (IndexedWord currVertex : currSg.vertexSet()) sg.addVertex(currVertex); for (SemanticGraphEdge currEdge : currSg.edgeIterable()) sg.addEdge(currEdge.getGovernor(), currEdge.getDependent(), currEdge.getRelation(), currEdge.getWeight(), currEdge.isExtra()); } sg.setRoots(newRoots); return sg; }
/** * This creates a new graph based off the given, but uses the existing nodes objects. */ public static SemanticGraph duplicateKeepNodes(SemanticGraph sg) { SemanticGraph retSg = new SemanticGraph(); for (IndexedWord node : sg.vertexSet()) { retSg.addVertex(node); } retSg.setRoots(sg.getRoots()); for (SemanticGraphEdge edge : sg.edgeIterable()) { retSg.addEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra()); } return retSg; }