-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTitleWords.py
53 lines (44 loc) · 1.47 KB
/
getTitleWords.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
# - *- coding: utf- 8 - *-
import nltk
from nltk import sent_tokenize,word_tokenize
from removestopwords import *
import re
import MySQLdb
def getTitleWords(bookid, type):
titlewords = []
titles = []
db = MySQLdb.connect("localhost", "root", "root", "sparks", charset='utf8', use_unicode=True)
cursor = db.cursor()
sql = "SELECT bookDetails.bookPath FROM bookDetails WHERE bookDetails.bookdetailID = " + bookid
cursor.execute(sql)
row = cursor.fetchone()
readPath = row[0]
print(row[0])
db.commit()
read_file = open(readPath, 'r', encoding="utf16")
file = read_file.read()
match = re.findall(r'(<h[0-9]>(.*?)</h[0-9]>)', file)
match2 = re.findall(r'(<h2>(.*?)</h2>)', file)
match3 = re.findall(r'(<h4>(.*?)</h4>)', file)
for m2 in match2:
# print(m2[1])
titles.append(m2[1])
for m3 in match3:
# print(m3[1])
titles.append(m3[1])
# print(titles)
for m in match:
# print(m[1])
words = word_tokenize(m[1])
for word in words:
if word.isnumeric() == False:
titlewords.append(word)
if type != "book":
match4 = re.findall(r'(<h5>(.*?)</h5>)', file)
for m in match4:
# print(m3[1])
titles.append(m[1])
titlewords = removestopwords(titlewords)
# print(titlewords)
return titlewords
# getTitleWords('./Data/books/වර්තමාන ලංකාව සහ ලොක ඉතිහාසය.txt')