-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWebSearchEngine.py
626 lines (602 loc) · 23.7 KB
/
WebSearchEngine.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# Copyright (C) 2022-2024 Stefan-Mihai MOGA
# This file is part of WebSearchEngine application developed by Stefan-Mihai MOGA.
#
# WebSearchEngine is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Open
# Source Initiative, either version 3 of the License, or any later version.
#
# WebSearchEngine is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# WebSearchEngine. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>
import mysql.connector
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from urllib.parse import urljoin
import urllib
import time
from gensim.utils import tokenize
HOSTNAME = "localhost"
DATABASE = "r46882text_mining"
USERNAME = "r46882text_engine"
PASSWORD = "TextMining2021!@#$"
webpage_count = 0
def create_database():
try:
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
sql_drop_table = "DROP TABLE IF EXISTS `occurrence`"
cursor = connection.cursor()
cursor.execute(sql_drop_table)
sql_drop_table = "DROP TABLE IF EXISTS `keyword`"
cursor.execute(sql_drop_table)
sql_drop_table = "DROP TABLE IF EXISTS `webpage`"
cursor.execute(sql_drop_table)
sql_drop_table = "DROP TABLE IF EXISTS `frontier`"
cursor.execute(sql_drop_table)
sql_create_table = (
"CREATE TABLE `frontier` (`url_id` BIGINT NOT NULL AUTO_INCREMENT, "
"`address` VARCHAR(256) NOT NULL, `visited` BOOL NOT NULL, "
"`score` BIGINT NOT NULL, PRIMARY KEY(`url_id`)) ENGINE=InnoDB "
"CHARACTER SET utf8 COLLATE utf8_general_ci"
)
cursor.execute(sql_create_table)
sql_create_index = (
"CREATE UNIQUE INDEX frontier_address ON `frontier`(`address`)"
)
cursor.execute(sql_create_index)
sql_create_table = (
"CREATE TABLE `webpage` (`webpage_id` BIGINT NOT NULL AUTO_INCREMENT, "
"`url` VARCHAR(256) NOT NULL, `title` VARCHAR(256) NOT NULL, "
"`content` LONGTEXT NOT NULL, PRIMARY KEY(`webpage_id`)) ENGINE=InnoDB "
"CHARACTER SET utf8 COLLATE utf8_general_ci"
)
cursor.execute(sql_create_table)
sql_create_table = (
"CREATE TABLE `keyword` (`keyword_id` BIGINT NOT NULL AUTO_INCREMENT, "
"`name` VARCHAR(256) NOT NULL, PRIMARY KEY(`keyword_id`)) ENGINE=InnoDB "
"CHARACTER SET utf8 COLLATE utf8_general_ci"
)
cursor.execute(sql_create_table)
sql_create_table = (
"CREATE TABLE `occurrence` (`webpage_id` BIGINT NOT NULL, "
"`keyword_id` BIGINT NOT NULL, `counter` BIGINT NOT NULL, "
"`pagerank` REAL NOT NULL, PRIMARY KEY(`webpage_id`, `keyword_id`), "
"FOREIGN KEY webpage_fk(webpage_id) REFERENCES webpage(webpage_id), "
"FOREIGN KEY keyword_fk(keyword_id) REFERENCES keyword(keyword_id)) "
"ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci"
)
cursor.execute(sql_create_table)
sql_create_index = "CREATE UNIQUE INDEX index_name ON `keyword`(`name`)"
cursor.execute(sql_create_index)
sql_no_of_words = (
"CREATE OR REPLACE FUNCTION no_of_words(token VARCHAR(256)) RETURNS "
"REAL READS SQL DATA RETURN (SELECT MAX(`counter`) FROM `occurrence` "
"INNER JOIN `keyword` USING(`keyword_id`) WHERE `name` = token)"
)
cursor.execute(sql_no_of_words)
sql_no_of_pages = (
"CREATE OR REPLACE FUNCTION no_of_pages(token VARCHAR(256)) RETURNS "
"REAL READS SQL DATA RETURN (SELECT COUNT(`webpage_id`) FROM `occurrence` "
"INNER JOIN `keyword` USING(`keyword_id`) WHERE `name` = token)"
)
cursor.execute(sql_no_of_pages)
sql_total_pages = (
"CREATE OR REPLACE FUNCTION total_pages() RETURNS REAL READS SQL DATA "
"RETURN (SELECT COUNT(`webpage_id`) FROM `webpage`)"
)
cursor.execute(sql_total_pages)
sql_data_mining = (
"CREATE OR REPLACE FUNCTION data_mining(webpage_no BIGINT, token VARCHAR(256)) "
"RETURNS REAL READS SQL DATA RETURN (SELECT SUM(`counter`)/no_of_words(token)*"
"LOG((1+total_pages())/no_of_pages(token)) FROM `occurrence` INNER JOIN `keyword` "
"USING(`keyword_id`) WHERE `name` = token AND `webpage_id` = webpage_no)"
)
cursor.execute(sql_data_mining)
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
return False
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is now closed")
return True
def add_url_to_frontier(connection, url):
if url.find("#") > 0:
url = url.split("#")[0]
if len(url) > 256:
return False
if url.endswith(".3g2"):
return False # 3GPP2 multimedia file
if url.endswith(".3gp"):
return False # 3GPP multimedia file
if url.endswith(".7z"):
return False # 7-Zip compressed file
if url.endswith(".ai"):
return False # Adobe Illustrator file
if url.endswith(".apk"):
return False # Android package file
if url.endswith(".arj"):
return False # ARJ compressed file
if url.endswith(".aif"):
return False # AIF audio file
if url.endswith(".avi"):
return False # AVI file
if url.endswith(".bat"):
return False # Batch file
if url.endswith(".bin"):
return False # Binary disc image
if url.endswith(".bmp"):
return False # Bitmap image
if url.endswith(".cda"):
return False # CD audio track file
if url.endswith(".com"):
return False # MS-DOS command file
if url.endswith(".csv"):
return False # Comma separated value file
if url.endswith(".dat"):
return False # Binary Data file
if url.endswith(".db") or url.endswith(".dbf"):
return False # Database file
if url.endswith(".deb"):
return False # Debian software package file
if url.endswith(".dmg"):
return False # macOS X disk image
if url.endswith(".doc") or url.endswith(".docx"):
return False # Microsoft Word Open XML document file
if url.endswith(".email") or url.endswith(".eml"):
return False # E-mail message file from multiple e-mail clients
if url.endswith(".emlx"):
return False # Apple Mail e-mail file
if url.endswith(".exe"):
return False # MS-DOS executable file
if url.endswith(".flv"):
return False # Adobe Flash file
if url.endswith(".fon"):
return False # Generic font file
if url.endswith(".fnt"):
return False # Windows font file
if url.endswith(".gadget"):
return False # Windows gadget
if url.endswith(".gif"):
return False # GIF image
if url.endswith(".h264"):
return False # H.264 video file
if url.endswith(".ico"):
return False # Icon file
if url.endswith(".iso"):
return False # ISO disc image
if url.endswith(".jar"):
return False # Java archive file
if url.endswith(".jpg") or url.endswith(".jpeg"):
return False # JPEG image
if url.endswith(".log"):
return False # Log file
if url.endswith(".m4v"):
return False # Apple MP4 video file
if url.endswith(".mdb"):
return False # Microsoft Access database file
if url.endswith(".mid") or url.endswith(".midi"):
return False # MIDI audio file
if url.endswith(".mov"):
return False # Apple QuickTime movie file
if url.endswith(".mp3") or url.endswith(".mpa"):
return False # MP3 audio file
if url.endswith(".mp4"):
return False # MPEG4 video file
if url.endswith(".mpa"):
return False # MPEG-2 audio file
if url.endswith(".mpg") or url.endswith(".mpeg"):
return False # MPEG video file
if url.endswith(".msg"):
return False # Microsoft Outlook e-mail message file
if url.endswith(".msi"):
return False # Windows installer package
if url.endswith(".odt"):
return False # OpenOffice Writer document file
if url.endswith(".ods"):
return False # OpenOffice Calc spreadsheet file
if url.endswith(".oft"):
return False # Microsoft Outlook e-mail template file
if url.endswith(".ogg"):
return False # Ogg Vorbis audio file
if url.endswith(".ost"):
return False # Microsoft Outlook e-mail storage file
if url.endswith(".otf"):
return False # Open type font file
if url.endswith(".pkg"):
return False # Package file
if url.endswith(".pdf"):
return False # Adobe PDF file
if url.endswith(".png"):
return False # PNG image
if url.endswith(".ppt") or url.endswith(".pptx"):
return False # Microsoft PowerPoint Open XML presentation
if url.endswith(".ps"):
return False # PostScript file
if url.endswith(".psd"):
return False # PSD image
if url.endswith(".pst"):
return False # Microsoft Outlook e-mail storage file
if url.endswith(".rar"):
return False # RAR file
if url.endswith(".rpm"):
return False # Red Hat Package Manager
if url.endswith(".rtf"):
return False # Rich Text Format file
if url.endswith(".sql"):
return False # SQL database file
if url.endswith(".svg"):
return False # Scalable Vector Graphics file
if url.endswith(".swf"):
return False # Shockwave flash file
if url.endswith(".xls") or url.endswith(".xlsx"):
return False # Microsoft Excel Open XML spreadsheet file
if url.endswith(".toast"):
return False # Toast disc image
if url.endswith(".tar"):
return False # Linux tarball file archive
if url.endswith(".tar.gz"):
return False # Tarball compressed file
if url.endswith(".tex"):
return False # A LaTeX document file
if url.endswith(".ttf"):
return False # TrueType font file
if url.endswith(".txt"):
return False # Plain text file
if url.endswith(".tif") or url.endswith(".tiff"):
return False # TIFF image
if url.endswith(".vcd"):
return False # Virtual CD
if url.endswith(".vcf"):
return False # E-mail contact file
if url.endswith(".vob"):
return False # DVD Video Object
if url.endswith(".xml"):
return False # XML file
if url.endswith(".wav") or url.endswith(".wma"):
return False # WAV file
if url.endswith(".wmv"):
return False # Windows Media Video file
if url.endswith(".wpd"):
return False # WordPerfect document
if url.endswith(".wpl"):
return False # Windows Media Player playlist
if url.endswith(".wsf"):
return False # Windows script file
if url.endswith(".z") or url.endswith(".zip"):
return False # Z or Zip compressed file
try:
if not connection.is_connected():
time.sleep(30)
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
sql_statement = (
"UPDATE `frontier` SET `score` = `score` + 1 WHERE `address` = '%s'" % url
)
cursor = connection.cursor()
cursor.execute(sql_statement)
if cursor.rowcount == 0:
sql_statement = (
"INSERT INTO `frontier` (`address`,`visited`, `score`) VALUES ('%s', FALSE, 1)"
% url
)
cursor.execute(sql_statement)
cursor.close()
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
return False
finally:
pass
return True
def extract_url_from_frontier(connection):
url = None
try:
if not connection.is_connected():
time.sleep(30)
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
sql_statement = "SELECT `address` FROM `frontier` WHERE `visited` = FALSE ORDER BY `score` DESC, `url_id` ASC LIMIT 1"
cursor = connection.cursor()
cursor.execute(sql_statement)
records = cursor.fetchall()
for row in records:
url = row[0]
sql_statement = (
"UPDATE `frontier` SET `visited` = TRUE WHERE `address` = '%s'" % url
)
cursor.execute(sql_statement)
cursor.close()
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
pass
return connection, url
def download_page_from_url(connection, url):
html_title = None
plain_text = None
try:
if not connection.is_connected():
time.sleep(30)
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
req = Request(url)
html_page = urlopen(req)
soup = BeautifulSoup(html_page, "html.parser")
html_title = soup.title.get_text().strip()
plain_text = soup.get_text().strip()
plain_text = " ".join(plain_text.split())
for hyperlink in soup.find_all("a"):
hyperlink = urljoin(url, hyperlink.get("href"))
add_url_to_frontier(connection, hyperlink)
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
except urllib.error.URLError as err:
print(str(err))
except urllib.error.HTTPError as err:
print(str(err))
except urllib.error.ContentTooShortError as err:
print(str(err))
finally:
return connection, html_title, plain_text
def get_webpage_count(connection):
counter = -1
try:
if not connection.is_connected():
time.sleep(30)
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
sql_last_id = "SELECT COUNT(`webpage_id`) FROM `webpage`"
cursor = connection.cursor()
cursor.execute(sql_last_id)
records = cursor.fetchone()
counter = records[0]
cursor.close()
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
return -1
finally:
pass
return counter
def web_search_engine():
global webpage_count
try:
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
webpage_count = get_webpage_count(connection)
print("get_webpage_count = %d" % webpage_count)
add_url_to_frontier(connection, "https://en.wikipedia.org/")
while True:
connection, url = extract_url_from_frontier(connection)
if url:
print("Crawling %s... [%d]" % (url, webpage_count + 1))
connection, html_title, plain_text = download_page_from_url(connection, url)
if html_title and plain_text:
if len(html_title) > 0:
connection = analyze_webpage(
connection, url, html_title, plain_text
)
if (webpage_count > 0) and ((webpage_count % 1000) == 0):
if connection.is_connected():
connection.close()
print("MySQL connection is now closed")
data_mining()
else:
break
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
if connection.is_connected():
connection.close()
print("MySQL connection is now closed")
def analyze_webpage(connection, url, html_title, plain_text):
global webpage_count
while not connection.is_connected():
try:
time.sleep(30)
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
pass
try:
# html_title = html_title.encode(encoding='utf-8')
# plain_text = plain_text.encode(encoding='utf-8')
sql_statement = (
"INSERT INTO `webpage` (`url`, `title`, `content`) VALUES ('%s', '%s', '%s')"
% (url, html_title.replace("'", '"'), plain_text.replace("'", '"'))
)
cursor = connection.cursor()
cursor.execute(sql_statement)
if cursor.rowcount == 0:
return connection
sql_last_id = "SET @last_webpage_id = LAST_INSERT_ID()"
cursor.execute(sql_last_id)
cursor.close()
webpage_count = webpage_count + 1
return analyze_keyword(connection, plain_text)
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
pass
return connection
def analyze_keyword(connection, plain_text):
global webpage_count
keyword_count = {}
tokenize_list = tokenize(plain_text)
for keyword in tokenize_list:
if keyword.isascii() and keyword.isalnum():
keyword = keyword.lower()
if keyword_count.get(keyword) is None:
keyword_count[keyword] = 1
else:
keyword_count[keyword] = keyword_count[keyword] + 1
for keyword in keyword_count.keys():
done = False
while not done:
try:
if not connection.is_connected():
time.sleep(30)
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
sql_last_id = "SET @last_webpage_id = %d" % webpage_count
cursor = connection.cursor()
cursor.execute(sql_last_id)
# keyword = keyword.encode(encoding='utf-8')
sql_statement = (
"SELECT `keyword_id` FROM `keyword` WHERE `name` = '%s'" % keyword
)
cursor = connection.cursor()
cursor.execute(sql_statement)
records = cursor.fetchone()
if cursor.rowcount == 0:
sql_statement = (
"INSERT INTO `keyword` (`name`) VALUES ('%s')" % keyword
)
cursor.execute(sql_statement)
sql_last_id = "SET @last_keyword_id = LAST_INSERT_ID()"
cursor.execute(sql_last_id)
else:
sql_last_id = "SET @last_keyword_id = %d" % records[0]
cursor.execute(sql_last_id)
sql_statement = (
"INSERT INTO `occurrence` (`webpage_id`, `keyword_id`, `counter`, `pagerank`) VALUES "
"(@last_webpage_id, @last_keyword_id, %d, 0.0)"
% keyword_count[keyword]
)
cursor.execute(sql_statement)
cursor.close()
done = True
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
pass
return connection
def data_mining():
records = None
connection = None
rowcount = 0
try:
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
sql_select_query = "SELECT * FROM `keyword` ORDER BY `keyword_id`"
cursor = connection.cursor()
cursor.execute(sql_select_query)
# get all records
records = cursor.fetchall()
print("Total number of rows in table:", cursor.rowcount)
rowcount = cursor.rowcount
cursor.close()
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
pass
for row in records:
done = False
while not done:
try:
if not connection.is_connected():
time.sleep(30)
connection = mysql.connector.connect(
host=HOSTNAME,
database=DATABASE,
user=USERNAME,
password=PASSWORD,
autocommit=True,
)
server_info = connection.get_server_info()
print("MySQL connection is open on", server_info)
data_update = connection.cursor()
sql_update_query = (
"UPDATE `occurrence` INNER JOIN `keyword` USING(`keyword_id`)"
"SET `pagerank` = data_mining(`webpage_id`, `name`) WHERE `name` = '%s'"
% row[1]
)
print(
"Applying data mining for '%s'... [%d/%d]"
% (row[1], records.index(row) + 1, rowcount)
)
data_update.execute(sql_update_query)
data_update.close()
done = True
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
pass
try:
if connection.is_connected():
connection.close()
print("MySQL connection is now closed")
except mysql.connector.Error as err:
print("MySQL connector error:", str(err))
finally:
pass
if create_database():
web_search_engine()