使用stanford nlp進行依存句法分析

本文主要研究下如何使用stanford nlp進行依存句法分析html

maven

<dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.9.1</version>
        </dependency>

LexicalizedParser

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]

關係說明

  • root 句子的開頭,一個虛擬的node
  • nsubj(nominal subject) 名詞主語
  • dobj(direct object) 直接賓語
  • ccomp(clausal complement) 從句補充

詞性說明

  • VV 動詞
  • NR 人名
  • NN 經常使用名詞

小結

本文利用stanford nlp的LexicalizedParser對中文句子進行了簡單的依存關係分析,更深刻的內容見下面的參考文檔。node

doc

相關文章
相關標籤/搜索