def on_wh_question(tmr): global clock refresh() question = grab_instance(Question, tmr) theme = question.theme.filler while theme.__class__ == ProtoEvent: theme = theme.object.filler if theme.at_least(Activity): if theme.at_least(Wh): acts = fr.kblookup('Activity') if acts: a = sorted(acts, key=lambda a: abs(a.time.filler.start - clock.quarters))[0] print '{0} is coming up next {1}.'.format(a.__class__.__name__, a.time.filler) else: print 'Either your agenda is clear or you are hiding something from me, %username%.' else: wh = filter(lambda f: f.at_least(Wh), map(lambda s: s.filler, theme.slots().values()))[0] if wh.at_least(Location): print 'You {0} at {1}, %username%.'.format(theme.__class__.__name__, ', and at '.join(map(lambda a: str(a.location.filler), fr.kblookup(theme.__class__.__name__)))) if wh.at_least(Time): print 'You {0} {1}, %username%.'.format(theme.__class__.__name__, ', and '.join(map(lambda a: str(a.time.filler), fr.kblookup(theme.__class__.__name__))))
def on_move(tmr): global clock global current_location global current_activity if current_activity: current_activity.time.filler.end = clock.quarters current_activity = None current_location = grab_instance(MoveEvent, tmr).to.filler refresh() clock.tick() print 'Moved to {0}'.format(current_location) loc_matches = filter(lambda loc: loc.longitude == current_location.longitude and loc.latitude == current_location.latitude, fr.kblookup('Location')) if loc_matches: current_location = loc_matches[0] if not current_location.__class__ is Location: print 'Aha! Checking-in at {0}'.format(current_location) else: fr.store(current_location)
def ask_define(what): global current_location global current_activity global clock if not fr.kblookup('DefineEvent'): define = DefineEvent() define.base.filler = what DefineEvent.definition.filler_class = what.__class__ fr.store(define, True) if what.__class__ == Location: print 'What is this place called?' elif what.__class__ == Activity: print 'What is that you are doing, %username%?'
def recover_context(concepts, instance = None): if instance: return ([instance.__class__] if not filter(lambda c: c.at_least(instance.__class__), concepts) else []) + sum([recover_context(concepts, filler) for filler in filter(lambda f: f, map(lambda s: s.filler, instance.slots().values()))], []) else: matches = fr.kblookup('DefineEvent') if matches and not filter(lambda c: c.at_least(DefineEvent), concepts): define = matches[0] return filter(lambda c: c != define.base.filler.__class__, recover_context(concepts, define)) + [Concept] if filter(lambda c: c.at_least(Activity), concepts): additional = [] if not filter(lambda c: c.at_least(Location), concepts): additional += [Location] if not filter(lambda c: c.at_least(Time), concepts): additional += [Time] if not filter(lambda c: c.at_least(Person), concepts): additional += [Person] return additional return []
def on_define(tmr): definition = grab_instance(DefineEvent, tmr).definition.filler define = fr.kblookup('DefineEvent')[0] base = define.base.filler if definition.at_least(Location): global current_location definition.longitude = base.longitude definition.latitude = base.latitude definition.stay = base.stay current_location = definition # need to remove because we already remember this location under a different concept fr.forget(define.base.filler) if definition.at_least(Activity): global current_activity definition.location = base.location current_activity = definition fr.store(definition) fr.forget(define) DefineEvent.definition.filler_class = Concept #DefineEvent.base.filler_class = Concept refresh()
def observe(tmr): global clock global current_location global current_activity wake = grab_instance(AgentWakeEvent, tmr) if wake: current_location = wake.where.filler fr.store(current_location) else: current_location.stay += 1 refresh() clock.tick() print 'Staying at {0} for {1} quarters'.format(current_location, current_location.stay) acts = fr.kblookup('Activity') if acts: a = sorted(acts, key=lambda a: abs(a.time.filler.start - clock.quarters))[0] if abs(a.time.filler.start - clock.quarters) < 5: print 'Hurry, %username%, {0} is starting at {1} in less than an hour!'.format(a.__class__.__name__, a.location.filler) return # if idle at an unknown loc if current_location.stay > 3 and current_location.__class__ == Location: print 'You seem to hang out here a lot.' ask_define(current_location) return # if idle with no known activity if current_location.stay > 7 and not current_activity: init_activity() print 'I see you\'re doing something.' ask_define(current_activity) return # if idle with a known activity for too long if current_location.stay > 96 and current_activity: print 'Wow, you seem really into it. Are you still doing {0}, %username%?'.format(current_activity) return
def refresh(): os.system(['clear','cls'][os.name == 'nt']) print 'FR:' primed = map(str, fr.kblookup('DefineEvent')) if primed: print 'Primed:' pprint(primed) locs = map(str, fr.kblookup('Location')) if locs: print 'Known Locations:' pprint(locs) acts = map(str, fr.kblookup('Activity')) if acts: print 'Known Activities:' pprint(acts) ppl = map(str, fr.kblookup('Person')) if ppl: print 'Known Persons:' pprint(ppl)
def senses(self, *args): senses = super(Lexicon, self).senses(*args) matches = fr.kblookup('DefineEvent') if senses != None or not matches: return senses else: lemma = args[0] define = matches[0] if lemma == 'it' or lemma == 'this': return [define.base.filler.__class__] if not define.definition.filler and lemma != '.': new_concept = ConceptType(lemma.capitalize(), (define.base.filler.__class__,), {}) define.definition.filler = new_concept() self.lexicon[lemma] = {} self.lexicon[lemma]['NOPOS'] = [new_concept] return [new_concept]