git @ Cat's Eye Technologies The-Swallows / 423f0d6
Add AggregateEvents, and demonstrate that they work (silly-ly.) Cat's Eye Technologies 9 years ago
1 changed file(s) with 57 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
5252 def initiator(self):
5353 return self.participants[0]
5454
55 def __str__(self):
55 def render(self):
5656 phrase = self.phrase
5757 i = 0
5858 for participant in self.participants:
6464 phrase = phrase.replace('<was-%d>' % (i + 1), participant.was())
6565 phrase = phrase.replace('<is-%d>' % (i + 1), participant.is_())
6666 i = i + 1
67 return phrase
68
69 def __str__(self):
70 phrase = self.render()
71 if self.excl:
72 phrase = phrase + '!'
73 else:
74 phrase = phrase + '.'
75 return phrase[0].upper() + phrase[1:]
76
77
78 class AggregateEvent(Event):
79 """Attempt at a way to combine multiple events into a single
80 sentence. Each constituent event must have the same initiator.
81
82 """
83 def __init__(self, template, events, excl=False):
84 self.template = template
85 self.events = events
86 self.excl = excl
87 self.phrase = 'SEE SUBEVENTS PLZ'
88 self._initiator = self.events[0].initiator()
89 for event in self.events:
90 assert event.initiator() == self._initiator
91 self.location = self._initiator.location
92
93 def rephrase(self, new_phrase):
94 #raise NotImplementedError
95 return self
96
97 def initiator(self):
98 return self._initiator
99
100 def __str__(self):
101 phrase = self.template % tuple([x.render() for x in self.events])
67102 if self.excl:
68103 phrase = phrase + '!'
69104 else:
160195
161196 # update our idea of where the character is, even if these are
162197 # not events we will be dumping out
163 self.character_location[event.participants[0]] = event.location
198 self.character_location[event.initiator()] = event.location
164199
165200 if event.location == self.character_location[pov_actor]:
166201 paragraph_events.append(event)
167202 # update the reader's idea of where the character is
168 self.last_seen_at[event.participants[0]] = event.location
203 self.last_seen_at[event.initiator()] = event.location
169204
170205 return paragraph_events
171206
193228 while consume_another_event and incoming_events:
194229 consume_another_event = False
195230 event = incoming_events.pop()
196 last_character = events[-1].participants[0]
197 if event.participants[0] == last_character:
198
231 last_character = events[-1].initiator()
232 if event.initiator() == last_character:
233
199234 # replace repeated proper nouns with pronouns
200235 if event.phrase.startswith('<1>'):
201236 event.phrase = '<he-1>' + event.phrase[3:]
251286 def publish_chapter(self, chapter_num):
252287 collector = EventCollector()
253288
254 for actor in self.characters:
255 actor.collector = collector
289 for character in self.characters:
290 character.collector = collector
256291 # don't continue a conversation from the previous chapter, please
257 actor.topic = None
258 actor.place_in(random.choice(self.setting))
292 character.topic = None
293 character.place_in(random.choice(self.setting))
294
295 # just testing
296 for character in self.characters:
297 character.collector.collect(AggregateEvent(
298 "%s, then %s",
299 [
300 Event("<1> looked at <his-1> shoes", [character]),
301 Event("<1> looked at the sky", [character]),
302 ],
303 excl=True))
259304
260305 while len(collector.events) < self.events_per_chapter:
261 for actor in self.characters:
262 actor.live()
306 for character in self.characters:
307 character.live()
263308 #print len(collector.events) # , repr([str(e) for e in collector.events])
264309
265310 # this contains duplicates because we are producing duplicates in