我已经尝试了以下代码,但是代码不起作用,并且只输出null。
String text = "我爱北京天安门。";
StanfordCoreNLP pipeline = new StanfordCoreNLP();
Annotation annotation = pipeline.process(text);
String result = annotation.get(CoreAnnotations.ChineseSegAnnotation.class);
System.out.println(result);结果是:
...
done [0.6 sec].
Using mention detector type: rule
null如何正确使用StanfordNLP中文分词工具?
发布于 2016-07-19 20:04:22
一些示例代码:
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.util.StringUtils;
import java.util.*;
public class ChineseSegmenter {
public static void main (String[] args) {
// set the properties to the standard Chinese pipeline properties
Properties props = StringUtils.argsToProperties("-props", "StanfordCoreNLP-chinese.properties");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "...";
Annotation annotation = new Annotation(text);
pipeline.annotate(annotation);
List<CoreLabel> tokens = annotation.get(CoreAnnotations.TokensAnnotation.class);
for (CoreLabel token : tokens)
System.out.println(token);
}
}注意:确保中国模型jar在您的CLASSPATH上。该文件可从以下位置获得:http://stanfordnlp.github.io/CoreNLP/download.html
上面的代码应该会打印出中文分词程序运行后创建的标记。
https://stackoverflow.com/questions/38392634
复制相似问题