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):
     #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 _deal_last_question(self, session_id_in):
     last_question, last_answer, last_sentence_pattern_classification = _get_last_question(session_id_in)
     classification = self._pattern_classification(last_question)
     if classification == classification_name.query_product.value:
         label2entities_cur_dict, words_timestap = self._get_entity_from_last_answer(last_answer)
         # self._slots_maintain_class.update_slots(label2entities_cur_dict, session_id_in)
         usermanager.get_user(session_id_in).update_slots(-1, **{usname.slots_answer.value: label2entities_cur_dict})
     else:
         pass
Exemple #4
0
    def action(self, sessionid_in):
        str_in = self._usermanager.get_user(
            sessionid_in).__dict__['str_synonym']
        slots_instance = usermanager.get_user(
            sessionid_in).get_instance_by_index(-1)
        # chatRecordList = getQAPairs(sessionid_in + _key_only_question_jdpin_with, _answer_type)
        last_question = slots_instance['question']
        if last_question:
            label2entities_cur_dict, words_timestap = self._pronoun_deal_class.get_entity_from_strs(
                [str_in], dcname.alldomain.value)
            label2entities_history_dict = self._pronoun_deal_class.get_label2entity_from_history(
                sessionid_in, dcname.alldomain.value)

            src_list = label2entities_history_dict.get(cname.domain.value)
            dst_list = label2entities_cur_dict.get(cname.domain.value)
            if src_list and dst_list:
                # cur_question_rewrite = last_question.replace(src_list[-1], dst_list[-1])
                self._usermanager.update(sessionid_in,
                                         str_synonym=last_question)
                return [src_list[-1]] if src_list else [], {
                    cname.domain.value: [dst_list[-1]] if dst_list else []
                }
            else:
                pass
        else:
            print('inherit_from_last_sentence 上文中沒有繼承到任何句子')
            last_question = ''
        return [], {}
    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 deal_interaction(self, sessionid_in, label2entities_cur_dict_in,
                      str_in):
     user_cur = usermanager.get_user(sessionid_in)
     domain_classification = user_cur['domain_classification']
     product_names = self._entity_class.get_domain2intent2std2sim_dict[
         domain_classification][cname.domain.value]
     product2str_dict = [{
         _tmp_for_show_str.get(product_iter, product_iter):
         product_iter + str_in
     } for product_iter in product_names]
     return 1, _default_name_abbrev, product2str_dict
Exemple #7
0
 def action(self, sessionid_in):
     str_in = usermanager.get_user(sessionid_in).__dict__['str_synonym']
     domain_classification = usermanager.get_user(
         sessionid_in).__dict__['domain_classification']
     slots_dict = collections.defaultdict(list)
     for index_iter in self._indexes:
         slots_instance = usermanager.get_user(
             sessionid_in).get_slots_by_index(domain_classification,
                                              index_iter)
         for k, v in slots_instance.items():
             if v:
                 slots_dict[k].append(v)
     if cname.domain.value in slots_dict:
         slots_dict.pop(cname.domain.value)
     slots_dict_only_one = collections.defaultdict(list)
     for k, v in slots_dict.items():
         if v:
             slots_dict_only_one[k] = v[0]
         else:
             slots_dict_only_one[k] = []
     return [], slots_dict_only_one
Exemple #8
0
def _deal_interaction(sessionid_in, strs_get_entity_from, str_cur):
    user_cur = usermanager.get_user(sessionid_in)
    last_label2entities_cur_dict, last_words_timestap = _get_entity_from_strs(
        strs_get_entity_from, user_cur['domain_classification'])
    abbrev2std_list = list()
    for entity_iter in last_label2entities_cur_dict[cname.domain.value]:
        str_iter = entity_iter + str_cur
        # todo 暂时,为了显示
        # abbrev2std_list.append({entity_iter: str_iter})
        abbrev2std_list.append(
            {_tmp_for_show_str.get(entity_iter, entity_iter): str_iter})
    # mylog.info('获取到的abbrev2std_list\t{}'.format(str(abbrev2std_list)))
    return abbrev2std_list
Exemple #9
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
    def _classification_sentence_pattern(self, sessionid_in,str_in):
        user_cur=usermanager.get_user(sessionid_in)
        classification = self._pattern_classification(str_in)
        if classification == classification_name.bottom_no_inherit.value:
            str_synonym, _ = self._preprocess_class.process(str_in)
            classification = self._pattern_classification(str_synonym)

        if _environ=='online':
            sentence_pattern_classification_from_kg = _get_sentence_pattern_from_kg(sessionid_in, user_cur['str_raw'])
            if sentence_pattern_classification_from_kg:
                # user_cur['sentence_pattern_classification'] = sentence_pattern_classification_from_kg
                classification=sentence_pattern_classification_from_kg
            else:
                pass
        return classification
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 judge_interaction(self, sessionid_in, label2entities_cur_dict_in,
                       str_in):
     user_cur = usermanager.get_user(sessionid_in)
     #{'dst_word_to_fufill':dst_word_to_fufill,'label2entities_cur_dict':label2entities_cur_dict})
     dst_word_to_fufill = user_cur['dst_word_to_fufill']
     label2entities_cur_dict = user_cur['label2entities_cur_dict']
     str_synonym_cut = user_cur['str_synonym_cut']
     str_raw_cut = user_cur['str_raw_cut']
     domain_classification = user_cur['domain_classification']
     intent_num = 0
     if cname.property.value in dst_word_to_fufill:
         intent_num += len(dst_word_to_fufill[cname.property.value])
     if cname.domain.value in dst_word_to_fufill:
         intent_num += len(dst_word_to_fufill[cname.domain.value])
     if cname.property.value in label2entities_cur_dict:
         intent_num += len(label2entities_cur_dict[cname.property.value])
     if cname.domain.value in label2entities_cur_dict:
         intent_num += len(label2entities_cur_dict[cname.domain.value])
     if intent_num == 1 and len(
             str_raw_cut
     ) == 1 and domain_classification in self._domain_areas:
         return True
     else:
         return False
 def get_label2entity_from_history(self, sessionid_in, domain_classification_in):
     """从历史数据中获取 label 对应的  实体词"""
     return usermanager.get_user(sessionid_in).get_slots_by_field(domain_classification_in)