最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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對(duì)象操作數(shù)據(jù)庫
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 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]#計(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>

相關(guān)文章

  • python實(shí)現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法

    python實(shí)現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法

    這篇文章主要介紹了python實(shí)現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法,涉及Python中l(wèi)ist方法的使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python通過字典映射函數(shù)實(shí)現(xiàn)switch

    Python通過字典映射函數(shù)實(shí)現(xiàn)switch

    這篇文章主要介紹了Python通過字典映射函數(shù)實(shí)現(xiàn)switch,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Python使用Supervisor來管理進(jìn)程的方法

    Python使用Supervisor來管理進(jìn)程的方法

    這篇文章主要介紹了Python使用Supervisor來管理進(jìn)程的方法,涉及Supervisor的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • linux環(huán)境下python中MySQLdb模塊的安裝方法

    linux環(huán)境下python中MySQLdb模塊的安裝方法

    這篇文章主要給大家介紹了在linux環(huán)境下python中MySQLdb模塊的安裝方法,文中給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-06-06
  • Python GUI Tkinter簡單實(shí)現(xiàn)個(gè)性簽名設(shè)計(jì)

    Python GUI Tkinter簡單實(shí)現(xiàn)個(gè)性簽名設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了Python GUI Tkinter簡單實(shí)現(xiàn)個(gè)性簽名設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Python使用tkinter制作在線翻譯軟件

    Python使用tkinter制作在線翻譯軟件

    這篇文章主要為大家詳細(xì)介紹了Python使用tkinter制作在線翻譯軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • opencv讀取視頻并保存圖像的方法

    opencv讀取視頻并保存圖像的方法

    實(shí)習(xí)項(xiàng)目要做安全帽目標(biāo)檢測,拿到了公司給的一些視頻數(shù)據(jù),使用Opencv讀取視頻并每隔1s存儲(chǔ)一副圖像,本文就詳細(xì)的介紹一下使用,感興趣的可以了解一下
    2021-06-06
  • 春節(jié)到了 教你使用python來搶票回家

    春節(jié)到了 教你使用python來搶票回家

    這篇文章主要介紹了春節(jié)到了 教你使用python來搶票回家,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • python中引用與復(fù)制用法實(shí)例分析

    python中引用與復(fù)制用法實(shí)例分析

    這篇文章主要介紹了python中引用與復(fù)制用法,以實(shí)例形式詳細(xì)分析了python中引用與復(fù)制的功能與相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • python代碼編寫計(jì)算器小程序

    python代碼編寫計(jì)算器小程序

    這篇文章主要為大家詳細(xì)介紹了python代碼編寫計(jì)算器小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07

最新評(píng)論

重庆市| 宣化县| 侯马市| 麻栗坡县| 双鸭山市| 攀枝花市| 北辰区| 拉萨市| 中宁县| 梅州市| 肇州县| 云和县| 茌平县| 筠连县| 南部县| 信宜市| 安塞县| 徐闻县| 阜新市| 凤台县| 都昌县| 建湖县| 宁波市| 宽甸| 石嘴山市| 南召县| 磐安县| 丰镇市| 兴安盟| 临安市| 任丘市| 平舆县| 西昌市| 梅河口市| 甘德县| 科技| 屯门区| 黄陵县| 临江市| 开封县| 班戈县|