Repository navigation
Expand file tree
/
Copy pathbook_processing.py
More file actions
226 lines (181 loc) · 7.4 KB
/
Copy pathbook_processing.py
File metadata and controls
226 lines (181 loc) · 7.4 KB
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
import requests
import os
import random
import re
import json
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
# Constants
GUTENBERG_TOP_100_URL = "https://www.gutenberg.org/browse/scores/top"
# Add project root detection to make paths work in tests
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
LOCAL_LIBRARY_PATH = os.path.join(PROJECT_ROOT, "local_library")
BOOK_IDS_FILE = os.path.join(PROJECT_ROOT, "data/book_ids.json")
FAILED_BOOK_IDS_FILE = os.path.join(PROJECT_ROOT, "data/failed_book_ids.json")
def save_book_ids(book_ids):
"""
Saves book IDs and the current date to a local JSON file.
"""
data = {
'date': datetime.now().strftime("%Y-%m-%d"),
'book_ids': book_ids
}
# Ensure the directory exists
os.makedirs(os.path.dirname(BOOK_IDS_FILE), exist_ok=True)
with open(BOOK_IDS_FILE, 'w', encoding='utf-8') as file:
json.dump(data, file)
def load_book_ids():
"""
Loads book IDs from the local JSON file if available and not outdated.
"""
if os.path.exists(BOOK_IDS_FILE):
with open(BOOK_IDS_FILE, 'r', encoding='utf-8') as file:
data = json.load(file)
saved_date = datetime.strptime(data['date'], "%Y-%m-%d")
if saved_date.date() >= (datetime.now() - timedelta(days=1)).date():
return data['book_ids']
return None
def save_failed_book_ids(failed_book_ids):
# Ensure the directory exists
os.makedirs(os.path.dirname(FAILED_BOOK_IDS_FILE), exist_ok=True)
with open(FAILED_BOOK_IDS_FILE, 'w', encoding='utf-8') as file:
json.dump(failed_book_ids, file)
def load_failed_book_ids():
if os.path.exists(FAILED_BOOK_IDS_FILE):
with open(FAILED_BOOK_IDS_FILE, 'r', encoding='utf-8') as file:
return json.load(file)
return []
def get_top_books_ids(limit=10):
"""
Fetches IDs of top books from Project Gutenberg's 'Top 100 EBooks last 30 days' section.
"""
response = requests.get(GUTENBERG_TOP_100_URL)
soup = BeautifulSoup(response.content, 'html.parser')
top_30_section = soup.find('h2', id='books-last30').find_next('ol')
book_links = top_30_section.find_all('a', href=True)
book_ids = [link['href'].split('/')[-1] for link in book_links]
return book_ids[:limit]
def fetch_and_store_top_books_ids():
"""
Fetches and stores the top book IDs
if they are not already stored or outdated.
"""
book_ids = load_book_ids()
if not book_ids:
book_ids = get_top_books_ids()
save_book_ids(book_ids)
return book_ids
def download_text(url):
"""
Downloads and returns the text from a given Project Gutenberg URL.
"""
response = requests.get(url)
response.raise_for_status()
return response.text
def preprocess_and_save_text(text, book_id):
"""
Preprocesses the text by extracting the main content,
converting to lowercase, and keeping alphabetic characters only.
Then, saves it to a file.
"""
main_text = extract_main_content(text)
processed_text = re.sub('[^a-z]+', '', main_text.lower())
local_path = os.path.join(LOCAL_LIBRARY_PATH, f"{book_id}_processed.txt")
with open(local_path, 'w', encoding='utf-8') as file:
file.write(processed_text)
def extract_main_content(text):
"""
Extracts the main content of the book from the given text.
"""
start_pattern = r"\*\*\* START OF (THIS|THE) PROJECT GUTENBERG EBOOK"
end_pattern = r"\*\*\* END OF (THIS|THE) PROJECT GUTENBERG EBOOK"
start_match = re.search(start_pattern, text)
end_match = re.search(end_pattern, text)
if start_match and end_match:
start_index = start_match.end()
end_index = end_match.start()
return text[start_index:end_index].strip()
# Handle error or return a default value if markers are not found
print("Could not find Project Gutenberg markers, returning full text")
return text
def download_and_store_book(book_id, failed_book_ids):
"""
Downloads book based on ID, preprocesses, and stores locally.
"""
if not os.path.exists(LOCAL_LIBRARY_PATH):
os.makedirs(LOCAL_LIBRARY_PATH)
canonical_url = (
f"https://www.gutenberg.org/cache/epub/{book_id}/pg{book_id}.txt")
processed_local_path = os.path.join(
LOCAL_LIBRARY_PATH, f"{book_id}_processed.txt")
if not os.path.exists(processed_local_path):
try:
text = download_text(canonical_url)
preprocess_and_save_text(text, book_id)
except requests.exceptions.RequestException as e:
print(f"Failed to download book ID {book_id}: {e}")
failed_book_ids.append(book_id)
save_failed_book_ids(failed_book_ids)
return False
return True
def _generate_test_text(length):
"""
Generate random text for testing when books aren't available.
"""
chars = 'abcdefghijklmnopqrstuvwxyz'
return ''.join(random.choice(chars) for _ in range(length))
def get_random_text_passage(length):
"""
Selects a random book from the list of top 100 IDs,
downloads and preprocesses it if necessary,
and extracts a random text passage.
Falls back to generated text if books can't be accessed (for testing).
"""
try:
# First check if we're running in a test environment by checking if any books exist
if not os.path.exists(LOCAL_LIBRARY_PATH):
# For testing - create if library path doesn't exist
os.makedirs(LOCAL_LIBRARY_PATH, exist_ok=True)
# No books downloaded yet, generate random text for tests
return _generate_test_text(length)
book_ids = fetch_and_store_top_books_ids()
if not book_ids:
return _generate_test_text(length)
max_attempts = 5 # Limit retries to avoid infinite loop
attempts = 0
while attempts < max_attempts:
attempts += 1
random_book_id = random.choice(book_ids)
failed_book_ids = load_failed_book_ids()
if random_book_id in failed_book_ids:
continue
processed_file_path = os.path.join(
LOCAL_LIBRARY_PATH, f"{random_book_id}_processed.txt")
if not os.path.exists(processed_file_path):
if not download_and_store_book(random_book_id, failed_book_ids):
continue
try:
with open(processed_file_path, 'r', encoding='utf-8') as file:
text = file.read()
if len(text) < length:
continue
start_index = random.randint(0, len(text) - length)
return text[start_index:start_index + length]
except Exception as e:
print(f"Error reading book file: {e}")
continue
# If all attempts fail, use test text
return _generate_test_text(length)
except Exception as e:
print(f"Error in get_random_text_passage: {e}")
# Fall back to random text for tests
return _generate_test_text(length)
# Example Usage
if __name__ == "__main__":
failed_book_ids = load_failed_book_ids()
samples = []
# Specify the length of the text passage in characters
text_length = 500
random_passage = get_random_text_passage(text_length)
print(random_passage)
save_failed_book_ids(failed_book_ids)