首页 > 编程 > Python > 正文

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

2019-11-08 01:39:45
字体:
来源:转载
供稿:网友

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

import pynlpirimport codecsimport math,MySQLdbfrom 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'
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表