Stanford CoreNLP

下載Stanford CoreNLP的壓縮包,地址:https://stanfordnlp.github.io/CoreNLP/api.html
新建java項目,引入壓縮包里的jar包(project右鍵——>build Path——>Configure BuildPath——>libraries——>add external jars)選中壓縮包文件夾里的jar文件,引入即可。

新建class文件:

package com.ww.corenlp;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation;
import edu.stanford.nlp.util.CoreMap;

public class TestNLP {
    public static void main(String[] args) {
        // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
        Properties props = new Properties();
        props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        // read some text in the text variable
        String text = "Mrs. Clinton previously worked for Mr. Obama, but she is  now distancing herself from him";
        
        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);
        
        // run all Annotators on this text
        pipeline.annotate(document);
        
        // these are all the sentences in this document
        // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
        List<CoreMap> sentences = document.get(SentencesAnnotation.class);
        
        System.out.println("word\t  pos\t  lemma\t  ner");
        for(CoreMap sentence: sentences) {
             // traversing the words in the current sentence
             // a CoreLabel is a CoreMap with additional token-specific methods
            for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
                // this is the text of the token
                String word = token.get(TextAnnotation.class);
                // this is the POS tag of the token
                String pos = token.get(PartOfSpeechAnnotation.class);
                // this is the NER label of the token
                String ne = token.get(NamedEntityTagAnnotation.class);
                String lemma = token.get(LemmaAnnotation.class);
               
                System.out.println(word+"\t"+pos+"\t"+lemma+"\t"+ne);
            }
            // this is the parse tree of the current sentence
            // 句子的解析樹  
            Tree tree = sentence.get(TreeAnnotation.class);
            System.out.println("\nparse tree:");
            tree.pennPrint();  
            // this is the Stanford dependency graph of the current sentence
            // 句子的依賴圖  
            SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
            System.out.println("\ndependencies:");
            System.out.println(dependencies.toString(SemanticGraph.OutputFormat.LIST));  
        }
        // This is the coreference link graph
        // Each chain stores a set of mentions that link to each other,
        // along with a method for getting the most representative mention
        // Both sentence and token offsets start at 1!
        Map<Integer, CorefChain> graph = document.get(CorefChainAnnotation.class);
    }
}

針對Advances in natural language processing論文中提到的句子做處理:

Mrs. Clinton previously worked for Mr. Obama, but she is  now distancing herself from him

可以在官方的在線demo上嘗試:

http://nlp.stanford.edu:8080/parser/index.jsp

得到結果為:

word      pos     lemma   ner
Mrs.    NNP Mrs.    O
Clinton NNP Clinton PERSON
previously  RB  previously  DATE
worked  VBD work    O
for IN  for O
Mr. NNP Mr. O
Obama   NNP Obama   PERSON
,   ,   ,   O
but CC  but O
she PRP she O
is  VBZ be  O
now RB  now DATE
distancing  VBG distance    O
herself PRP herself O
from    IN  from    O
him PRP he  O

parse tree:
(ROOT
  (FRAG
    (S
      (S
        (NP (NNP Mrs.) (NNP Clinton))
        (ADVP (RB previously))
        (VP (VBD worked)
          (PP (IN for)
            (NP (NNP Mr.) (NNP Obama)))))
      (, ,)
      (CC but)
      (S
        (NP (PRP she))
        (VP (VBZ is)
          (ADVP (RB now))
          (VP (VBG distancing)
            (NP (PRP herself))
            (PP (IN from)
              (NP (PRP him)))))))))

dependencies:
root(ROOT-0, worked-4)
compound(Clinton-2, Mrs.-1)
nsubj(worked-4, Clinton-2)
advmod(worked-4, previously-3)
case(Obama-7, for-5)
compound(Obama-7, Mr.-6)
nmod:for(worked-4, Obama-7)
punct(worked-4, ,-8)
cc(worked-4, but-9)
nsubj(distancing-13, she-10)
aux(distancing-13, is-11)
advmod(distancing-13, now-12)
conj:but(worked-4, distancing-13)
dobj(distancing-13, herself-14)
case(him-16, from-15)
nmod:from(distancing-13, him-16)

同論文中效果對比:

Paste_Image.png

引用:REVIEW
Advances in natural language processing Julia Hirschberg 1 and Christopher D. Manning 2,3
以上:
祝好!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容