導航:首頁 > 編程語言 > pythonsimhash模塊

pythonsimhash模塊

發布時間:2022-08-21 05:58:35

A. 新手python 使用pycharm run了下simhash 但是 怎麼都出不來結果

在界面上方點: file--open(打開你要運行的程序) 然後再點擊Run(Ctrl+Shift+F10)運行該程序即可.

B. python有simhash模塊嗎

在界面上方點: file--open(打開你要運行的程序) 然後再點擊Run(Ctrl+Shift+F10)運行該程序即可.

C. python中simhash包怎麼用

#!/usr/bin/python
#coding=utf-8
classsimhash:

#構造函數
def__init__(self,tokens='',hashbits=128):
self.hashbits=hashbits
self.hash=self.simhash(tokens);

#toString函數
def__str__(self):
returnstr(self.hash)

#生成simhash值
defsimhash(self,tokens):
v=[0]*self.hashbits
fortin[self._string_hash(x)forxintokens]:#t為token的普通hash值
foriinrange(self.hashbits):
bitmask=1<<i
ift&bitmask:
v[i]+=1#查看當前bit位是否為1,是的話將該位+1
else:
v[i]-=1#否則的話,該位-1
fingerprint=0
foriinrange(self.hashbits):
ifv[i]>=0:
fingerprint+=1<<i
returnfingerprint#整個文檔的fingerprint為最終各個位>=0的和

#求海明距離
defhamming_distance(self,other):
x=(self.hash^other.hash)&((1<<self.hashbits)-1)
tot=0;
whilex:
tot+=1
x&=x-1
returntot

#求相似度
defsimilarity(self,other):
a=float(self.hash)
b=float(other.hash)
ifa>b:returnb/a
else:returna/b

#針對source生成hash值(一個可變長度版本的Python的內置散列)
def_string_hash(self,source):
ifsource=="":
return0
else:
x=ord(source[0])<<7
m=1000003
mask=2**self.hashbits-1
forcinsource:
x=((x*m)^ord(c))&mask
x^=len(source)
ifx==-1:
x=-2
returnx
if__name__=='__main__':
s='Thisisateststringfortesting'
hash1=simhash(s.split())

s=''
hash2=simhash(s.split())

s='nainaigexiongcao'
hash3=simhash(s.split())

print(hash1.hamming_distance(hash2),"",hash1.similarity(hash2))
print(hash1.hamming_distance(hash3),"",hash1.similarity(hash3))

閱讀全文

與pythonsimhash模塊相關的資料

熱點內容
androideclipse32 瀏覽:125
商用壓縮機承受溫度 瀏覽:48
健身房壓縮衣 瀏覽:976
單片機太陽光追蹤系統所需材料 瀏覽:356
比澤爾壓縮機型號規則 瀏覽:85
華興數控切斷編程 瀏覽:789
西安離心壓縮機 瀏覽:545
程序員需要優盤嗎 瀏覽:879
西藏掌上社保app在哪裡下載 瀏覽:599
怎麼讓伺服器固定 瀏覽:65
計數器定時器編程 瀏覽:13
程序員網上投資平台 瀏覽:878
用shell編程計算1加到100 瀏覽:233
外包公司的程序員一天寫多少代碼 瀏覽:532
蘋果手機主屏幕app如何移動 瀏覽:567
伺服器怎麼連接遠程密碼 瀏覽:431
linux娛樂命令 瀏覽:368
單片機數碼管循環顯示9到0 瀏覽:494
程序員懟代碼思路 瀏覽:327
新能源碼磚機產品介紹 瀏覽:37