|
1 | 1 | import logging
|
| 2 | + |
2 | 3 | import numpy as np
|
3 | 4 | import spacy
|
4 | 5 |
|
| 6 | +""" |
| 7 | +Cosine Similarity Algorithm |
5 | 8 |
|
6 |
| -class CosineSimilarity: |
7 |
| - """ |
8 |
| - Cosine Similarity Algorithm |
| 9 | +Use Case: |
| 10 | +- The Cosine Similarity Algorithm measures the Cosine of the Angle between two |
| 11 | +Non-Zero Vectors in a Multi-Dimensional Space. |
| 12 | +- It is used to determine how similar two texts are based on their Vector |
| 13 | +representations. |
| 14 | +- The similarity score ranges from -1 (Completely Dissimilar) to 1 (Completely Similar), |
| 15 | +with 0 indicating no Similarity. |
9 | 16 |
|
10 |
| - Use Case: |
11 |
| - - The Cosine Similarity Algorithm measures the Cosine of the Angle between two Non-Zero Vectors in a Multi-Dimensional Space. |
12 |
| - - It is used to determine how similar two texts are based on their Vector representations. |
13 |
| - - The similarity score ranges from -1 (Completely Dissimilar) to 1 (Completely Similar), with 0 indicating no Similarity. |
| 17 | +Dependencies: |
| 18 | +- spacy: A Natural Language Processing library for Python, used here for Tokenization |
| 19 | +and Vectorization. |
| 20 | +- numpy: A Library for Numerical Operations in Python, used for Mathematical |
| 21 | +Computations. |
| 22 | +""" |
14 | 23 |
|
15 |
| - Dependencies: |
16 |
| - - spacy: A Natural Language Processing library for Python, used here for Tokenization and Vectorization. |
17 |
| - - numpy: A Library for Numerical Operations in Python, used for Mathematical Computations. |
18 |
| - """ |
19 | 24 |
|
| 25 | +class CosineSimilarity: |
20 | 26 | def __init__(self) -> None:
|
21 | 27 | """
|
22 |
| - Initializes the Cosine Similarity class by loading the SpaCy model. |
| 28 | + Initializes the Cosine Similarity class by loading the SpaCy Model. |
23 | 29 | """
|
24 | 30 | self.nlp = spacy.load("en_core_web_md")
|
25 | 31 |
|
@@ -132,7 +138,7 @@ def cosine_similarity(self, vector1: np.ndarray, vector2: np.ndarray) -> float:
|
132 | 138 | """
|
133 | 139 | try:
|
134 | 140 | dot = self.dot_product(vector1, vector2)
|
135 |
| - magnitude1, magnitude2 = self.magnitude(vector1), self.magnitude(vector2) |
| 141 | + magnitude1, magnitude2 = (self.magnitude(vector1), self.magnitude(vector2)) |
136 | 142 | if magnitude1 == 0 or magnitude2 == 0:
|
137 | 143 | return 0.0
|
138 | 144 | return dot / (magnitude1 * magnitude2)
|
|
0 commit comments