private void setDependencyTree(Annotation anno) { List<CoreMap> sentences = anno.get(SentencesAnnotation.class); for(CoreMap sentence : sentences) { Tree tree = sentence.get(TreeAnnotation.class); if (tree==null) continue; SemanticGraph deps = null; SemanticGraph basicDeps = null; if (options.lang == Locale.CHINESE) { final boolean threadSafe = true; deps = SemanticGraphFactory.makeFromTree( new ChineseGrammaticalStructure(tree, Filters.acceptFilter(), chineseHeadFinder), SemanticGraphFactory.Mode.COLLAPSED, GrammaticalStructure.Extras.NONE, null); basicDeps = SemanticGraphFactory.makeFromTree( new ChineseGrammaticalStructure(tree, Filters.acceptFilter(), chineseHeadFinder), SemanticGraphFactory.Mode.BASIC, GrammaticalStructure.Extras.NONE, null); } else { deps = SemanticGraphFactory.generateEnhancedDependencies(tree); basicDeps = SemanticGraphFactory.generateUncollapsedDependencies(tree); } sentence.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, basicDeps); sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, deps); } }
private static SemanticGraph getParse(CoreMap sentence) { GrammaticalStructure gs = parser.predict(sentence); GrammaticalStructure.Extras maximal = GrammaticalStructure.Extras.MAXIMAL; // SemanticGraph deps = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.ENHANCED, maximal, true, null), // uncollapsedDeps = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.BASIC, maximal, true, null), // SemanticGraph ccDeps = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.ENHANCED_PLUS_PLUS, maximal, true, null); SemanticGraph ccDeps = SemanticGraphFactory.generateEnhancedPlusPlusDependencies(gs); return ccDeps; }
/** * 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); }
/** * Produces an Uncollapsed (basic) SemanticGraph. */ public static SemanticGraph generateUncollapsedDependencies(GrammaticalStructure gs) { return makeFromTree(gs, Mode.BASIC, GrammaticalStructure.Extras.NONE, null); }
SemanticGraph deps = SemanticGraphFactory.generateCollapsedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph uncollapsedDeps = SemanticGraphFactory.generateUncollapsedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph ccDeps = SemanticGraphFactory.generateCCProcessedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph enhancedDeps = SemanticGraphFactory.generateEnhancedDependencies(gsf.newGrammaticalStructure(tree)); SemanticGraph enhancedPlusPlusDeps = SemanticGraphFactory.generateEnhancedPlusPlusDependencies(gsf.newGrammaticalStructure(tree));
public Pair<CoreMap, GrammaticalStructure> parseForCoref(String sentence) { List<CoreLabel> tokens = stanfordTokenize(sentence); Tree tree = parser.parse(tokens); GrammaticalStructure gs = tagDependencies(tree, true); tagPOS(tokens); tagLemma(tokens); tagNamedEntity(tokens); CoreMap result = new ArrayCoreMap(); result.set(CoreAnnotations.TokensAnnotation.class, tokens); result.set(TreeCoreAnnotations.TreeAnnotation.class, tree); GrammaticalStructure.Extras extras = GrammaticalStructure.Extras.NONE; SemanticGraph deps = SemanticGraphFactory.generateCollapsedDependencies(gs, extras); SemanticGraph uncollapsedDeps = SemanticGraphFactory.generateUncollapsedDependencies(gs, extras); SemanticGraph ccDeps = SemanticGraphFactory.generateCCProcessedDependencies(gs, extras); result.set(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class, deps); result.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, uncollapsedDeps); result.set(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class, ccDeps); return new ImmutablePair<>(result, gs); }
SemanticGraph sg = SemanticGraphFactory.generateUncollapsedDependencies(t); System.out.println(sg.toString()); System.out.println(sg.toCompactString()); SemanticGraph g1 = SemanticGraphFactory.generateCollapsedDependencies(t); System.out.println("TEST SEMANTIC GRAPH - graph ----------------------------"); System.out.println(g1.toString());
/** * 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; }
public static void main(String[] args) { String treeString = "(ROOT (S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))"; // Typically the tree is constructed by parsing or reading a // treebank. This is just for example purposes Tree tree = Tree.valueOf(treeString); // This creates English uncollapsed dependencies as a // SemanticGraph. If you are creating many SemanticGraphs, you // should use a GrammaticalStructureFactory and use it to generate // the intermediate GrammaticalStructure instead SemanticGraph graph = SemanticGraphFactory.generateUncollapsedDependencies(tree); // Alternatively, this could have been the Chinese params or any // other language supported. As of 2014, only English and Chinese TreebankLangParserParams params = new EnglishTreebankParserParams(); GrammaticalStructureFactory gsf = params.treebankLanguagePack().grammaticalStructureFactory(params.treebankLanguagePack().punctuationWordRejectFilter(), params.typedDependencyHeadFinder()); GrammaticalStructure gs = gsf.newGrammaticalStructure(tree); log.info(graph); SemgrexPattern semgrex = SemgrexPattern.compile("{}=A <<nsubj {}=B"); SemgrexMatcher matcher = semgrex.matcher(graph); // This will produce two results on the given tree: "likes" is an // ancestor of both "dog" and "my" via the nsubj relation while (matcher.find()) { log.info(matcher.getNode("A") + " <<nsubj " + matcher.getNode("B")); } }
graph = SemanticGraphFactory.generateCollapsedDependencies(tree); } catch(Exception e){ System.err.println("WARNING: failed to generate dependencies from tree " + tree.toString());
/** * Converts basic UD tree to enhanced UD graph. */ private static SemanticGraph convertBasicToEnhanced(SemanticGraph sg) { GrammaticalStructure gs = semanticGraphToGrammaticalStructure(sg); return SemanticGraphFactory.generateEnhancedDependencies(gs); }
/** * * Produces an enhanced++ dependencies SemanticGraph. */ public static SemanticGraph generateEnhancedPlusPlusDependencies(Tree tree){ return makeFromTree(tree, Mode.ENHANCED_PLUS_PLUS, GrammaticalStructure.Extras.NONE); }
SemanticGraph deps = SemanticGraphFactory.generateCollapsedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph uncollapsedDeps = SemanticGraphFactory.generateUncollapsedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph ccDeps = SemanticGraphFactory.generateCCProcessedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph enhancedDeps = SemanticGraphFactory.generateEnhancedDependencies(gsf.newGrammaticalStructure(tree)); SemanticGraph enhancedPlusPlusDeps = SemanticGraphFactory.generateEnhancedPlusPlusDependencies(gsf.newGrammaticalStructure(tree));
SemanticGraph deps = SemanticGraphFactory.generateCollapsedDependencies(gsf.newGrammaticalStructure(tree), docID, index); SemanticGraph uncollapsedDeps = SemanticGraphFactory.generateUncollapsedDependencies(gsf.newGrammaticalStructure(tree), docID, index); SemanticGraph ccDeps = SemanticGraphFactory.generateCCProcessedDependencies(gsf.newGrammaticalStructure(tree), docID, index); if (verbose) { System.err.println("SDs:");
/** * 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; }
/** * Converts basic UD tree to enhanced UD graph. */ private static SemanticGraph convertBasicToEnhanced(SemanticGraph sg) { GrammaticalStructure gs = semanticGraphToGrammaticalStructure(sg); return SemanticGraphFactory.generateEnhancedDependencies(gs); }
/** * @see #makeFromTree(Tree, Mode, GrammaticalStructure.Extras, Predicate */ public static SemanticGraph makeFromTree(Tree tree, Mode mode, GrammaticalStructure.Extras includeExtras, Predicate<TypedDependency> filter) { return makeFromTree(tree, mode, includeExtras, filter, false); }
SemanticGraph deps = SemanticGraphFactory.generateCollapsedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph uncollapsedDeps = SemanticGraphFactory.generateUncollapsedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph ccDeps = SemanticGraphFactory.generateCCProcessedDependencies(gsf.newGrammaticalStructure(tree), extras); SemanticGraph enhancedDeps = SemanticGraphFactory.generateEnhancedDependencies(gsf.newGrammaticalStructure(tree)); SemanticGraph enhancedPlusPlusDeps = SemanticGraphFactory.generateEnhancedPlusPlusDependencies(gsf.newGrammaticalStructure(tree));
private void setDependencyTree(Annotation anno) { List<CoreMap> sentences = anno.get(SentencesAnnotation.class); for(CoreMap sentence : sentences) { Tree tree = sentence.get(TreeAnnotation.class); if (tree==null) continue; SemanticGraph deps = null; SemanticGraph basicDeps = null; if (options.lang == Locale.CHINESE) { final boolean threadSafe = true; deps = SemanticGraphFactory.makeFromTree( new ChineseGrammaticalStructure(tree, Filters.acceptFilter(), chineseHeadFinder), SemanticGraphFactory.Mode.COLLAPSED, GrammaticalStructure.Extras.NONE, null); basicDeps = SemanticGraphFactory.makeFromTree( new ChineseGrammaticalStructure(tree, Filters.acceptFilter(), chineseHeadFinder), SemanticGraphFactory.Mode.BASIC, GrammaticalStructure.Extras.NONE, null); } else { deps = SemanticGraphFactory.generateEnhancedDependencies(tree); basicDeps = SemanticGraphFactory.generateUncollapsedDependencies(tree); } sentence.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, basicDeps); sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, deps); } }
/** * 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; }