python定义词典

编程入门 行业动态 更新时间:2024-10-21 05:45:47

python定义<a href=https://www.elefans.com/category/jswz/34/1761996.html style=词典"/>

python定义词典

原标题:pyhanlp用户自定义词典添加

pyhanlp是python版封装的的HanLP,项目地址:

经过测试,HanLP比nltk在中文分词和实体识别方面都更好用。

如何向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,重新加载缓存文件而已。

备注:

HanLP词性列表:

文章来源于明月三千里68的博客返回搜狐,查看更多

责任编辑:

更多推荐

python定义词典

本文发布于:2024-03-07 02:56:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1716652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词典   定义   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!