"""Split the Project Gutenberg text of George and Weedon Grossmith, *The Diary of a Nobody* (1892, eBook #1026), one story per chapter. It is one continuous diary, a clerk's life at home and in the office, so chapters are episodes of one world rather than separate tales. The body heads each chapter with `CHAPTER ` and then a one-paragraph synopsis, which is dropped: it summarises the chapter and would be read as a scene.""" import re, sys sys.path.insert(0, '..') from gutenberg_chunks import gutenberg_body, paragraphs, write VOLUME = 'The Diary of a Nobody (George and Weedon Grossmith, 1892)' 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/diary-of-a-nobody.txt') heads = [i for i, l in enumerate(lines) if re.fullmatch(r'CHAPTER [IVXL]+\.?', l.strip())] assert [roman(lines[i].split()[1].rstrip('.')) for i in heads] == list(range(1, len(heads) + 1)), [lines[i] for i in heads] stories = [] for n, a in enumerate(heads): paras = paragraphs('\n'.join(lines[a + 1:(heads[n + 1] if n + 1 < len(heads) else len(lines))]), drop=(r'THE END\.?', r'\[Illustration[^\]]*\]')) synopsis, paras = paras[0], paras[1:] stories.append({'volume': VOLUME, 'title': f'Chapter {n + 1}: {synopsis[:70]}', 'synopsis': synopsis, 'words': sum(len(p.split()) for p in paras), 'text': '\n\n'.join(paras)}) write('.', stories)