国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python使用gensim計(jì)算文檔相似性

2019-11-25 16:51:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

pre_file.py

#-*-coding:utf-8-*-import MySQLdbimport MySQLdb as mdbimport os,sys,stringimport jiebaimport codecsreload(sys)sys.setdefaultencoding('utf-8')#連接數(shù)據(jù)庫(kù)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對(duì)象操作數(shù)據(jù)庫(kù)cursor=conn.cursor(mdb.cursors.DictCursor) #cursor游標(biāo)#獲取內(nèi)容sql='SELECT link,content FROM test1.spider;'cursor.execute(sql)   #execute()方法,將字符串當(dāng)命令執(zhí)行data=cursor.fetchall()#fetchall()接收全部返回結(jié)果行f=codecs.open('C:/Users/kk/Desktop/hello-result1.txt','w','utf-8') for row in data:    #row接收結(jié)果行的每行數(shù)據(jù)  seg='/'.join(list(jieba.cut(row['content'],cut_all='False')))  f.write(row['link']+' '+seg+'/r/n')f.close() cursor.close()      #提交事務(wù),在插入數(shù)據(jù)時(shí)必須

jiansuo.py

#-*-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',user='root',passwd='kongjunli',db='test1',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()#將網(wǎng)頁(yè)文檔轉(zhuǎn)化為tf-idfdictionary=corpora.Dictionary(Corp)corpus=[dictionary.doc2bow(text) for text in Corp] #將文檔轉(zhuǎn)化為詞袋模型#print corpustfidf=models.TfidfModel(corpus)#使用tf-idf模型得出文檔的tf-idf模型corpus_tfidf=tfidf[corpus]#計(jì)算得出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(' '))#將請(qǐng)求轉(zhuǎn)化為詞帶模型vec_tfidf=tfidf[vec_bow]#計(jì)算出請(qǐng)求的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/爬蟲練習(xí)代碼.iml" filepath="$PROJECT_DIR$/.idea/爬蟲練習(xí)代碼.iml" />  </modules> </component></project>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汉中市| 秭归县| 来凤县| 岳阳县| 马公市| 调兵山市| 阿勒泰市| 常德市| 同德县| 泉州市| 永泰县| 嘉定区| 汉源县| 彝良县| 黔西| 扶风县| 宣化县| 肃南| 兴隆县| 溧水县| 政和县| 临夏县| 翼城县| 陵川县| 浦江县| 垦利县| 金阳县| 延安市| 江孜县| 治多县| 蒲江县| 庆阳市| 天峨县| 蒙阴县| 河北省| 东山县| 商都县| 社会| 迁西县| 洪泽县| 靖州|