"""Split the Project Gutenberg text of George Randolph Chester, *Get-Rich-Quick Wallingford* (eBook #37010), one story per chapter. It is a novel of linked episodes: each of the promoter's schemes runs over several chapters, so for the recurrence rule the reader groups chapters into schemes (ARCS in claude/groups_src.py) and a situation must recur across schemes, not merely across chapters of one scheme. The body heads each chapter with a bare `CHAPTER ` line followed by an upper-case title that may wrap; the headings must number 1..28.""" import re, sys sys.path.insert(0, '..') from gutenberg_chunks import gutenberg_body, paragraphs, write VOLUME = 'Get-Rich-Quick Wallingford (George Randolph Chester, 1908)' ROMAN = {'I': 1, 'V': 5, 'X': 10, 'L': 50} def roman(s): return sum(-ROMAN[a] if ROMAN[a] < ROMAN.get(b, 0) else ROMAN[a] for a, b in zip(s, s[1:] + ' ')) lines = gutenberg_body('corpus/get-rich-quick-wallingford.txt') heads = [i for i, l in enumerate(lines) if re.fullmatch(r'CHAPTER [IVXL]+', l.strip())] assert [roman(lines[i].split()[1]) for i in heads] == list(range(1, 29)), [lines[i] for i in heads] stories = [] for n, a in enumerate(heads): body = lines[a + 1:(heads[n + 1] if n + 1 < len(heads) else len(lines))] k = next(j for j, l in enumerate(body) if l.strip()) title = [] while body[k].strip() and body[k].strip().upper() == body[k].strip(): title.append(body[k].strip()); k += 1 paras = paragraphs('\n'.join(body[k:]), drop=(r'THE END\.?', r'\[Illustration[^\]]*\]')) stories.append({'volume': VOLUME, 'title': ' '.join(title).capitalize(), 'words': sum(len(p.split()) for p in paras), 'text': '\n\n'.join(paras)}) write('.', stories)