-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
similarity.py
29 lines (24 loc) · 878 Bytes
/
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
import spacy
class Match:
"""Ref: https://spacy.io/"""
def __init__(self):
"""Load language model."""
# spacy.prefer_gpu()
# self.nlp = spacy.load("en_core_web_sm")
self.nlp = spacy.load("en_core_web_md")
# self.nlp = spacy.load("en_core_web_lg") # Switch to large version for performance
def compare(self, s1, s2):
"""
Measure similarity score between two strings.
Ref: https://spacy.io/api/doc#similarity
"""
p1 = self.nlp(s1)
p2 = self.nlp(s2)
return p1.similarity(p2)
if __name__ == "__main__":
"""Example"""
match = Match()
s1 = "How much is the cost of one student's school fees?"
s2 = "How much do I need to pay for my child's school fees?"
score = match.compare(s1, s2)
print("Similarity score:", score)