Skip to content

Commit 2fe680f

Browse files
committed
Fixed Model Dependency
1 parent 1b87ff9 commit 2fe680f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

machine_learning/cosine_similarity.py

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def __init__(self) -> None:
2929
"""
3030
self.nlp = spacy.load("en_core_web_md")
3131

32+
def model_download(self):
33+
"""
34+
Downloads the SpaCy Model if it has not been downloaded before.
35+
"""
36+
try:
37+
spacy.cli.download("en_core_web_md")
38+
except Exception as e:
39+
logging.error("An Error Occured: ", exc_info=e)
40+
raise e
41+
3242
def tokenize(self, text: str) -> list:
3343
"""
3444
Tokenizes the input text into a list of lowercased tokens.
@@ -160,6 +170,8 @@ def cosine_similarity_percentage(self, text1: str, text2: str) -> float:
160170
- float: The cosine similarity percentage between the two texts.
161171
"""
162172
try:
173+
self.model_download() # Comment if Installed
174+
163175
tokens1 = self.tokenize(text1)
164176
tokens2 = self.tokenize(text2)
165177

@@ -186,6 +198,7 @@ def cosine_similarity_percentage(self, text1: str, text2: str) -> float:
186198
text1 = "The biggest Infrastructure in the World is Burj Khalifa"
187199
text2 = "The name of the talllest Tower in the world is Burj Khalifa"
188200

201+
spacy.cli.download("en_core_web_md")
189202
similarity_percentage = CosineSimilarity().cosine_similarity_percentage(
190203
text1, text2
191204
)

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ requests
1515
rich
1616
# scikit-fuzzy # uncomment once fuzzy_logic/fuzzy_operations.py is fixed
1717
scikit-learn
18-
spacy
18+
spacy>=3.7.6
1919
statsmodels
2020
sympy
2121
tensorflow

0 commit comments

Comments
 (0)