def response(user, qus): ans = '' if re.match('^translate .*$', qus): content = re.sub('^translate ', '', qus) ans = stardict.translate(content) elif re.match('^home$', qus): ans = web.show_homepage() elif re.match('^wiki$', qus): ans = web.show_wiki() elif re.match('^baidu .*$', qus): content = re.sub('^baidu ', '', qus) ans = web.search_baidu(content) if common.is_super(user): if re.match('^idea .*$', qus): content = re.sub('^idea ', '', qus) ans = idea.Idea().store_idea(user, content) elif re.match('^teach .*$', qus): content = re.sub('^teach ', '', qus) ans = teach.Teach().do_teach(user, content) elif re.match('^corpus .*$', qus): content = re.sub('^corpus ', '', qus) ans = web.search_corpus(content) return ans
def store_idea(self, user, idea_str): if not common.is_super(user): return if not idea_str: return data_path = common.Cfg().get('local', 'data_paths')[0] file_path = os.path.join(data_path, self.store_file) time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) text = '* %s %s\n' % (time_str, idea_str) with open(file_path, 'a') as fp: fp.write(text) return 'OK, idea is stored.'
def do_teach(self, user, inStr): if not common.is_super(user): return if not inStr: return if self.splitSym not in inStr: return data_path = common.cfg().get('data_path')[0] file_path = os.path.join(data_path, self.file_name) f = open(file_path, 'a') que = inStr.split(self.splitSym)[0] ans = inStr.split(self.splitSym)[1] que = common.init_input(que) ans = common.init_input(ans) text = '\n- que:\n - %s\n ans:\n - %s\n' % (que, ans) f.write(text) f.close() import_data.importData().do_import([file_path]) return 'OK, I learned.'
def do_teach(self, user, in_str): if not common.is_super(user): return if not in_str: return if self.split_sym not in in_str: return data_path = common.Cfg().get('local', 'data_paths')[0] file_path = os.path.join(data_path, self.teach_file) f = open(file_path, 'a') que = in_str.split(self.split_sym)[0] ans_list = in_str.split(self.split_sym)[1:] ans = self.split_sym.join(ans_list) que = common.init_input(que) ans = common.init_input(ans) text = '\n- que:\n - %s\n ans:\n - %s\n' % (que, ans) f.write(text) f.close() return 'OK, I learned.'
def response(db, user, inStr): isSuper = common.is_super(user) inStr = common.expand_abbrev(inStr) inStr = re.sub('[%s]+' % string.punctuation, '', inStr) inStr = inStr.lower() if len(inStr) < 3: return regexStr = '(^|.* )' + inStr + '( .*|$)' colls = db.collection_names() try: dias = memory.dialogue(user).get_dia() lastDia = dias[-1] lastAns = list(lastDia.values())[0] except Exception: return newQues = [] if DO_YOU_MEAN in lastAns: ques = lastAns.split(r'\n')[1:] for que in ques: if re.match(regexStr, que): newQues.append(que) newQues = list(set(newQues)) if len(newQues) < 1: words = inStr.split(' ') for coll in colls: qas = _get_qas(db, coll, isSuper) if not qas: continue for qa in qas: ques = qa['que'] for que in ques: all_words_in = True que_words = que.split(' ') for word in words: if word not in que_words: all_words_in = False break if all_words_in: newQues.append('- ' + que) break newQues = list(set(newQues)) if len(newQues) < 1: return elif len(newQues) == 1: Que = newQues[0] Que = re.sub(r'^- ', '', Que) for coll in colls: qas = _get_qas(db, coll, isSuper) if not qas: continue for qa in qas: ques = qa['que'] if Que in ques: res = qa['ans'] break if type(res) == list: res = random.choice(res) if res[-1] == '\n': res = res[:-1] else: newQues.insert(0, DO_YOU_MEAN) res = '\n'.join(newQues) if res: memory.dialogue(user).insert_dia(inStr, res) return res