def judge_interaction(self, sessionid_in, label2entities_cur_dict_in, str_in): #todo 添加 领域判断 jdpin_id = usermanager.get_user(sessionid_in)['jdpin_id'] sentence_classification = usermanager.get_user( sessionid_in)['sentence_pattern_classification'] key_name = jdpin_id + _flag_recomm_sent_pattern # print('key_name==>',key_name) # last_question, last_answer,last_sentence_pattern_classification = _get_last_question(sessionid_in) last_question, last_answer, last_sentence_pattern_classification = _get_last_question_info( sessionid_in, 'none') # sc_cur=self._sentence_classification_class.classification_accurate(last_question) # print('rds.get(key_name)',) # print(rds.keys()) # val_raw=rds.get(key_name) # if val_raw: # print(eval(val_raw)) # val_flag=bytes(val_raw,encoding='utf-8') # if eval(val_raw) not in [0,'0']: # print('val_flag==>',val_raw) if last_sentence_pattern_classification == pcname.query_product.value and sentence_classification in self._included_classification: mylog.info('RecomLastQuestionMultiProductInteraction return True') return True else: pass #todo 后续再打开 # val_raw=rds.get(key_name) # if val_raw: # flag_raw=eval(val_raw) # if flag_raw and flag_raw==1 : # print('测试检测到 redis 中上一句 是 推荐句式') # mylog.info('测试检测到 redis 中上一句 是 推荐句式\t keyname={}'.format(str(key_name))) # return True return False
def judge_interaction(self, sessionid_in, label2entities_cur_dict_in, str_in): #todo 添加 领域判断 user_cur = usermanager.get_user(sessionid_in) # last_question, last_answer,last_sentence_pattern_classification = _get_last_question(sessionid_in) last_question, last_answer, last_sentence_pattern_classification = _get_last_question_info( sessionid_in, flag_preprocess='question') last_label2entities_cur_dict, last_words_timestap = self._get_entity_from_strs( [last_question], user_cur['domain_classification']) mylog.info('last_question\t{}'.format(str(last_question))) mylog.info( 'last_label2entities_cur_dict[cname.domain.value\t{}'.format( str(last_label2entities_cur_dict[cname.domain.value]))) mylog.info('label2entities_cur_dict_in[cname.domain.value\t{}'.format( str(label2entities_cur_dict_in[cname.domain.value]))) mylog.info('sentence_pattern_classification\t{}'.format( str(user_cur['sentence_pattern_classification']))) if len(last_label2entities_cur_dict[cname.domain.value]) > 1 and not label2entities_cur_dict_in['domain'] and \ user_cur['sentence_pattern_classification'] in self._sentence_classifications_needed: mylog.info('LastQuestionMultiProductInteracton return True') return True else: return False
def replace_with_context(self, sessionid_in): self._deal_last_question(sessionid_in) # todo 添加分类 user_cur = usermanager.get_user(sessionid_in) # todo 添加当前槽位切换 domain_classification = self._domain_classification_class.classify(user_cur['str_synonym']) mylog.info('domain_classification:\t\t{}'.format(domain_classification)) usermanager.update(sessionid_in, domain_classification=domain_classification) # usermanager.get_user(sessionid_in).update_slots(index=-1,sentence_pattern_classification=sentence_pattern_classification_from_kg) # self._usermanager_class.update(sessionid_in,{'str_raw':str_in,'str_synonym':str_synonym, # 'domain_classification':domain_classification,'str_synonym_cut':str_synonym_cut}) words_list_out, dst_word_to_fufill, label2entities_cur_dict = self.replace_with_context_only_question( sessionid_in) # print('words_list_out, dst_word_to_fufill, label2entities_cur_dict==>',words_list_out, dst_word_to_fufill, label2entities_cur_dict) flag_inter, abbrev_str, abbrev2std_list = self._fuzzy_interaction_class.deal_fuzzy_interaction(sessionid_in, label2entities_cur_dict, user_cur[ 'str_raw'], words_list_out) usermanager.get_user(sessionid_in).insert_slots( **{'question': user_cur['str_synonym'], 'slots_question': label2entities_cur_dict, 'domain_classification': user_cur['domain_classification'], 'sentence_pattern_classification': user_cur['sentence_pattern_classification']}) rds.set(user_cur['jdpin_id'] + _flag_recomm_sent_pattern, 0) return words_list_out, dst_word_to_fufill, label2entities_cur_dict, flag_inter, abbrev_str, abbrev2std_list
def judge_interaction(self, sessionid_in, label2entities_cur_dict_in, str_in): if not label2entities_cur_dict_in[ 'domain'] and self._infer_abbrev2std_class.bool_abbrevation( str_in): mylog.info('CurStrAbbrevInteraction return True') return True else: return False
def deal_interaction(self, sessionid_in, label2entities_cur_dict_in, str_in): # last_question, last_answer,last_sentence_pattern_classification = _get_last_question(sessionid_in) # last_question_rewrite,_=self._preprocess_class.process(last_question) last_question, last_answer, last_sentence_pattern_classification = _get_last_question_info( sessionid_in, flag_preprocess='question') mylog.info('deal_interaction:\t{}'.format(str(last_question))) abbrev2std_list = _deal_interaction(sessionid_in, [last_question], str_in) return 1, _default_name_abbrev, abbrev2std_list
def process(self, str_in): str_clean_se = self._clean_start_end(str_in) str_clean_punc = self._clean_punc(str_clean_se) str_std = self._similiar_sentences_replace(str_clean_punc) # str_std=str_clean_punc words = jieba.lcut(str_std) words_replaced = self._synonym_replace(words) words_number_transed = self._chinese_number2arab_number(words_replaced) str_synonym = ''.join(words_number_transed) str_synonym = self._reback_replace(str_synonym, str_in) words = self._cut_words(str_synonym) mylog.info('当前class\t{}\t\t 当前函数{}\n 输入str\t,输出str\t'.format( str(self.__class__), 'process', str_in, str_synonym)) return str_synonym, words
def request_post_kg(str_in, jdpin_in): # params = {"question": "我要去潜水能买什么产品?", "jdPin": "安行万里保不保外国人"} params = {"question": str_in, "jdPin": jdpin_in} status = 0 try: result = requests.post(_url_kg_slots, json=params, timeout=0.5).json() status = 1 mylog.info('请求kg 返回结果\t{}'.format(str(result))) except Exception as e: error_str = 'request_kg 错误信息{}\t url\t{}字段\t{}'.format( str(e.args), str(_url_kg_slots), str(params)) mylog.debug(error_str) mylog.info(error_str) result = {} return status, result
def _get_sentence_pattern_from_kg(sessionid_in, str_in): jdpin = usermanager.get_user(sessionid_in)['jdpin_id'] status, result = request_post_kg(str_in, jdpin) sentence_pattern_classification_recommend = '' if status == 1: if _key_kg_sentence_classificiation in result: sentence_pattern_classification = result[_key_kg_sentence_classificiation] if sentence_pattern_classification == _value_kg_sentence_classificiation_recommendation: mylog.info('从图谱中获取句式\t{}'.format(sentence_pattern_classification)) sentence_pattern_classification_recommend = pcname.query_product.value else: pass else: pass return sentence_pattern_classification_recommend
def deal_interaction(self, sessionid_in, label2entities_cur_dict_in, str_in): user_cur = usermanager.get_user(sessionid_in) last_question, last_answer, last_sentence_pattern_classification = _get_last_question( sessionid_in) jdpin_id = user_cur['jdpin_id'] last_answer_rewrite, _ = self._preprocess_class.process(last_answer) mylog.info( 'jdpin\t{}\t获取到的last_question\t{},\n last_answer{}\t'.format( str(jdpin_id), str(last_question), str(last_answer_rewrite))) abbrev2std_list = _deal_interaction(sessionid_in, [last_answer_rewrite], str_in) if abbrev2std_list: mylog.info('deal_interaction:\t{}'.format( str(last_answer_rewrite))) return 1, _default_name_abbrev, abbrev2std_list else: return 0, '', []
def replace_with_context_only_question(self, sessionid_in): """ 根据历史对话,从历史对话提取实体,根据当前str中是否存在指代词,决定 实体是补充还是替换 args: str_in: 输入字符串 words_list_in:输入字符串分词结果 jdpin_in: id return: words_list_out: 补全后的分词列表 dst_word_out: 从历史对话记录提取的slots label2entities_cur_dict: 当前用户话提取的slots,准备存入列表的slots """ user_cur = self._usermanager_class.get_user(sessionid_in) sentence_classification = self._classification_sentence_pattern(sessionid_in,user_cur['str_raw']) mylog.info('sentence_classification:\t\t{}\tstr_raw\t{}'.format(sentence_classification, user_cur['str_raw'])) self._usermanager_class.update(sessionid_in, sentence_pattern_classification=sentence_classification) src_prounoun_word, dst_word_to_fufill = self._get_prounoun_and_dst_words(sessionid_in) label2entities_cur_dict, words_timestap = self._prounoun_class.get_entity_from_strs([user_cur['str_synonym']], user_cur[ 'domain_classification']) words_list_out = self.replace_words_list(user_cur['str_synonym_cut'], src_prounoun_word, dst_word_to_fufill) usermanager.update(sessionid_in,**{'dst_word_to_fufill':dst_word_to_fufill,'label2entities_cur_dict':label2entities_cur_dict}) return words_list_out, dst_word_to_fufill, label2entities_cur_dict
def action_with_fuzzy(self, sessionid_in): # 同义句替换,先分析当前句子实体词情况,再决定是否模糊匹配 user_cur = self._usermanager.get_user(sessionid_in) src_prounoun_word, dst_word_to_fufill, label2entities_cur_dict = \ self._prounoun_class.replace_words(sessionid_in) property_value = label2entities_cur_dict[cname.property.value] domain_list = label2entities_cur_dict[cname.domain.value] if not property_value: if domain_list: domain_str = domain_list[0] str_no_domain = user_cur['str_synonym'].replace(domain_str, '') else: domain_str = '' str_no_domain = user_cur['str_synonym'] strs_set, scores_set = self._similarmodel_class.similar_threshold( str_no_domain) if strs_set: # print('recall similarity===>',words_set) mylog.info('similar_threshold str_no_domain\t\t{}'.format( str(str_no_domain))) mylog.info('similar_threshold words_set\t\t{}'.format( str(strs_set))) mylog.info('similar_threshold scores_set\t\t{}'.format( str(scores_set))) str_similar = strs_set[0] # str_similar = ''.join(words_similar) str_similar_with_domain = domain_str + str_similar str_synonym, str_synonym_words = self._preprocess_class.process( str_similar_with_domain) self._usermanager.update(sessionid_in, str_synonym=str_synonym, str_synonym_cut=str_synonym_words) src_prounoun_word, dst_word_to_fufill, label2entities_cur_dict = self._prounoun_class.replace_words( sessionid_in) else: # if '是多久' in str_no_domain: # print('str_no_domain==>',str_no_domain) if self._depend_analysis_class.judge_complete(str_no_domain): dst_word_to_fufill = { cname.domain.value: dst_word_to_fufill[cname.domain.value] } else: pass else: pass return src_prounoun_word, dst_word_to_fufill
def GET(self, name): # print('name==>',name) web.header('Content-Type', 'application/json,charset=UTF-8') ts = time.time() input_ = web.input(name=None) # for key,value in input_.items(): # print(key,value) # data_return={'status':0,'data':{},'msg':None} # print('input==>',input) _data_return = self._response_manage_class.distribute(name_in=name, input_in=input_) mylog.info('输入字段信息{}'.format(str(input_))) mylog.info('get 返回字段信息 {}\n'.format(str(_data_return))) cost_time = time.time() - ts mylog.info('此次请求耗时\t{}'.format(str(cost_time))) return json.dumps(_data_return)
def deal_fuzzy_interaction(self, sessionid_in, label2entities_cur_dict_in, str_in, words_list_out): # todo 改成 class,不同的情况不同的交互 if self.no_interaction.judge_interaction(sessionid_in, label2entities_cur_dict_in, str_in): mylog.info('no_interaction.judge_interaction return True') pass else: for class_obj_iter in self.interactions_instances: if class_obj_iter.judge_interaction( sessionid_in, label2entities_cur_dict_in, str_in): flag_inter, abbrev, abbrev2std_list = class_obj_iter.deal_interaction( sessionid_in, label2entities_cur_dict_in, str_in) mylog.info(' {} return True'.format(str(class_obj_iter))) return flag_inter, abbrev, abbrev2std_list else: pass mylog.info('交互反问模块都没有return True') return 0, '', []
def _get_last_question(sessionid_in): jdpin_id = usermanager.get_user(sessionid_in)['jdpin_id'] instance = usermanager.get_user(sessionid_in).get_instance_by_index(-1) last_sentence_pattern_classification = '' if instance: last_question = instance.question last_sentence_pattern_classification = instance.sentence_pattern_classification # print('last_question==>',last_question) chatRecordList = getQAPairs(jdpin_id) if chatRecordList: last_answer = chatRecordList[-1].answer last_question = chatRecordList[-1].question usermanager.get_user(sessionid_in).update_slots( -1, **{'answer': last_answer}) # mylog.info('获取 上一轮 question\t{}\n,answer\t{}\n 从 redis'.format(str(last_question),str(last_answer))) # mylog.info('redis中5轮问题 开始') # for chatrecord_iter in chatRecordList: # question_iter=chatrecord_iter.question # answer_iter=chatrecord_iter.answer # mylog.info('redis 迭代 question\t{}\t\tanswer\t{}'.format(question_iter,answer_iter)) # mylog.info('redis中5轮问题 开始') else: mylog.info( '_get_last_question 获取到instance,但是没有chatRecordList,last_question={}' .format(last_question)) last_answer = '' else: mylog.info('_get_last_question 没有获取到instance') last_question, last_answer = '', '' mylog.info( '从redis获取到的last_question\t{},\n last_answer\t{},\nlast_sentence_pattern_classification\t{}' .format(last_question, last_answer, last_sentence_pattern_classification)) return last_question, last_answer, last_sentence_pattern_classification