本文主要研究下如何使用stanford nlp進行依存句法分析html
<dependency> <groupId>edu.stanford.nlp</groupId> <artifactId>stanford-corenlp</artifactId> <version>3.9.1</version> </dependency>
Lexical是詞彙的意思,LexicalizedParser即詞彙的語法解析
@Test public void testLexicalizedParser() throws IOException { LexicalizedParser lp = LexicalizedParser.loadModel(this.getClass().getClassLoader().getResource("xinhuaFactoredSegmenting.ser.gz").getPath()); List<String> lines = Arrays.asList("小明喜歡吃香蕉"); lines.stream().forEach(sentence -> { Tree tree = lp.parse(sentence); ChineseGrammaticalStructure gs = new ChineseGrammaticalStructure(tree); Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed(); System.out.println("sentence:"+sentence); tdl.stream().forEach(typedDependency -> { System.out.println("Governor Word: [" + typedDependency.gov() + "] Relation: [" + typedDependency.reln().getLongName() + "] Dependent Word: [" + typedDependency.dep() + "]"); }); }); }
這裏加載了xinhuaFactoredSegmenting.ser.gz
輸出java
sentence:小明喜歡吃香蕉 Governor Word: [喜歡/VV] Relation: [nominal subject] Dependent Word: [小明/NR] Governor Word: [ROOT] Relation: [root] Dependent Word: [喜歡/VV] Governor Word: [喜歡/VV] Relation: [clausal complement] Dependent Word: [吃/VV] Governor Word: [吃/VV] Relation: [direct object] Dependent Word: [香蕉/NN]
nominal subject
) 名詞主語direct object
) 直接賓語clausal complement
) 從句補充本文利用stanford nlp的LexicalizedParser對中文句子進行了簡單的依存關係分析,更深刻的內容見下面的參考文檔。node