温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何向pyhanlp添加自定义的词典

发布时间:2021-12-17 17:44:31 来源:亿速云 阅读:360 作者:小新 栏目:大数据

小编给大家分享一下如何向pyhanlp添加自定义的词典,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

以python 2.7.9为例:

1.安装pyhanlp:pip install pyhanlp

2.在字典路径下添加自定义的词典:CustomDictionary主词典文本路径是data/dictionary/custom/CustomDictionary.txt,用户可以在此增加自己的词语(不推荐);也可以单独新建一个文本文件,通过配置文件;CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; 我的词典.txt;来追加词典(推荐)。

具体绝对路径可用hanlp --version获取:

#hanlp --version

jar  1.6.3: /usr/local/lib/python2.7/site-packages/pyhanlp/static/hanlp-1.6.3.jar

data 1.6.2: /usr/local/lib/python2.7/site-packages/pyhanlp/static/data

config    : /usr/local/lib/python2.7/site-packages/pyhanlp/static/hanlp.properties

#cat /usr/local/lib/python2.7/site-packages/pyhanlp/static/hanlp.properties | grep "CustomDictionaryPath"

3.建议在该路径下添加自己的词典文件例如 我的词典.txt,并将其加入上面的properties文件里的CustomDictionaryPath下面。

#cat 我的词典.txt  

codis集群 nz 1000

今日头条 nz 1000

第一列为词条,第二列为词性(默认为n),第三列为词频

4.然后需要删除缓存文件,这样python才会重新加载新增的文件:

#rm -f CustomDictionary.txt.bin

5.测试新增的词典:

python -c "from pyhanlp import *;print(HanLP.segment('codis集群,今日头条'))"

五月 16, 2018 4:43:14 下午 com.hankcs.hanlp.corpus.io.IOUtil readBytes

警告: 读取/usr/local/lib/python2.7/site-packages/pyhanlp/static/data/dictionary/custom/CustomDictionary.txt.bin时发生异常java.io.FileNotFoundException: /usr/local/lib/python2.7/site-packages/pyhanlp/static/data/dictionary/custom/CustomDictionary.txt.bin (没有那个文件或目录)

报这个错误没有关系,只是个warning,重新加载缓存文件而已。

以上是“如何向pyhanlp添加自定义的词典”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI