def landslip_search_challenge(self, tweet): data = super().ParseAndCheckTwitter(tweet.decode('utf-8')) # 格式转化 tweet_dict = [Status.NewFromJsonDict(x) for x in [data]][0] # 判断是否为demo账户 if tweet_dict.user.id != self.demo_acc_id: logger.error("Received not the demo account, the ID: ", tweet_dict.user.id) return # 清除redis信息 测试用 REDIS_CLIENT.delete(self.demo_acc_id) # 初始化需要回复的tweet id self.wait_reply_message_id = tweet_dict.id # 初始化需要回复的screen name 目前没用 self.screen_name = tweet_dict.user.screen_name # 匹配demo账户的id if tweet_dict.user.id == self.demo_acc_id: logger.info( "---------------------------------------- New tweet matched From Kafka Start -------------------------------------------" ) logger.info( "Tweet ID: {0} , Screen name: {1} Message text : {2}".format( tweet_dict.user.id, tweet_dict.user.screen_name, tweet_dict.text)) logger.info( "---------------------------------------- New tweet matched From Kafka End -------------------------------------------" ) self._message_entry(tweet_dict) # 启动监控监控回复线程 self.reply_monitor()
def add_value_location(self, location): logger.debug("Adding location info") if not REDIS_CLIENT.hexists(self.demo_acc_id, "location"): # 如果已经已经有的话更新,没有就创建 REDIS_CLIENT.hset(self.demo_acc_id, "location", json.dumps({"title": location})) else: info = REDIS_CLIENT.hget(self.demo_acc_id, "location") dict = json.loads(info) # 直接更新data dict['title'] = location REDIS_CLIENT.hset(self.demo_acc_id, "location", json.dumps(dict))
def nlp_datetime_processing(self, nlp): if REDIS_CLIENT.hget(self.demo_acc_id, "hazard_chat_status") is None: return elif REDIS_CLIENT.hget(self.demo_acc_id, "hazard_chat_status") == "end": return # 只要开始了对话就必须先提供时间日期 if 'datetime' not in nlp["entities"].keys() and \ REDIS_CLIENT.hget(self.demo_acc_id, "hazard_chat_status") == "start": logger.debug("datatime not in entities") return False # 判断类型 if 'type' in nlp["entities"]["datetime"][0]: # 指定日期以及 今天明天昨天关键词 if nlp["entities"]["datetime"][0]["type"] == "value": if 'value' in nlp["entities"]["datetime"][0]: date = nlp["entities"]["datetime"][0]['value'].split( 'T')[0] if not REDIS_CLIENT.hexists(self.demo_acc_id, "date"): # 如果已经已经有的话更新,没有就创建 REDIS_CLIENT.hset( self.demo_acc_id, "date", json.dumps({ "value": date, "type": "value" })) else: info = REDIS_CLIENT.hget(self.demo_acc_id, "date") dict = json.loads(info) # 直接更新data dict['value'] = date dict['type'] = "value" REDIS_CLIENT.hset(self.demo_acc_id, "date", json.dumps(dict)) # 一段时间 目前只支持输入 from 。。。 to ... if nlp["entities"]["datetime"][0]["type"] == "interval": if 'from' in nlp["entities"]["datetime"][0] and 'to' in nlp[ "entities"]["datetime"][0]: # 当前只取date date_from = nlp["entities"]["datetime"][0]["from"][ "value"].split('T')[0] date_to = nlp["entities"]["datetime"][0]["to"][ "value"].split('T')[0] if not REDIS_CLIENT.hexists(self.demo_acc_id, "date"): # 如果已经已经有的话更新,没有就创建 REDIS_CLIENT.hset( self.demo_acc_id, "date", json.dumps({ "from": date_from, "to": date_to, "type": "interval" })) else: info = REDIS_CLIENT.hget(self.demo_acc_id, "date") dict = json.loads(info) # 直接更新data dict['from'] = date_from dict['to'] = date_to dict['type'] = "interval" REDIS_CLIENT.hset(self.demo_acc_id, "date", json.dumps(dict))
def _question_match(self, seq, tweet_dict, nlp_res): logger.debug("Question sequence: {0} ".format(seq, )) # 第1个问题 地点 if seq == "1": logger.info("Mached question seq 1") # 获取信息中的地点信息 location_res = self.nlp_location_processing(nlp_res) # 没有地点信息 if location_res is not None: logger.debug("location_res is {0}".format(location_res, )) if not REDIS_CLIENT.hexists(self.demo_acc_id, "location_reply"): REDIS_CLIENT.hset(self.demo_acc_id, "location_reply", "1") return "Where did you observe this event ? " REDIS_CLIENT.hset(self.demo_acc_id, "question_seq", "2") # 第2个问题 时间 if REDIS_CLIENT.hget(self.demo_acc_id, "question_seq") == "2": logger.info("Mached question seq 2") # 获取信息中的地点信息 date_res = self.nlp_datetime_processing(nlp_res) if date_res is not None: logger.debug("date_res is {0}".format(date_res, )) if not REDIS_CLIENT.hexists(self.demo_acc_id, "date_reply"): REDIS_CLIENT.hset(self.demo_acc_id, "date_reply", "1") return "When did you observe this event?" return None # 回答完问题 切换下一个问题 REDIS_CLIENT.hset(self.demo_acc_id, "question_seq", "3") elif seq == "3": logger.info("Mached question seq 3") if not REDIS_CLIENT.hexists(self.demo_acc_id, "Q_2_1_reply"): REDIS_CLIENT.hset(self.demo_acc_id, "Q_2_1_reply", "1") return "Is it ‘raining’ where you observed the ‘leaning pole’?" # 收录回复 if REDIS_CLIENT.hexists(self.demo_acc_id, "Q_2_1_reply"): if self.check_Y_N_answer(tweet_dict.text): REDIS_CLIENT.hset(self.demo_acc_id, "Q_2_1_answer", tweet_dict.text) # 回答完问题 切换下一个问题 REDIS_CLIENT.hset(self.demo_acc_id, "question_seq", "4") logger.info("Q_2_1 finished") elif seq == "4": logger.info("Mached question seq 4") if not REDIS_CLIENT.hexists(self.demo_acc_id, "Q_2_2_reply"): REDIS_CLIENT.hset(self.demo_acc_id, "Q_2_2_reply", "1") return "Have you noticed any ‘rockfalls’ where you observed the ‘leaning pole’?" # 收录回复 if REDIS_CLIENT.hexists(self.demo_acc_id, "Q_2_2_reply"): if self.check_Y_N_answer(tweet_dict.text): REDIS_CLIENT.hset(self.demo_acc_id, "Q_2_2_answer", tweet_dict.text) # 回答完问题 切换下一个问题 REDIS_CLIENT.hset(self.demo_acc_id, "question_seq", "5") logger.info("Q_2_2 finished") elif seq == "5": logger.info("Mached question seq 4") if not REDIS_CLIENT.hexists(self.demo_acc_id, "Q_2_3_reply"): REDIS_CLIENT.hset(self.demo_acc_id, "Q_2_3_reply", "1") return "Is there ‘flooding’ in ‘Newcastle upon Tyne’?" # 收录回复 if REDIS_CLIENT.hexists(self.demo_acc_id, "Q_2_3_reply"): if self.check_Y_N_answer(tweet_dict.text): REDIS_CLIENT.hset(self.demo_acc_id, "Q_2_3_answer", tweet_dict.text) # 关闭问题 REDIS_CLIENT.hset(self.demo_acc_id, "hazard_chat_status", "end") REDIS_CLIENT.hset(self.demo_acc_id, "question_seq", "0") logger.info("Q_2_3 finished") return None