-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
218 lines (178 loc) · 5.16 KB
/
demo.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import requests
from bs4 import BeautifulSoup
import re
from nltk.corpus import stopwords
from nltk import download
import gensim
from gensim.models import Word2Vec
download('stopwords') # Download stopwords
model = gensim.models.KeyedVectors.load_word2vec_format("GoogleNews-vectors-negative300.bin", binary=True)
sent_final = []
def tokenize_sentence(text):
sentences = re.split('(?<!\\w\\.\\w.)(?<![A-Z][a-z]\\.)(?<=\\.|\\?|\\!)\\s', text)
print("Tokenize: ")
print(sentences)
return sentences
def into_words(sentence):
sentence = sentence.lower()
sentence = re.findall('[a-z]\w+', sentence)
return sentence
def remove_punctuation(s):
return re.sub(r'[^\w\s]', '', s)
#
# def binarySearch(arr, x):
# arr = sorted(arr)
# l = 0
# r = len(arr) - 1
# while l <= r:
# m = l + int(((r - l) / 2))
# if x == arr[m]:
# return m
# if x > arr[m]:
# l = m + 1
# else:
# r = m - 1
# return -1
#
# #Percentage of plagiarism
# def compare(sent1, sent2):
# count = 0
# for x in sent1:
# if binarySearch(sent2, x) != -1:
# count += 1
#
# # print(count, sent2)
#
# n1 = len(sent1)
# n2 = len(sent2)
#
# if n1 == 0 or n2 == 0:
# return 0
#
# return 100 * (count / n1) * (min(n1, n2) / max(n1, n2)) #percentage
def get_urls(sent):
Base_string = "https://www.google.com/search?q="
Google_Search = Base_string + sent
res = requests.get(Google_Search)
soup = BeautifulSoup(res.text, 'html.parser')
# print(soup)
links_list = []
links = soup.findAll("a")
for link in links:
link_href = link.get('href')
if "url?q=" in link_href and not "webcache" in link_href:
links_list.append(link.get('href').split("?q=")[1].split("&sa=U")[0])
links = []
count = 0
for x in links_list:
if count == 5:
break
if 'youtube' not in x:
links.append(x)
count += 1
return links
def get_original_words(sent):
original_words = remove_punctuation(sent)
original_words = into_words(original_words)
original_words = sorted(original_words)
return original_words
def get_text_from_url(url):
try:
# print(url)
inside_webpage = requests.get(url)
except:
return []
soup2 = BeautifulSoup(inside_webpage.text, 'html.parser')
paragraphs = soup2.select('p')
string2 = ''
for para in paragraphs:
string2 += para.text
website_sent = tokenize_sentence(string2)
website_words = []
for sent in website_sent:
sent = remove_punctuation(sent)
website_words.append(into_words(sent))
return website_words
def check(sent1, sent_website):
result = []
print("Stop words initializing...")
stop_words = stopwords.words('english')
print("Stop words done")
sent1 = [w for w in sent1 if w not in stop_words]
global sent_final
sent_final = sent1[:]
for sent in sent_website:
sent = [w for w in sent if w not in stop_words]
# print("__Sentences__")
# print(sent1)
# print(sent)
print("Initializing model...")
distance = model.wmdistance(sent1, sent)
print("Done...")
print('Distance = %.4f' % distance)
if distance == 0.0:
result.append(100)
else:
result.append(distance * 100)
if distance <= 0.1:
break
return result
# Text comparison
def check_one_sent(sent):
ans = []
url = get_urls(sent)
# for x in url:
website_words = get_text_from_url(url[0])
original_words = get_original_words(sent)
ans.append(check(original_words, website_words))
return ans, url[0]
# def find_max_url(ans, url):
# mx = 0
# count = -1
# url_ans = ""
# for x in ans:
# count += 1
# for y in x:
# if y < mx:
# mx = y
# url_ans = url[count]
#
# return mx, url_ans
def get_ans_for_one_sent(sent):
ans, url = check_one_sent(sent)
print(ans)
# mx, url_ans = find_max_url(ans, url)
mx = 0
for x in ans:
for y in x:
if y < 100 and y > 50:
if y > mx:
mx = y
elif y < 50 and y > 0:
y = 100 - y
if y > mx:
mx = y
elif y > 100:
y = 100 - (y - 100)
if y > mx:
mx = y
return mx, url
def main_function(txt):
all_sentences = tokenize_sentence(txt)
# print(all_sentences)
ans = []
for x in all_sentences:
t = x.split()
if len(t) > 4:
print(str(x) + '\n' + '--' + '\n')
prob, url_ans = get_ans_for_one_sent(x)
print(str(prob) + " --- " + str(url_ans) + "\n")
ans.append([prob, url_ans])
print(ans)
print(sent_final)
return ans, sent_final
# if __name__ == "__main__":
#
# main_function('Online Application For Examination Students should fill this form online and submit the printout of the acknowledgement to the college')
# print(sent_final)
#highlight_text(sent_final)