分享

Spark中使用HanLP分词

 lanlantian123 2018-11-21
1.将HanLP的data(包含词典和模型)放到hdfs上,然后在项目配置文件hanlp.properties中配置root的路径,比如: 
root=hdfs://localhost:9000/tmp/

2.实现com.hankcs.hanlp.corpus.io.IIOAdapter接口:

    public static class HadoopFileIoAdapter implements IIOAdapter {

        @Override
        public InputStream open(String path) throws IOException {
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(path), conf);
            return fs.open(new Path(path));
        }

        @Override
        public OutputStream create(String path) throws IOException {
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(path), conf);
            OutputStream out = fs.create(new Path(path));
            return out;
        }
    }

3.设置IoAdapter,创建分词器:

private static Segment segment;

static {
    HanLP.Config.IOAdapter = new HadoopFileIoAdapter();
    segment = new CRFSegment();
}

然后,就可以在Spark的操作中使用segment进行分词了。




文章来源于云聪的博客

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约