Stanford CoreNLP使用

来源:互联网 发布:程序员需要读的书 编辑:程序博客网 时间:2024/05/16 18:19
package dong.aid;import java.util.List;import java.util.Properties;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.trees.Tree;import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation;import edu.stanford.nlp.util.CoreMap;public class Testing_StanfordNLP2 {public static void main(String[] args) {  Properties props = new Properties();  props.put("annotators", "tokenize,ssplit,pos,lemma,ner, parse");  /* tokenize     Tokenizes the text.   * ssplit       Splits a sequence of tokens into sentences.   * pos  Labels tokens with their POS tag.   * parse   *    * */  StanfordCoreNLP pipeline = new StanfordCoreNLP(props);  String txtWord = "Beijing sing Lenovo.";//"Stanford University is located in Stanford. It is one of best good universities";  Annotation document = new Annotation(txtWord);  pipeline.annotate(document);  // these are all the sentences in this document  //下面的sentences 中包含了所有分析结果,遍历即可获知结果。  List<CoreMap> sentences = document.get(SentencesAnnotation.class);  for(CoreMap sentence: sentences) {// 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);      String lema = token.get(LemmaAnnotation.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); //      logger.info(word+","+lema);//      originWord = lema;//      originFlag = true;      System.out.println(word+"\t"+pos+"\t"+lema+"\t"+ne);    } // this is the parse tree of the current sentence      Tree tree = sentence.get(TreeAnnotation.class);      System.out.println(tree.toString());      // this is the Stanford dependency graph of the current sentence      SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);      System.out.println(dependencies.toString());  }}}

0 0