Looking for nlp Answers? Try Ask4KnowledgeBase
Looking for nlp Keywords? Try Ask4Keywords

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