Cosine calculator

Report a typo

Write a program to calculate the Cosine similarity between two sentences using their corresponding bag-of-words representations provided below. Input should contain two Python lists, each of which consists of numbers. The output should be a number rounded with 4 decimals.

You can use both NumPy and SciPy libraries. Note that SciPy offers Cosine distance calculation, you need to subtract this number from 1 to it.



Example of calculation:

doc_1 = "Cosine similarity is a semantic similarity metric."
doc_2 = "Cosine similarity is not a lexical similarity metric"

Their corresponding bag-of-words representations as input:

doc_1 = [1, 1, 0, 1, 0, 1, 2]
doc_2 = [1, 1, 1, 1, 1, 0, 2]

The cosine similarity between them is 0.825.

Sample Input 1:

3 45 7 2 5 6 7
2 54 13 15 8 9 10

Sample Output 1:

0.9731
Write a program in Python 3
def cosine_similarity(doc_1: list, doc_2: list) -> float:
...
return similarity

doc_1 = [int(i) for i in input().split()]
doc_2 = [int(i) for i in input().split()]
print(cosine_similarity(doc_1, doc_2))
___

Create a free account to access the full topic