Extract the story and summary from a story file. Arguments: raw_story (str): content of the story file as an utf-8 encoded string. Raises: IndexError: If the story is empty or contains no highlights.
(raw_story)
| 60 | |
| 61 | |
| 62 | def process_story(raw_story): |
| 63 | """ Extract the story and summary from a story file. |
| 64 | |
| 65 | Arguments: |
| 66 | raw_story (str): content of the story file as an utf-8 encoded string. |
| 67 | |
| 68 | Raises: |
| 69 | IndexError: If the story is empty or contains no highlights. |
| 70 | """ |
| 71 | nonempty_lines = list(filter(lambda x: len(x) != 0, [line.strip() for line in raw_story.split("\n")])) |
| 72 | |
| 73 | # for some unknown reason some lines miss a period, add it |
| 74 | nonempty_lines = [_add_missing_period(line) for line in nonempty_lines] |
| 75 | |
| 76 | # gather article lines |
| 77 | story_lines = [] |
| 78 | lines = deque(nonempty_lines) |
| 79 | while True: |
| 80 | try: |
| 81 | element = lines.popleft() |
| 82 | if element.startswith("@highlight"): |
| 83 | break |
| 84 | story_lines.append(element) |
| 85 | except IndexError: |
| 86 | # if "@highlight" is absent from the file we pop |
| 87 | # all elements until there is None, raising an exception. |
| 88 | return story_lines, [] |
| 89 | |
| 90 | # gather summary lines |
| 91 | summary_lines = list(filter(lambda t: not t.startswith("@highlight"), lines)) |
| 92 | |
| 93 | return story_lines, summary_lines |
| 94 | |
| 95 | |
| 96 | def _add_missing_period(line): |