def execute_command(self, mecab, message): fun_talk = [] # TODO : 개선 fun_talk.append('흠..그럼 컬투쇼 사연 하나 소개해 드릴께요ㅋㅋ') fun_talk.append(PresenTALKUtil.get_random_message('fun_talk')) fun_talk.append(PresenTALKUtil.get_random_message('laugh')) fun_talk.append(PresenTALKUtil.get_random_message('apologize')) return_dict = {'command': 'fun_talk', 'message': fun_talk} return return_dict
def __init__(self): # 챗봇 종료 self.isFinish = False # 형태소 분석기 self.mecab = Mecab() # config self.intent_analyzer = intent_analyzer.IntentAnalyzer() # 사용자 생성 self.user = User() # 대화 내용 list self.conversation_list = [] # 토픽 list ( 기본 정의 ) self.topic_list = PresenTALKUtil.get_topic_list() # 룰 딕셔너리 ( 특정 토픽에 대한 룰 ) self.rule_list = {} # 룰 리스트 ( 모든 토픽에 대한 룰 ) # Topic 을 찾지 못했을 때는, 모든 rule list 에 대해서 전부 매칭을 진행 -> 이건 추후 변경 여지가 있음 -> Proto Type 에 대해서는 이렇게 진행하고자 함 self.all_rule_list = [] # feature 딕셔너리 # 일단 feature 를 세 개로 지정 -> 추후 늘리거나 추천 알고리즘에 맞게 적용 self.feature_dict = {'object': '', 'age': '', 'purpose': ''} # feature 딕셔너리 후보 ( 일상대화에서 feature 후보군이 나올수도 있음 ) self.feature_candidate_dict = {'object': '', 'age': '', 'purpose': ''} # Rule list self.temp_rule_list = PresenTALKUtil.get_temp_rule_list() for rule_row in self.temp_rule_list: topic_id = rule_row['topic_id'] if topic_id in self.rule_list.keys(): temp_rule_dict = { 'matching_rule': rule_row['matching_rule'], 'response_rule': rule_row['response_rule'] } self.rule_list[topic_id].append(temp_rule_dict) else: temp_rule_list = [] temp_rule_dict = { 'matching_rule': rule_row['matching_rule'], 'response_rule': rule_row['response_rule'] } temp_rule_list.append(temp_rule_dict) self.rule_list[topic_id] = temp_rule_list self.all_rule_list.append(temp_rule_dict)
def get_user_info_message(self): return_message = '' for key in self.user.user_info.keys(): if self.user.user_info[key] == '': return_message = PresenTALKUtil.get_random_message(key) break return return_message
def print_feature_message(self): return_feature_dic = {'feature_key': '', 'feature_message': ''} for key in self.feature_dict.keys(): if self.feature_dict[key] == '': return_feature_dic['feature_key'] = key return_feature_dic[ 'feature_message'] = PresenTALKUtil.get_random_message(key) break return return_feature_dic
def analyze_data(self, file_path): present_list = PresenTALKUtil.get_all_present_list() present_target = PresenTALKUtil.get_feature_target() present_purpose = PresenTALKUtil.get_feature_purpose() data_file = open(file_path, 'r', encoding='utf-8') lines = data_file.readlines() word_count_dict = dict() for line in lines: word_list = line.split(' ') for word in word_list: word = word.replace('\n', '') if word in word_count_dict: word_count_dict[word] += 1 else: word_count_dict[word] = 1 # word Count sorted_list = sorted(word_count_dict.items(), key=itemgetter(1), reverse=True) # for key, value in sorted_list: # print(key, value) # present word count for key, value in sorted_list: if key in present_list: print(key, value) # target word count for key, value in sorted_list: if key in present_target: print(key, value) # purpose word count for key, value in sorted_list: if key in present_purpose: print(key, value)
def get_is_present(word): is_present = False; present_list = PresenTALKUtil.get_present_list() for present in present_list: if present in word: is_present = True break else: if word in wiki_model.wv.vocab: if wiki_model.similarity('선물', word) > 0.3: is_present = True break return is_present
def find_feature(self, message, feature_key): # feature 에 대한 정보를 사용자가 이야기했는지를 체크해서 feature 리스트에 담아 두어야 한다 # feature 를 어떻게 찾을 것인가? -> 포함 ? 명사 비교 ? feature_dic_list = PresenTALKUtil.get_feature_dictionary(feature_key) # 명사 카운팅을 하자 # 이유 : 명사가 여러 개인 경우, 포함된 feature 가 꼭 그 feature 일 가능성이 점점 낮아짐 # ex) "얼마전에 엄마 선물을 해드려서 아빠도 좀 해드리려고" is_find = False for feature in feature_dic_list: feature_list = feature['concept_list'].split(',') feature_type = feature['feature_type'] feature_root = feature['concept_name'] for m_feature in feature_list: if m_feature in message: is_find = True self.feature_dict[feature_type] = feature_root return is_find
def find_feature_candidate(self, message): # message 의 길이에 따라 로직을 달리 하자 # length 가 길면 여러 의미로 해석될 수 있고, 짧은 경우에는 대부분의 내용이 핵심 내용일 확률이 높다 # 뒤에 불필요한 내용은 제거 후 길이 확인 ( <= 20 ) message = text_util.remove_unsueful_text(message) if len(message) <= 20: all_feature_dic_list = PresenTALKUtil.get_feature_dictionary('') for feature in all_feature_dic_list: feature_list = feature['concept_list'].split(',') feature_type = feature['feature_type'] feature_root = feature['concept_name'] for m_feature in feature_list: if m_feature in message: self.feature_dict[feature_type] = feature_root else: pass
def __init__(self): self._neo4j_c = neo4j_client.Neo4jClient('your db host', 'your db name', 'your db password') self._node_list = self._neo4j_c.execute_query('MATCH (n) RETURN n.name, n.alias', ['n.name', 'n.alias']) self._relation_list = self._neo4j_c.execute_query('MATCH (n)-[r]-() RETURN DISTINCT type(r), r.alias', ['type(r)', 'r.alias']) self._prop_list = PresenTALKUtil.get_knowledge_prop_list()
def __init__(self): self.keyword_list = PresenTALKUtil.get_command_config('recommend')[0]['command_word'].split(',')
def execute_fragments(self, mecab, message): # TODO : feature 를 찾는 로직 & feature 정보가 기준 이상이라면 그 feature 정보를 바탕으로 추천 # 그게 아니라면 일반적인 선물 이야기하기 ( PresenTALKUtil.get_random_message() 활용 ) return PresenTALKUtil.get_random_message('normal_present')
def __init__(self): self.end_post_list = [ 'VX+EC', 'XSV+EC', 'VX', 'EP+EF', 'VX+EF', 'VV+EC', 'VV', 'EC' ] self.command_config_list = PresenTALKUtil.get_command_config('')
def response_quibble_message(self, message): # default response_message = [] response_message.append(PresenTALKUtil.get_random_message('quibble')) return response_message
def print_gambit_message(self, message, intent_index, message_type, pre_feature_key): # TODO : 로직 정리 print(message_type) response_message_type = '' feature_key = '' response_text = [] if message_type == 'BG': response_text.append( PresenTALKUtil.get_random_message('bot_starting')) # BS : Bot Start, BQ : Bot Question response_message_type = 'BS' elif message_type == 'BS': # 사용자의 응답이 긍정일 경우 if BayesClassifier.get_message_class(message) == 'POS': temp_dic = self.print_feature_message() feature_key = temp_dic['feature_key'] response_text.append(temp_dic['feature_message']) response_message_type = 'BF' # 긍정이 아닐 경우 else: # 사용자가 원하는 걸 직접 질문 response_text.append( PresenTALKUtil.get_random_message('reject')) response_message_type = 'BJ' elif message_type == 'BF': is_find = False if pre_feature_key != '': # feature 추출 is_find = self.find_feature(message, pre_feature_key) temp_dic = self.print_feature_message() feature_key = temp_dic['feature_key'] # feature 추출이 모두 끝난 경우 if feature_key == '': # rec = recommand_present.Recommandation() # response_text.append(rec.get_recommand_message(self.feature_dict)) # who, age, price, why recommend_list = recommend_data2.recommend_in_DB( self.feature_dict['object'], self.feature_dict['age'], '10', self.feature_dict['purpose']) present_list = '' for present in recommend_list: if present_list == '': present_list = present else: present_list = present_list + ', ' + present recommend_text = '선물 추천 결과입니다. ' + str( present_list) + ' 어떠신가요?' response_text.append(recommend_text) response_message_type = 'BR' else: response_text.append(temp_dic['feature_message']) response_message_type = 'BF' # TODO : 추후 추가 ( 로직의 복잡도 증가 ) # elif message_type == 'BI': # if BayesClassifier.get_message_class(message) == 'POS': # pass # else: # pass elif message_type == 'BN': response_text = self.find_matching_rule(message) if len(response_text) == 0: response_message_type = 'BN' response_text = self.response_quibble_message(message) # TODO : 추후 추가 ( 로직의 복잡도 증가 ) # 매칭하는 룰을 찾지 못했을 경우 특정 토픽으로 이야기하기 # if len(response_text) == 0: # user_info_text = self.get_user_info_message() # # if user_info_text == '': # response_message_type = 'BN' # response_text = self.response_quibble_message(message) # else: # response_text.append('제가 공부한 분야로 이야기해 보시겠어요?') # response_text.append(user_info_text) # response_message_type = 'BI' elif message_type == 'BR': # TODO : 로직 정리 필요 if BayesClassifier.get_message_class(message) == 'POS': response_message_type = 'BE' response_text.append( PresenTALKUtil.get_random_message('bot_ending')) else: # 사용자가 원하는 걸 직접 질문 response_text.append( PresenTALKUtil.get_random_message('reject')) response_message_type = 'BJ' # TODO : 대답 하고 나서의 로직 추가 elif message_type == 'BA': return_message = self.find_matching_rule(message) if len(return_message) != 0: response_text = return_message else: response_text = self.response_quibble_message(message) response_message_type = 'BN' elif message_type == 'BJ': if BayesClassifier.get_message_class(message) == 'POS': response_message_type = 'BE' response_text.append( PresenTALKUtil.get_random_message('bot_ending')) else: response_message_type = 'BN' response_text.append( PresenTALKUtil.get_random_message('bot_restart')) elif message_type == 'BE': if BayesClassifier.get_message_class(message) == 'POS': self.isFinish = True response_text.append( PresenTALKUtil.get_random_message('bot_bye')) # TODO : 시나리오 구성 else: return_message = self.find_matching_rule(message) if len(return_message) != 0: response_text = return_message else: response_text = self.response_quibble_message(message) response_message_type = 'BN' self.save_conversation_list(response_text, response_message_type, feature_key) return response_text
def print_greeting_message(self): greeting_message = [] greeting_message.append( PresenTALKUtil.get_random_message('bot_greeting')) self.save_conversation_list(greeting_message, 'BG', '') return greeting_message