-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookparser.py
37 lines (31 loc) · 1.22 KB
/
bookparser.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
import os
# gets the list of files in the folder, expects them to be some sort of html
def getChapters(inputFolder):
chapterList = os.listdir(inputFolder)
print("[+] Found "+str(len(chapterList))+" files in "+inputFolder)
return chapterList
# converts .xhtml files to plain text
def parseChapter(chapter):
with open(chapter) as html:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, features="html.parser")
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
plainChapter = soup.get_text()
print("[+] Converted "+chapter+" into plaint text")
return plainChapter
print("[!] Failed to convert "+chapter+" into plaint text")
return
#split text to small parts as tacotron2 doesn't like long sentences and will clip them
def splitSentences(fulltext):
#for splitting text by sentence
import nltk.data
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
sentences =tokenizer.tokenize(fulltext)
#addittionally splitting at every comma
subsentences = []
for i in sentences:
subsentences.extend(i.split(", "))
return subsentences