2
votes

I have a dictionary like this:

dict = {in : [0.01, -0.07, 0.09, -0.02], and : [0.2, 0.3, 0.5, 0.6], to : [0.87, 0.98, 0.54, 0.4]}

I want to calculate the cosine similarity between each word for which I have written a function that takes two vectors. First, it will take value for 'in' and 'and', then it should take value for 'in' and 'to' and so on.

I want it to store the result of this in another dictionary, where 'in' should be the key, and the values should be the ones returned after calculating cosine similarity. Similarly, I want dictionaries, for other words as well.

This is my function to calculate cosine similarity:

import math
def cosine_similarity(vec1,vec2):
    sum11, sum12, sum22 = 0, 0, 0
    for i in range(len(vec1)):
        x = vec1[i]; y = vec2[i]
        sum11 += x*x
        sum22 += y*y
        sum12 += x*y
    return sum12/math.sqrt(sum11*sum22)

vec1 and vec2 can be two lists like: [0.01, -0.07, 0.09, -0.02] and [0.2, 0.3, 0.5, 0.6], and it returns a result like: 0.14

How do I compute it in this way for each key and store the results in dictionaries in this way? :

{in : {and : 0.4321, to : 0.218}, and : {in : 0.1245, to : 0.9876}, to : { in : 0.8764, and : 0.123}}
2
Do you want to store the three results in the same dictionary, or different dictionaries? Also, does the order of the list [0.4321, 0.218] matter (can it be [0.218, 0.4321])? - Alex Riley
Are you sure about your calculations? The cosine sim of the given vectors should be 0.14. - axiom
@ajcr : I want to store the three result into the same dictionary and the order doesn't matter. - Martha Pears
@axiom : This is just an example and not the real result. - Martha Pears

2 Answers

1
votes
import math
inputDict = {"in" : [0.01, -0.07, 0.09, -0.02], "and" : [0.2, 0.3, 0.5, 0.6], "to" : [0.87, 0.98, 0.54, 0.4]}
def cosine_similarity(vec1,vec2):
    sum11, sum12, sum22 = 0, 0, 0
    for i in range(len(vec1)):
        x = vec1[i]; y = vec2[i]
        sum11 += x*x
        sum22 += y*y
        sum12 += x*y
    return sum12/math.sqrt(sum11*sum22)


result = {}
for key,value in inputDict.items():
    temp,tempDict= 0,{}
    for keyC,valueC in inputDict.items():
        if keyC == key:
            continue
        temp = cosine_similarity(value,valueC)
        tempDict[keyC] =temp
    result[key]= tempDict


print(result)

output:

{'in': {'and': 0.14007005254378826, 'to': -0.11279001655020567}, 'and': {'in': 0.14007005254378826, 'to': 0.7719749900051109}, 'to': {'in': -0.11279001655020567, 'and': 0.7719749900051109}}
0
votes

firstly you can compute a list from dictionary then this list you use for computing & after computing you can assign result into that dictionary, like as

import math
def cosine_similarity(vec1,vec2):
sum11, sum12, sum22 = 0, 0, 0
for i in range(len(vec1)):
    x = vec1[i]; y = vec2[i]
    sum11 += x*x
    sum22 += y*y
    sum12 += x*y
return sum12/math.sqrt(sum11*sum22)

dictio = {"in" : [0.01, -0.07, 0.09, -0.02], "and" : [0.2, 0.3, 0.5, 0.6], "to" : [0.87, 0.98,     0.54, 0.4]}

L = []
A = []
B = []

for i in dictio:
    L.append(dictio[i])

for i in range(len(L)):
    for j in range(len(L)):
        if ( i != j):
            B.append(cosine_similarity(L[i],L[j]))
    A.append(B)
    B=[]

c=0
for i in dictio:
    dictio[i]= A[c]
    c = c + 1