我不明白这个函数返回两个变量的意义,它们是相同的:
def construct_shingles(doc,k,h):
#print 'antes -> ',doc,len(doc)
doc = doc.lower()
doc = ''.join(doc.split(' '))
#print 'depois -> ',doc,len(doc)
shingles = {}
for i in xrange(len(doc)):
substr = ''.join(doc[i:i+k])
if len(substr) == k and substr not in shingles:
shingles[substr] = 1
if not h:
return doc,shingles.keys()
ret = tuple(shingles_hashed(shingles))
return ret,ret
似乎多余,但必须有充分的理由,我只是不明白为什么。也许是因为有两个返回语句?如果 'h' 为真,它是否返回两个返回语句?调用函数如下所示:
def construct_set_shingles(docs,k,h=False):
shingles = []
for i in xrange(len(docs)):
doc = docs[i]
doc,sh = construct_shingles(doc,k,h)
docs[i] = doc
shingles.append(sh)
return docs,shingles
和
def shingles_hashed(shingles):
global len_buckets
global hash_table
shingles_hashed = []
for substr in shingles:
key = hash(substr)
shingles_hashed.append(key)
hash_table[key].append(substr)
return shingles_hashed
数据集和函数调用如下所示:
k = 3 #number of shingles
d0 = "i know you"
d1 = "i think i met you"
d2 = "i did that"
d3 = "i did it"
d4 = "she says she knows you"
d5 = "know you personally"
d6 = "i think i know you"
d7 = "i know you personally"
docs = [d0,d1,d2,d3,d4,d5,d6,d7]
docsChange,shingles = construct_set_shingles(docs[:],k)
github位置:lsh/LHS