Python使用gensim计算文档相似性

Python使用gensim计算文档相似性,第1张

概述pre_file.py#-*-coding:utf-8-*-importMySQLdbimportMySQLdbasmdbimportos,sys,stringimportjiebaimportcodecs

pre_file.py

@H_403_3@#-*-Coding:utf-8-*-import MysqLdbimport MysqLdb as mdbimport os,sys,stringimport jIEbaimport codecsreload(sys)sys.setdefaultencoding('utf-8')#连接数据库try: conn=mdb.connect(host='127.0.0.1',user='root',passwd='kongjunli',db='test1',charset='utf8')except Exception,e: print e sys.exit()#获取cursor对象 *** 作数据库cursor=conn.cursor(mdb.cursors.DictCursor) #cursor游标#获取内容sql='SELECT link,content FROM test1.spIDer;'cursor.execute(sql) #execute()方法,将字符串当命令执行data=cursor.fetchall()#fetchall()接收全部返回结果行f=codecs.open('C:\Users\kk\Desktop\hello-result1.txt','w','utf-8') for row in data: #row接收结果行的每行数据 seg='/'.join(List(jIEba.cut(row['content'],cut_all='False'))) f.write(row['link']+' '+seg+'\r\n')f.close() cursor.close() #提交事务,在插入数据时必须

jiansuo.py

@H_403_3@#-*-Coding:utf-8-*-import sysimport stringimport MysqLdbimport MysqLdb as mdbimport gensimfrom gensim import corpora,models,similaritIEsfrom gensim.similaritIEs import MatrixSimilarityimport loggingimport codecsreload(sys)sys.setdefaultencoding('utf-8') con=mdb.connect(host='127.0.0.1',charset='utf8')with con: cur=con.cursor() cur.execute('SELECT * FROM cutresult_copy') rows=cur.fetchall() class MyCorpus(object): def __iter__(self): for row in rows: yIEld str(row[1]).split('/')#开启日志logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',level=logging.INFO)Corp=MyCorpus()#将网页文档转化为tf-IDfdictionary=corpora.Dictionary(Corp)corpus=[dictionary.doc2bow(text) for text in Corp] #将文档转化为词袋模型#print corpustfIDf=models.TfIDfModel(corpus)#使用tf-IDf模型得出文档的tf-IDf模型corpus_tfIDf=tfIDf[corpus]#计算得出tf-IDf值#for doc in corpus_tfIDf: #print doc###'''q_file=open('C:\Users\kk\Desktop\q.txt','r')query=q_file.readline()q_file.close()vec_bow=dictionary.doc2bow(query.split(' '))#将请求转化为词带模型vec_tfIDf=tfIDf[vec_bow]#计算出请求的tf-IDf值#for t in vec_tfIDf: # print t'''###query=raw_input('Enter your query:')vec_bow=dictionary.doc2bow(query.split())vec_tfIDf=tfIDf[vec_bow]index=similaritIEs.MatrixSimilarity(corpus_tfIDf)sims=index[vec_tfIDf]similarity=List(sims)print sorted(similarity,reverse=True)

enCodings.xml

<?xml version="1.0" enCoding="UTF-8"?><project version="4"> <component name="EnCoding">  <file url="PROJECT" charset="UTF-8" /> </component></project>

misc.xml

<?xml version="1.0" enCoding="UTF-8"?><project version="4"> <component name="ProjectLevelVcsManager" settingsEditedManually="false">  <OptionsSetting value="true" ID="Add" />  <OptionsSetting value="true" ID="Remove" />  <OptionsSetting value="true" ID="Checkout" />  <OptionsSetting value="true" ID="Update" />  <OptionsSetting value="true" ID="Status" />  <OptionsSetting value="true" ID="Edit" />  <ConfirmationsSetting value="0" ID="Add" />  <ConfirmationsSetting value="0" ID="Remove" /> </component> <component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.11 (C:\Python27\python.exe)" project-jdk-type="Python SDK" /></project>

modules.xml

<?xml version="1.0" enCoding="UTF-8"?><project version="4"> <component name="ProjectModuleManager">  <modules>   <module fileurl="file://$PROJECT_DIR$/.IDea/爬虫练习代码.iml" filepath="$PROJECT_DIR$/.IDea/爬虫练习代码.iml" />  </modules> </component></project>

总结

以上是内存溢出为你收集整理的Python使用gensim计算文档相似性全部内容,希望文章能够帮你解决Python使用gensim计算文档相似性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1204234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存