Ruby/reader/reader.py
Dani 4d4b39b4c7 added a brainmap checker,
Fixed the trainer and reader
2025-04-27 16:40:50 -04:00

82 lines
2.6 KiB
Python

import os
import asyncio
import json
from model.trainer import train_on_message
from model.scheduler import set_next_action
from reader.filter import is_valid_line
BOOK_DIR = "data/books"
PROGRESS_FILE = "data/memory/book_progress.json"
READ_DELAY = 0.2 # seconds between paragraphs
PARAGRAPH_MIN_LENGTH = 20
def get_books():
return [f for f in os.listdir(BOOK_DIR) if f.endswith(".txt")]
def load_progress():
if os.path.exists(PROGRESS_FILE):
with open(PROGRESS_FILE, "r", encoding="utf-8") as f:
return json.load(f)
return {"progress": {}, "completed": []}
def save_progress(prog):
with open(PROGRESS_FILE, "w", encoding="utf-8") as f:
json.dump(prog, f, indent=2)
async def read_books_forever():
books = get_books()
progress_data = load_progress()
progress = progress_data.get("progress", {})
completed_books = progress_data.get("completed", [])
while True:
# Filter out completed books
available_books = [b for b in books if b not in completed_books]
if not available_books:
print("[Reader] All books completed. Resetting progress.")
progress_data = {"progress": {}, "completed": []}
save_progress(progress_data)
available_books = books # Re-enable all books
progress = {}
completed_books = []
for book in available_books:
path = os.path.join(BOOK_DIR, book)
if not os.path.exists(path):
continue
with open(path, "r", encoding="utf-8") as f:
lines = f.readlines()
idx = progress.get(book, 0)
paragraph = ""
while idx < len(lines):
line = lines[idx].strip()
idx += 1
if not line:
if len(paragraph) > PARAGRAPH_MIN_LENGTH:
train_on_message(paragraph.strip(), source="book")
paragraph = ""
await asyncio.sleep(READ_DELAY)
set_next_action(READ_DELAY, "Reading")
else:
paragraph += " " + line
progress[book] = idx
progress_data["progress"] = progress
save_progress(progress_data)
# End of book
if idx >= len(lines):
print(f"[Reader] Finished reading {book}.")
completed_books.append(book)
progress_data["completed"] = list(set(completed_books)) # Avoid duplicates
save_progress(progress_data)