Stanford CoreNLP使用

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值