Skip to content

Commit 1b87ff9

Browse files
committed
spaCy Fixes
1 parent fa91225 commit 1b87ff9

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

machine_learning/cosine_similarity.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import logging
2+
23
import numpy as np
34
import spacy
45

6+
"""
7+
Cosine Similarity Algorithm
58
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.
916
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+
"""
1423

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-
"""
1924

25+
class CosineSimilarity:
2026
def __init__(self) -> None:
2127
"""
22-
Initializes the Cosine Similarity class by loading the SpaCy model.
28+
Initializes the Cosine Similarity class by loading the SpaCy Model.
2329
"""
2430
self.nlp = spacy.load("en_core_web_md")
2531

@@ -132,7 +138,7 @@ def cosine_similarity(self, vector1: np.ndarray, vector2: np.ndarray) -> float:
132138
"""
133139
try:
134140
dot = self.dot_product(vector1, vector2)
135-
magnitude1, magnitude2 = self.magnitude(vector1), self.magnitude(vector2)
141+
magnitude1, magnitude2 = (self.magnitude(vector1), self.magnitude(vector2))
136142
if magnitude1 == 0 or magnitude2 == 0:
137143
return 0.0
138144
return dot / (magnitude1 * magnitude2)

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ requests
1515
rich
1616
# scikit-fuzzy # uncomment once fuzzy_logic/fuzzy_operations.py is fixed
1717
scikit-learn
18+
spacy
1819
statsmodels
1920
sympy
2021
tensorflow

0 commit comments

Comments
 (0)