前叙
博主写这篇文章使用了八十分钟,阅读需要十五分钟,读完之后你将会学会在Python中使用NLPIR2016.如果你花费更多的时间练习后半部分的例子,你将能够在一天内学会在Python中使用NLPIR2016的全部所需知识 如果你想要获取更详细的API翻译,你需要进一步学习ctypes,附赠一篇关于API翻译,虽然是java语言的,但是NLPIR的接口翻译都是一样的 http://blog.csdn.net/fontthrone/article/details/72882938 我决定上传一下有关NLP的博客中的源码: http://blog.csdn.net/fontthrone/article/details/72885329
如何使用接口
将打包好的[组合包]下所有内容拖入你的项目,然后开始在你的py文件中通过引用nlpir中的方法使用NLPIR2016
from nlpir
import *
from ctypes
import *
import sys
reload(sys)
sys.setdefaultencoding(
'utf-8')
from os
import path
d = path.dirname(__file__)
text_path =
'txt/lztest.txt'
stopwords_path =
'stopwords\stopwords1893.txt'
text = open(path.join(d, text_path)).read()
txt = seg(text)
seg_list =[]
for t
in txt:
seg_list.append(t[
0].encode(
'utf-8'))
print seg_list
for j
in seg_list:
print j
用户自定义词库的两种方式
1. 修改NLPIR的词库
想要修改NLPIR的词库你可以阅读官方的文档,其方法为”汉语分词20140928\importuserdict\Readme.txt” 下面为其中内容:
采用附件的小工具,可以实现脱机导入用户词典;具体步骤如下:
1.与分词Data文件夹同级建立 bin目录,下面建立二级目录ICTCLAS2014
2.将附件的内容解压缩后放在ICTCLAS2014下面;
3.编辑bin/ICTCLAS2014下面的userdic
.txt,这里放置用户词典与标注;
4.执行bin/ICTCLAS2014的批处理文件。即可导入用户词典到Data目录下的field
.pdat field
.pos。
5.30万词条会划分更多的时间,可能需要
2小时左右。
2.在代码中动态引用与删除用户自定义词库
#下面代码中loadFun的参数c_int,c_uint等为ctype类型,在下文中我会有部分介绍,其足以满足一般的使用,但是想要深入学习,你需要自己学习该部分
# nlpir的源代码
ImportUserDict = loadFun(
'NLPIR_ImportUserDict',c_uint, [c_char_p])
# 从txt文件中导入用户词典
AddUserWord = loadFun(
'NLPIR_AddUserWord', c_int, [c_char_p])
# 添加用户自定义词语
SaveTheUsrDic = loadFun(
'NLPIR_SaveTheUsrDic', c_int, None)
# 将用户词典保存到硬盘
DelUsrWord = loadFun(
'NLPIR_DelUsrWord',c_int, [c_char_p])
# 删除用户的自定义词语
# 例子
AddUserWord(
'龙族')
AddUserWord(
'路明非')
AddUserWord(
'大和炮')
AddUserWord(
'竞技类')
DelUsrWord(
'竞技类')
# 注意在NLPIR的默认分词格式中会识别汉语名字,但是测试发现:即使在某个句子中,将一个名字,比如'路明非'分词成功了,但是在其他句子中并不一定能够正确分词,如果你想更好地使用分词功能,可以和下一个部分中的提取新词配合使用
提取新词与关键字
text_path =
'txt/lztest.txt' #设置要分析的文本路径
text = open(path.join(d, text_path)).read()
txt = seg(text)
kw_list =[]
seg_list =[]
# 获得新词,第二个参数控制新词的个数,排名按照TF-IDF(term frequency–inverse document frequency排序
# 该功能可以和AddUserWord()方法配合使用,以更好地获取分词效果
strs1 = GetNewWords(text,c_int(
10),[c_char_p, c_int, c_bool])
print strs1
# 获得新词(从txt文件中),第二个参数控制新词的个数,排名按照TF-IDF(term frequency–inverse document frequency排序
# strs10 = GetFileNewWords(text,c_int(10),[c_char_p, c_int, c_bool])
# print strs10
# WindowsError: exception: access violation reading 0x0000000000000000
# 获得关键词,第二个参数控制新词的个数,排名按照TF-IDF(term frequency–inverse document frequency排序
strs2= GetKeyWords(text,c_int(
10),[c_char_p, c_int, c_bool])
print strs2
演示效果如下: 其中第一部分(比如”富山雅史”)为词语,第二部分(n_new为新词)为词性,第三部分为权重(TF*IDF)
使用停用词的实例
from os
import path
from nlpir
import *
from scipy.misc
import imread
import matplotlib.pyplot
as plt
from wordcloud
import WordCloud, ImageColorGenerator
from ctypes
import *
import sys
reload(sys)
sys.setdefaultencoding(
'utf-8')
d = path.dirname(__file__)
AddUserWord(
'龙族')
AddUserWord(
'路明非')
AddUserWord(
'大和炮')
AddUserWord(
'竞技类')
text_path =
'txt/lztest.txt'
stopwords_path =
'stopwords\stopwords1893.txt'
text = open(path.join(d, text_path)).read()
txt = seg(text)
seg_list =[]
for t
in txt:
seg_list.append(t[
0].encode(
'utf-8'))
def NLPIRclearText(text):
mywordlist = []
liststr =
"/ ".join(seg_list)
f_stop = open(stopwords_path)
try:
f_stop_text = f_stop.read()
f_stop_text = unicode(f_stop_text,
'utf-8')
finally:
f_stop.close()
f_stop_seg_list = f_stop_text.split(
'\n')
for myword
in liststr.split(
'/'):
if not (myword.strip()
in f_stop_seg_list)
and len(myword.strip()) >
1:
mywordlist.append(myword)
return ''.join(mywordlist)
s = NLPIRclearText(seg_list)
print s
结果如下:
使用中的可能会遇见的一些问题
ctype的问题,想要更好地在Python中使用NLPIR2016,你需要了解该部分控制台输出utf-8编码格式中文显示乱码,这个在windows中比较常见,原因是控制台的默认中文编码为gbk或者或者其他格式进一步掌握文章中未介绍的部分方法,你可以直接浏览nlpir.py文件或者参考这篇文章:http://blog.csdn.net/fontthrone/article/details/72882938在Python3中使用NLPIR2016,这个你只需要参考官网的文档即可,这个我就不再做过介绍了