首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中使用StanfordNLP中文分词?

问如何在Java中使用StanfordNLP中文分词?
EN

Stack Overflow用户
提问于 2016-07-15 17:30:50
回答 1查看 295关注 0票数 0

我已经尝试了以下代码,但是代码不起作用,并且只输出null。

代码语言:javascript
复制
String text = "我爱北京天安门。";
StanfordCoreNLP pipeline = new StanfordCoreNLP();
Annotation annotation = pipeline.process(text);
String result = annotation.get(CoreAnnotations.ChineseSegAnnotation.class);
System.out.println(result);

结果是:

代码语言:javascript
复制
...
done [0.6 sec].
Using mention detector type: rule
null

如何正确使用StanfordNLP中文分词工具?

EN

回答 1

Stack Overflow用户

发布于 2016-07-19 20:04:22

一些示例代码:

代码语言:javascript
复制
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

上面的代码应该会打印出中文分词程序运行后创建的标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38392634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档