/
bookutil.py
60 lines (45 loc) · 1.75 KB
/
bookutil.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
#!/usr/bin/env python3
import docx2txt
import ebooklib
from ebooklib import epub
import os.path
import PyPDF2
import re
def get_text(file_path: str):
extension = os.path.splitext(file_path)[1]
text = ""
if extension == ".epub":
book = epub.read_epub(file_path)
for doc in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
text += doc.get_content().decode("utf-8")
elif extension == ".docx":
text += docx2txt.process(file_path)
elif extension == ".mobi":
print("[!] Unsupported file: " + file_path)
return None
elif extension == ".pdf":
pdf_file = open(file_path, "rb")
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
for x in range(number_of_pages):
page = read_pdf.getPage(x)
text += page.extractText()
else:
if not extension == ".txt":
print("[!] Unkown file type: " + file_path + ", processing raw text...")
text_file = open(file_path, "r", encoding="utf-8")
text += text_file.read()
text = text.strip().rstrip().replace('\n', '')
return text
def count_words_in_text(text: str, wordlist: list):
if text == "" or text is None:
return None
words_count = {}
for word in wordlist:
word = word.strip().rstrip().replace('\n', '')
words_count[word] = len(re.findall("(?i)\s" + re.escape(word) + "(\s|\.|!|\?|\')", text))
sorted_words_count = [(k, words_count[k]) for k in sorted(words_count, key=words_count.get, reverse=True)]
sorted_text = ""
for word, amount in sorted_words_count:
sorted_text += word + ": " + str(amount) + "\n"
return sorted_text