nlpnlp入门


备注

本节概述了nlp是什么,以及开发人员可能想要使用它的原因。

它还应该提到nlp中的任何大型主题,并链接到相关主题。由于nlp的文档是新的,您可能需要创建这些相关主题的初始版本。

斯坦福CoreNLP

Stanford CoreNLP是一种流行的自然语言处理工具包,支持许多核心NLP任务。

要下载并安装该程序,请下载发行包并在类路径中包含必要的*.jar 文件,或者从Maven中心添加依赖项。有关详细信息,请参阅下载页面 。例如:

curl http://nlp.stanford.edu/software/stanford-corenlp-full-2015-12-09.zip -o corenlp.zip
unzip corenlp.zip
cd corenlp
export CLASSPATH="$CLASSPATH:`pwd`/*
 

运行CoreNLP工具有三种支持的方法:(1)使用基本完全可自定义的API ,(2)使用Simple CoreNLP API,或(3)使用CoreNLP服务器 。下面给出每个的简单使用示例。作为一个激励用例,这些例子将用于预测句子的句法分析。

  1. CoreNLP API

    public class CoreNLPDemo {
      public static void main(String[] args) {
    
        // 1. Set up a CoreNLP pipeline. This should be done once per type of annotation,
        //    as it's fairly slow to initialize.
        // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, parse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    
        // 2. Run the pipeline on some text.
        // read some text in the text variable
        String text = "the quick brown fox jumped over the lazy dog"; // Add your text here!
        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);
        // run all Annotators on this text
        pipeline.annotate(document);
    
        // 3. Read off the result
        // Get the list of sentences in the document
        List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
          // Get the parse tree for each sentence
          Tree parseTree = sentence.get(TreeAnnotations.TreeAnnotation.class);
          // Do something interesting with the parse tree!
          System.out.println(parseTree);
        }
    
      }
    }
    
  2. 简单的CoreNLP

    public class CoreNLPDemo {
      public static void main(String[] args) {
        String text = "The quick brown fox jumped over the lazy dog");  // your text here!
        Document document = new Document(text);  // implicitly runs tokenizer
        for (Sentence sentence : document.sentences()) {
          Tree parseTree = sentence.parse();  // implicitly runs parser
          // Do something with your parse tree!
          System.out.println(parseTree);
        }
      } 
    }
    
  3. CoreNLP服务器

    使用以下命令启动服务器(适当地设置类路径):

    java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer [port] [timeout]
     

    获取给定注释器集的JSON格式输出,并将其打印到标准输出:

     wget --post-data 'The quick brown fox jumped over the lazy dog.' 'localhost:9000/?properties={"annotators":"tokenize,ssplit,parse","outputFormat":"json"}' -O -
     

    为了从JSON获取我们的解析树,我们可以将JSON导航到sentences[i].parse