Saving one lookup

This commit is contained in:
Yomguithereal 2018-05-30 08:57:55 +02:00
parent 775d02a6a4
commit c1b5e732c1
2 changed files with 9 additions and 3 deletions

View File

@ -4,7 +4,7 @@ SOURCE = fog
# Commands
all: lint test
test: unit
publish: upload clean
publish: lint test upload clean
clean:
rm -rf *.egg-info .pytest_cache build dist

View File

@ -6,6 +6,10 @@
#
import math
# TODO: test one of the two counters is empty
# TODO: test both are empty
# TODO: test 1 dimension (identical + different)
def sparse_cosine_similarity(A, B, key=lambda x: x):
"""
@ -35,8 +39,10 @@ def sparse_cosine_similarity(A, B, key=lambda x: x):
weight = key(v)
xx += weight ** 2
if k in B:
xy += weight * key(B[k])
v2 = B.get(k)
if v2 is not None:
xy += weight * key(v2)
for v in B.values():
weight = key(v)