-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsimilarity.py
49 lines (35 loc) · 1.38 KB
/
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
import nltk
import websearch
from difflib import SequenceMatcher
import pandas as pd
nltk.download('stopwords')
nltk.download('punkt')
stop_words = set(nltk.corpus.stopwords.words('english'))
def purifyText(string):
words = nltk.word_tokenize(string)
return (" ".join([word for word in words if word not in stop_words]))
def webVerify(string, results_per_sentence):
sentences = nltk.sent_tokenize(string)
matching_sites = []
for url in websearch.searchBing(query=string, num=results_per_sentence):
matching_sites.append(url)
for sentence in sentences:
for url in websearch.searchBing(query = sentence, num = results_per_sentence):
matching_sites.append(url)
return (list(set(matching_sites)))
def similarity(str1, str2):
return (SequenceMatcher(None,str1,str2).ratio())*100
def report(text):
matching_sites = webVerify(purifyText(text), 2)
matches = {}
for i in range(len(matching_sites)):
matches[matching_sites[i]] = similarity(text, websearch.extractText(matching_sites[i]))
matches = {k: v for k, v in sorted(matches.items(), key=lambda item: item[1], reverse=True)}
return matches
def returnTable(dictionary):
df = pd.DataFrame({'Similarity (%)': dictionary})
#df = df.fillna(' ').T
#df = df.transpose()
return df.to_html()
if __name__ == '__main__':
report('This is a pure test')