|
|
| import edu.stanford.nlp.parser.lexparser.TreebankLangParserParams; |
| import edu.stanford.nlp.parser.lexparser.EnglishTreebankParserParams; |
| import edu.stanford.nlp.semgraph.SemanticGraph; |
| import edu.stanford.nlp.semgraph.SemanticGraphFactory; |
| import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher; |
| import edu.stanford.nlp.semgraph.semgrex.SemgrexPattern; |
| import edu.stanford.nlp.trees.GrammaticalStructure; |
| import edu.stanford.nlp.trees.GrammaticalStructureFactory; |
| import edu.stanford.nlp.trees.Tree; |
|
|
| |
| |
| |
| |
| |
| |
| public class SemgrexDemo { |
| 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))))) (. .)))"; |
| |
| |
| Tree tree = Tree.valueOf(treeString); |
|
|
| |
| |
| |
| |
| SemanticGraph graph = SemanticGraphFactory.generateUncollapsedDependencies(tree); |
|
|
| |
| |
| TreebankLangParserParams params = new EnglishTreebankParserParams(); |
| GrammaticalStructureFactory gsf = params.treebankLanguagePack().grammaticalStructureFactory(params.treebankLanguagePack().punctuationWordRejectFilter(), params.typedDependencyHeadFinder()); |
|
|
| GrammaticalStructure gs = gsf.newGrammaticalStructure(tree); |
|
|
| System.err.println(graph); |
|
|
| SemgrexPattern semgrex = SemgrexPattern.compile("{}=A <<nsubj {}=B"); |
| SemgrexMatcher matcher = semgrex.matcher(graph); |
| |
| |
| while (matcher.find()) { |
| System.err.println(matcher.getNode("A") + " <<nsubj " + matcher.getNode("B")); |
| } |
| } |
| } |
|
|