HanLP Android 示例
23212 点击·0 回帖
![]() | ![]() | |
![]() | 图片:图片1.png ![]() portable版 portable版零配置,仅提供中文分词、简繁拼音、同义词等功能。只需在build.gradle中加入依赖: dependencies { compile 'com.hankcs:hanlp:portable-1.6.8' } 自定义版 HanLP的全部功能(分词、简繁、拼音、文本分类、句法分析)都兼容安卓,具体配置方法如下: 1、下载hanlp.jar放入app/libs。 2、下载data.zip解压到app/src/main/assets ,按需删除不需要的文件以减小apk体积。 3、在程序启动时(通常是MainApplication或MainActivity的onCreate方法)执行初始化代码: private void initHanLP() { try { Os.setenv("HANLP_ROOT", "", true); } catch (ErrnoException e) { throw new RuntimeException(e); } final AssetManager assetManager = getAssets(); HanLP.Config.IOAdapter = new IIOAdapter() { @Override public InputStream open(String path) throws IOException { return assetManager.open(path); } @Override public OutputStream create(String path) throws IOException { throw new IllegalAccessError("不支持写入" + path + "!请在编译前将需要的数据放入app/src/main/assets/data"); } }; } 之后就可以像普通java项目一样调用HanLP的全部功能了。 图片:图片2.png ![]() | |
![]() | ![]() |