-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocess_epubs.py
60 lines (45 loc) · 1.77 KB
/
preprocess_epubs.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
import os
import ebooklib
from ebooklib import epub
from bs4 import BeautifulSoup
import re
import nltk
def extract_text_from_epub(file_path):
book = epub.read_epub(file_path)
content = []
for item in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
soup = BeautifulSoup(item.get_content(), 'html.parser')
content.append(soup.get_text())
return "\n".join(content)
def clean_and_tokenize(text):
# Remove extra spaces and punctuation
text = re.sub(r'\s+', ' ', text) # Remove extra whitespace
text = re.sub(r'[^\w\s]', '', text) # Remove punctuation
# Tokenize
tokens = nltk.word_tokenize(text)
return tokens
def preprocess_books(input_folder, output_folder):
# Ensure output folder exists
os.makedirs(output_folder, exist_ok=True)
# Loop through EPUB files
for file_name in os.listdir(input_folder):
if file_name == 'The Books of Earthsea.epub' or file_name == 'The Wheel of Time Books 1-10.epub':
print(f'{file_name} is a collection')
continue
if file_name.endswith('.epub'):
file_path = os.path.join(input_folder, file_name)
print(f"Processing: {file_name}")
# Extract and clean text
raw_text = extract_text_from_epub(file_path)
tokens = clean_and_tokenize(raw_text)
# Save processed tokens to a new file
output_file = os.path.join(output_folder, f"{os.path.splitext(file_name)[0]}_processed.txt")
with open(output_file, 'w', encoding='utf-8') as f:
f.write(" ".join(tokens))
def main():
nltk.download('punkt_tab')
input_folder = 'Data/raw'
output_folder = 'Data/processed'
preprocess_books(input_folder, output_folder)
if __name__ == '__main__':
main()