-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_similarity.py
159 lines (125 loc) · 5.98 KB
/
calculate_similarity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# # from sklearn.feature_extraction.text import TfidfVectorizer
# # from sentence_transformers import SentenceTransformer, util
# # from nltk.metrics.distance import edit_distance
# # from scipy.spatial.distance import euclidean
# # import spacy
# # from simhash import Simhash
# # IF CUDA UNAVAILABLE, USE THE BELOW CODE:
# # def calculate_tfidf_similarity(text1, text2):
# # tfidf_vectorizer = TfidfVectorizer()
# # tfidf_matrix = tfidf_vectorizer.fit_transform([text1, text2])
# # similarity_score = (tfidf_matrix * tfidf_matrix.T).toarray()[0, 1]
# # return similarity_score
# # def calculate_sentence_transformer_similarity(text1, text2):
# # model = SentenceTransformer('LaBSE')
# # embedding1 = model.encode([text1], convert_to_tensor=True)
# # embedding2 = model.encode([text2], convert_to_tensor=True)
# # similarity_score = util.pytorch_cos_sim(embedding1, embedding2)[0][0].item()
# # return similarity_score
# # def calculate_levenshtein_similarity(text1, text2):
# # levenshtein_score = 1 - edit_distance(text1, text2) / max(len(text1), len(text2))
# # return levenshtein_score
# # def calculate_bert_similarity(text1, text2):
# # model = SentenceTransformer('LaBSE')
# # embedding1 = model.encode([text1], convert_to_tensor=True)[0]
# # embedding2 = model.encode([text2], convert_to_tensor=True)[0]
# # similarity_score = 1 / (1 + euclidean(embedding1, embedding2))
# # return similarity_score
# # def calculate_spacy_similarity(text1, text2, model_name):
# # nlp = spacy.load(model_name)
# # doc1 = nlp(text1)
# # doc2 = nlp(text2)
# # similarity_score = doc1.similarity(doc2)
# # return similarity_score
# # def calculate_simhash_similarity(text1, text2):
# # hash1 = Simhash(text1)
# # hash2 = Simhash(text2)
# # similarity = 1 - (hash1.distance(hash2) / 64)
# # return similarity
# from sklearn.feature_extraction.text import TfidfVectorizer
# from sentence_transformers import SentenceTransformer, util
# from nltk.metrics.distance import edit_distance
# from scipy.spatial.distance import euclidean
# import spacy
# from simhash import Simhash
# import torch
# def calculate_tfidf_similarity(text1, text2):
# tfidf_vectorizer = TfidfVectorizer()
# tfidf_matrix = tfidf_vectorizer.fit_transform([text1, text2])
# similarity_score = (tfidf_matrix * tfidf_matrix.T).toarray()[0, 1]
# return similarity_score
# def calculate_sentence_transformer_similarity(text1, text2):
# model = SentenceTransformer('LaBSE')
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model.to(device)
# embedding1 = model.encode([text1], convert_to_tensor=True).to(device)
# embedding2 = model.encode([text2], convert_to_tensor=True).to(device)
# similarity_score = util.pytorch_cos_sim(embedding1, embedding2)[0][0].item()
# return similarity_score
# def calculate_levenshtein_similarity(text1, text2):
# levenshtein_score = 1 - edit_distance(text1, text2) / max(len(text1), len(text2))
# return levenshtein_score
# def calculate_bert_similarity(text1, text2):
# model = SentenceTransformer('LaBSE')
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model.to(device)
# embedding1 = model.encode([text1], convert_to_tensor=True).to(device)[0]
# embedding2 = model.encode([text2], convert_to_tensor=True).to(device)[0]
# similarity_score = 1 / (1 + euclidean(embedding1.cpu(), embedding2.cpu()))
# return similarity_score
# def calculate_spacy_similarity(text1, text2, model_name):
# nlp = spacy.load(model_name)
# doc1 = nlp(text1)
# doc2 = nlp(text2)
# similarity_score = doc1.similarity(doc2)
# return similarity_score
# def calculate_simhash_similarity(text1, text2):
# hash1 = Simhash(text1)
# hash2 = Simhash(text2)
# similarity = 1 - (hash1.distance(hash2) / 64)
# return similarity
import torch
import spacy
from sklearn.feature_extraction.text import TfidfVectorizer
from sentence_transformers import SentenceTransformer, util
from nltk.metrics.distance import edit_distance
from scipy.spatial.distance import euclidean
from simhash import Simhash
# Load models and resources
nlp_spacy = spacy.load("en_core_web_lg")
nlp2 = spacy.load("D:/CVNER/archive/output/model-best")
model_sentence_transformer = SentenceTransformer('LaBSE')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def calculate_tfidf_similarity(text1, text2):
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform([text1, text2])
similarity_score = (tfidf_matrix * tfidf_matrix.T).toarray()[0, 1]
return similarity_score
def calculate_sentence_transformer_similarity(text1, text2):
embeddings = model_sentence_transformer.encode([text1, text2], convert_to_tensor=True).to(device)
similarity_score = util.pytorch_cos_sim(embeddings[0].unsqueeze(0), embeddings[1].unsqueeze(0))[0][0].item()
return similarity_score
def calculate_levenshtein_similarity(text1, text2):
levenshtein_score = 1 - edit_distance(text1, text2) / max(len(text1), len(text2))
return levenshtein_score
def calculate_bert_similarity(text1, text2):
embeddings = model_sentence_transformer.encode([text1, text2], convert_to_tensor=True).to(device)
similarity_score = 1 / (1 + euclidean(embeddings[0].cpu(), embeddings[1].cpu()))
return similarity_score
def calculate_spacy_similarity(text1, text2):
doc1 = nlp_spacy(text1)
doc2 = nlp_spacy(text2)
similarity_score = doc1.similarity(doc2)
return similarity_score
def calculate_simhash_similarity(text1, text2):
hash1 = Simhash(text1)
hash2 = Simhash(text2)
similarity = 1 - (hash1.distance(hash2) / 64)
return similarity
def extract_ner_from_cvs(cv_texts):
ner_results = []
for cv_text in cv_texts:
doc = nlp2(cv_text)
ner_info = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
ner_results.append(ner_info)
return ner_results