【python】使用中科院NLPIR分词工具进行mysql数据分词

本文主要是使用中科院的分词工具对于数据库中的数据文本进行分词
在电脑上安装python,并导入python与数据库的连接插件MySQLdb 以及中科院的分词工具NLPIR

import pynlpir
import codecs
import math,MySQLdb
from search import *
pynlpir.open()
#连接数据库
conn=MySQLdb.connect(host="127.0.0.1",user="root",passwd="123456",db="",charset="utf8") 
cursor = conn.cursor() 
n = cursor.execute("select * from test where  id = 8 ")

停用词
st = codecs.open('E:\\testword\\stopwords.txt', 'rb',encoding='gbk')
读取数据库中的数据

for row in cursor.fetchall():  
    s=row[3]
    singletext_result = []
    #item中第一列存储的是关键词,第二列是词性
    print row[0]
    for item in pynlpir.segment(s):
        #print item[0]
        singletext_result.append(item[0])
    #print singletext_result
    #读取停用词
    for line in st:
        line = line.strip()
        stopwords.append(line)
    print stopwords

过滤停用词

#过滤停用词
    localtion = 0
    for word in singletext_result:
        localtion = localtion + 1
        if word not in stopwords:
            if word >= u'\u4e00' and word <= u'\u9fa5':#判断是否是汉字
                 delstopwords_singletxt.append(word)

构建词表

#构建词表
    for item in delstopwords_singletxt:
        if(search(item)):
            if(savecount(item)):
                print 'success to add count'
        else:
            if(save(item)):
                print 'success to add keyword'
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值