def init(self): self.name = "VariableFinder" TripsModule.init(self) self.send(KQMLPerformative.from_string("(subscribe :content (request &key :content (find-var . *)))")) self.send(KQMLPerformative.from_string("(subscribe :content (request &key :content (find-code . *)))")) self.trips_base = os.environ['TRIPS_BASE'] resources_path = self.trips_base + '/etc/VariableFinder/resources' self.var_tagger = WMVariable(resources_path=resources_path) self.ready()
def receive_tell(self, msg, content): if not isinstance(content, KQMLList): self.error_reply(msg, "expected :content to be a list") return verb = content[0].to_string().lower() if verb == "i-am-here": if self.iam_here_flag: rep_msg = KQMLPerformative.from_string(self.definition_message) self.iam_here_flag = False self.reply(msg, rep_msg) return
def receive_request(self, msg, content): if not isinstance(content, KQMLList): self.error_reply(msg, "expected :content to be a list") return verb = content[0].to_string().lower() if verb == "restart": self.iam_here_flag = True self.send( KQMLPerformative.from_string( "(request :content (ARE-YOU-THERE :who CWMSAGENT)))")) return if verb == "find-var": phrase, top, thresh = self.parse_args(content) if phrase is None: self.error_reply(msg, 'There should be a :phrase slot') return if top is None: top = 1 if thresh is None: thresh = 0.95 results = self.var_tagger.find_variables(phrase, thresh, top) self.create_result(results, is_code=False, msg=msg) elif verb == "find-code": phrase, top, thresh = self.parse_args(content) if phrase is None: self.error_reply(msg, 'There should be a :phrase slot') return if top is None: top = 1 if thresh is None: thresh = 0.95 results = self.var_tagger.find_codes(phrase, thresh, top) self.create_result(results, is_code=True, msg=msg) else: self.error_reply(msg, "unknown request verb " + verb) return
def create_result(self, result, is_code, msg): """ Reply to msg with results from result; is_code indicates this was a find-code request; otherwise it's a find-var request. """ reply_content = KQMLList('ANSWER') for ky_mp in result: score = ky_mp['score'] variable = KQMLList() match = KQMLPerformative('match') if is_code: for k, v in ky_mp['variable'].items(): kqml_v = v if isinstance(v, str): kqml_v = self.kqml_normalize(v) elif isinstance(v, list): kqml_v = KQMLList([self.kqml_normalize(e) for e in v]) variable.add(KQMLList([self.kqml_normalize(k), kqml_v])) code = KQMLList() for k, v in ky_mp['code'].items(): code.add( KQMLList( [self.kqml_normalize(k), self.kqml_normalize(v)])) match.set(':code', code) else: for k in [ 'DSSAT', 'ICASA', 'TRIPS', 'FAO', 'TOPOFLOW', 'UN', 'WDI' ]: if k not in ky_mp: continue v = ky_mp[k] variable.add( KQMLList( [self.kqml_normalize(k), self.kqml_normalize(v)])) match.set(':variable', variable) match.set(':score', score) reply_content.add(match) reply_msg = KQMLPerformative("reply") reply_msg.set(":content", reply_content) self.reply(msg, reply_msg)