斯坦福 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