Spaces:
Runtime error
Runtime error
File size: 781 Bytes
fe6855c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
vectorizer = TfidfVectorizer()
email_cache = []
def check_duplicate(email_text: str):
global email_cache
email_cache.append(email_text)
if len(email_cache) > 1: #check if there is more than 1 email in cache.
tfidf_matrix = vectorizer.fit_transform(email_cache)
similarity_matrix = cosine_similarity(tfidf_matrix[-1:], tfidf_matrix[:-1])
if len(similarity_matrix[0]) > 0 and max(similarity_matrix[0]) > 0.9:
return True, f"Similar email found with similarity {max(similarity_matrix[0])}"
else:
return False, None
else: #if only 1 email, then it is not a duplicate.
return False, None |