Advertisement

Stanford NLP 安装与初步使用

阅读量:
1.如何使用CoreNLP工具

通过Maven来使用
后面两个dependency是导入model用的,支持的语言包括英语、汉语、法语、西班牙语和德语。默认情况下CoreNLP是支持英语的,其他语言的model需要独立下载。

复制代码
 <dependency>

    
     <groupId>edu.stanford.nlp</groupId>
    
     <artifactId>stanford-corenlp</artifactId>
    
     <version>3.6.0</version>
    
 </dependency>
    
 <dependency>
    
     <groupId>edu.stanford.nlp</groupId>
    
     <artifactId>stanford-corenlp</artifactId>
    
     <version>3.6.0</version>
    
     <classifier>models</classifier>
    
 </dependency>
    
  
    
 <!-- 解析中文需要下面这个包 -->
    
  <dependency> 
    
     <groupId>edu.stanford.nlp</groupId> 
    
     <artifactId>stanford-corenlp</artifactId> 
    
     <version>3.6.0</version> 
    
     <classifier>models-chinese</classifier>
    
 </dependency>
    
  
    
  
    
 <!-- 使用 Simple CoreNLP API 需要而外下面两个包 -->
    
 <dependency>
    
     <groupId>org.slf4j</groupId>
    
     <artifactId>slf4j-nop</artifactId>
    
     <version>1.7.12</version>
    
 </dependency>
    
 <dependency>
    
     <groupId>com.google.protobuf</groupId>
    
     <artifactId>protobuf-java</artifactId>
    
     <version>3.1.0</version>
    
 </dependency>
2.简单上手CoreNLP

在代码中使用Simple CoreNLP API
顾名思义,Simple CoreNLP API是相对于Stanford CoreNLP API比较简单的API操作方式。

复制代码
 import edu.stanford.nlp.simple.*;

    
  
    
 public class SimpleCoreNLPDemo {
    
     public static void main(String[] args) {
    
     // Create a document. No computation is done yet.
    
     Document doc = new Document("add your text here! It can contain multiple sentences.");
    
     for (Sentence sent : doc.sentences()) {  // Will iterate over two sentences
    
         // We're only asking for words -- no need to load any models yet
    
         System.out.println("The second word of the sentence '" + sent + "' is " + sent.word(1));
    
         // When we ask for the lemma, it will load and run the part of speech tagger
    
         System.out.println("The third lemma of the sentence '" + sent + "' is " + sent.lemma(2));
    
         // When we ask for the parse, it will load and run the parser
    
         System.out.println("The parse of the sentence '" + sent + "' is " + sent.parse());
    
         // ...
    
     }
    
     }
    
 }

全部评论 (0)

还没有任何评论哟~