Rewrite MadeTheirWayTransformer, add AggregateEventsTransformer.
Cat's Eye Technologies
9 years ago
239 | 239 | for event in incoming_events: |
240 | 240 | if events: |
241 | 241 | if event.initiator() == events[-1].initiator(): |
242 | if event.phrase.startswith('<1>'): | |
243 | event.phrase = '<he-1>' + event.phrase[3:] | |
242 | event.phrase = event.phrase.replace('<1>', '<he-1>') | |
244 | 243 | events.append(event) |
245 | 244 | else: |
246 | 245 | events.append(event) |
249 | 248 | |
250 | 249 | class MadeTheirWayToTransformer(Transformer): |
251 | 250 | def transform(self, editor, incoming_events): |
252 | # TODO: rewrite this to use Python's shift(), whatever that is | |
253 | incoming_events = list(reversed(incoming_events)) | |
254 | events = [] | |
255 | events.append(incoming_events.pop()) | |
256 | ||
257 | while incoming_events: | |
258 | consume_another_event = True | |
259 | while consume_another_event and incoming_events: | |
260 | consume_another_event = False | |
261 | event = incoming_events.pop() | |
262 | last_character = events[-1].initiator() | |
263 | if event.initiator() == last_character: | |
264 | ||
265 | # replace chains of 'went to' with 'made way to' | |
266 | if (events[-1].phrase in ('<1> went to <2>', '<he-1> went to <2>') and | |
267 | event.phrase == '<he-1> went to <2>'): | |
268 | assert event.location == event.participants[1] | |
269 | assert events[-1].location == events[-1].participants[1] | |
270 | events[-1].phrase = '<1> made <his-1> way to <2>' | |
271 | events[-1].participants[1] = event.participants[1] | |
272 | events[-1].location = event.participants[1] | |
273 | # ack | |
274 | events[-1].original_location = editor.character_location[last_character] | |
275 | consume_another_event = True | |
276 | elif (events[-1].phrase in ('<1> made <his-1> way to <2>', '<he-1> made <his-1> way to <2>') and | |
277 | event.phrase == '<he-1> went to <2>'): | |
278 | assert event.location == event.participants[1] | |
279 | assert events[-1].location == events[-1].participants[1] | |
280 | events[-1].phrase = '<1> made <his-1> way to <2>' | |
281 | events[-1].participants[1] = event.participants[1] | |
282 | events[-1].location = event.participants[1] | |
283 | consume_another_event = True | |
284 | elif (events[-1].phrase in ('<1> went to <2>', '<he-1> went to <2>') and | |
285 | event.phrase in ('<1> saw <2>', '<he-1> saw <2>')): | |
286 | # this *might* be better if we only do it when <1> | |
287 | # is the pov character for this paragraph. but it | |
288 | # does work... | |
289 | events[-1] = AggregateEvent( | |
290 | "%s, where %s", [events[-1], event], | |
291 | excl = event.excl) | |
292 | consume_another_event = True | |
293 | # and if they 'made their way' to their current location... | |
294 | # if editor.character_location[last_character] == events[-1].original_location: | |
295 | # events[-1].phrase = '<1> wandered around for a bit, then went back to <2>' | |
296 | ||
251 | events = [] | |
252 | for event in incoming_events: | |
253 | if (events and | |
254 | event.initiator() == events[-1].initiator()): | |
255 | if (events[-1].phrase in ('<1> went to <2>',) and | |
256 | event.phrase == '<1> went to <2>'): | |
257 | assert event.location == event.participants[1] | |
258 | assert events[-1].location == events[-1].participants[1] | |
259 | events[-1].phrase = '<1> made <his-1> way to <2>' | |
260 | events[-1].participants[1] = event.participants[1] | |
261 | events[-1].location = event.participants[1] | |
262 | elif (events[-1].phrase in ('<1> made <his-1> way to <2>',) and | |
263 | event.phrase == '<1> went to <2>'): | |
264 | assert event.location == event.participants[1] | |
265 | assert events[-1].location == events[-1].participants[1] | |
266 | events[-1].phrase = '<1> made <his-1> way to <2>' | |
267 | events[-1].participants[1] = event.participants[1] | |
268 | events[-1].location = event.participants[1] | |
269 | else: | |
270 | events.append(event) | |
271 | else: | |
272 | events.append(event) | |
273 | return events | |
274 | ||
275 | ||
276 | class AggregateEventsTransformer(Transformer): | |
277 | # replace "Bob went to the kitchen. Bob saw the toaster" | |
278 | # with "Bob went to the kitchen, where he saw the toaster" | |
279 | def transform(self, editor, incoming_events): | |
280 | events = [] | |
281 | for event in incoming_events: | |
282 | if events: | |
283 | if ( event.initiator() == events[-1].initiator() and | |
284 | events[-1].phrase in ('<1> went to <2>',) and | |
285 | event.phrase in ('<1> saw <2>',) ): | |
286 | # this *might* be better if we only do it when <1> | |
287 | # is the pov character for this paragraph. but it | |
288 | # does work... | |
289 | event.phrase = event.phrase.replace('<1>', '<he-1>') | |
290 | events[-1] = AggregateEvent( | |
291 | "%s, where %s", [events[-1], event], | |
292 | excl = event.excl) | |
293 | else: | |
294 | events.append(event) | |
295 | else: | |
296 | events.append(event) | |
297 | return events | |
298 | ||
299 | ||
300 | class DetectWanderingTransformer(Transformer): | |
301 | # not used yet | |
302 | # if they 'made their way' to their current location... | |
303 | def transform(self, editor, incoming_events): | |
304 | events = [] | |
305 | for event in incoming_events: | |
306 | if (event.phrase == '<1> made their way to <2>' and | |
307 | event.location == event.original_location): | |
308 | event.phrase = '<1> wandered around for a bit, then came back to <2>' | |
297 | 309 | events.append(event) |
298 | ||
299 | 310 | return events |
300 | 311 | |
301 | 312 | |
347 | 358 | editor = Editor(collector, self.characters) |
348 | 359 | editor.add_transformer(MadeTheirWayToTransformer()) |
349 | 360 | editor.add_transformer(DeduplicateTransformer()) |
361 | editor.add_transformer(AggregateEventsTransformer()) | |
362 | # editor.add_transformer(DetectWanderingTransformer()) | |
363 | # this one should be last, so prior transformers don't | |
364 | # have to worry themselved about looking for pronouns | |
350 | 365 | editor.add_transformer(UsePronounsTransformer()) |
351 | 366 | editor.publish() |
352 | 367 |