美文网首页
Stanford CoreNLP

Stanford CoreNLP

作者: 兔牙君尼酱 | 来源:发表于2017-06-28 11:05 被阅读0次

    下载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
    以上:
    祝好!

    相关文章

      网友评论

          本文标题:Stanford CoreNLP

          本文链接:https://www.haomeiwen.com/subject/xjxucxtx.html