欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用gensim計算文檔相似性

 更新時間:2016年04月10日 09:05:44   作者:junliKONG  
在文本處理中,比如商品評論挖掘,有時需要了解每個評論分別和商品的描述之間的相似度,以此衡量評論的客觀性。那么python 里面有計算文本相似度的程序包嗎,恭喜你,不僅有,而且很好很強大。下面我們就來體驗下gensim的強大

pre_file.py

#-*-coding:utf-8-*-
import MySQLdb
import MySQLdb as mdb
import os,sys,string
import jieba
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
#連接數(shù)據(jù)庫
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對象操作數(shù)據(jù)庫
cursor=conn.cursor(mdb.cursors.DictCursor) #cursor游標
#獲取內(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ù)時必須

jiansuo.py

#-*-coding:utf-8-*-
import sys
import string
import MySQLdb
import MySQLdb as mdb
import gensim
from gensim import corpora,models,similarities
from gensim.similarities import MatrixSimilarity
import logging
import codecs
reload(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)頁文檔轉(zhuǎn)化為tf-idf
dictionary=corpora.Dictionary(Corp)
corpus=[dictionary.doc2bow(text) for text in Corp] #將文檔轉(zhuǎn)化為詞袋模型
#print corpus
tfidf=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(' '))#將請求轉(zhuǎn)化為詞帶模型
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/爬蟲練習(xí)代碼.iml" filepath="$PROJECT_DIR$/.idea/爬蟲練習(xí)代碼.iml" />
  </modules>
 </component>
</project>

相關(guān)文章

  • python3跳出一個循環(huán)的實例操作

    python3跳出一個循環(huán)的實例操作

    在本篇內(nèi)容里小編給大家整理的是關(guān)于python3跳出一個循環(huán)的實例操作內(nèi)容,有需要的朋友們可以參考下。
    2020-08-08
  • python運行加速的幾種方式

    python運行加速的幾種方式

    Python運行的慢是歷來被詬病的,本文就來介紹一下python運行加速的幾種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Python函數(shù)參數(shù)定義及傳遞方式解析

    Python函數(shù)參數(shù)定義及傳遞方式解析

    這篇文章主要介紹了Python函數(shù)參數(shù)定義及傳遞方式解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • python解決報錯ImportError: Bad git executable.問題

    python解決報錯ImportError: Bad git executable.問題

    這篇文章主要介紹了python解決報錯ImportError: Bad git executable.問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 如何Tkinter模塊編寫Python圖形界面

    如何Tkinter模塊編寫Python圖形界面

    本文講解為何使用Tkinter而非PyQt,創(chuàng)建一個基本的Tkinter程序,模塊化Tkinter程序,希望對大家有幫助。
    2020-10-10
  • 詳解python 利用echarts畫地圖(熱力圖)(世界地圖,省市地圖,區(qū)縣地圖)

    詳解python 利用echarts畫地圖(熱力圖)(世界地圖,省市地圖,區(qū)縣地圖)

    這篇文章主要介紹了詳解python 利用echarts畫地圖(熱力圖)(世界地圖,省市地圖,區(qū)縣地圖),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python中單下劃線_的常見用法總結(jié)

    python中單下劃線_的常見用法總結(jié)

    這篇文章主要介紹了python中單下劃線_的常見用法總結(jié),其實很多(不是所有)關(guān)于下劃線的使用都是一些約定俗成的慣例,而不是真正對python解釋器有影響,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-07-07
  • python生成tensorflow輸入輸出的圖像格式的方法

    python生成tensorflow輸入輸出的圖像格式的方法

    本篇文章主要介紹了python生成tensorflow輸入輸出的圖像格式的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • python找出列表中大于某個閾值的數(shù)據(jù)段示例

    python找出列表中大于某個閾值的數(shù)據(jù)段示例

    今天小編就為大家分享一篇python找出列表中大于某個閾值的數(shù)據(jù)段示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python入門教程(二十六)Python的模塊

    Python入門教程(二十六)Python的模塊

    這篇文章主要介紹了Python入門教程(二十六)Python的模塊,Python是一門非常強大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下
    2023-04-04

最新評論