Spark中使用HanLP分词

今日头条

1097人已加入

描述

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进行分词了。




文章来源于云聪的博客


打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 相关推荐

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分