def test_util(self): import chatbot.utils as utils text = '''My mind is built using Hanson Robotics' character engine, a simulated humanlike brain that runs inside a personal computer. Within this framework, Hanson has modelled Phil's personality and emotions, allowing you to talk with Phil through me, using speech recognition, natural language understanding, and computer vision such as face recognition, and animation of the robotic muscles in my face.''' text2 = utils.shorten(text, 123) self.assertTrue(len(text2) <= 123) text2 = utils.shorten(text, 0) self.assertTrue(len(text2) > 0) self.assertTrue(utils.str_cleanup(' . ') == '') self.assertTrue(utils.str_cleanup(' .ss ') == 'ss') self.assertTrue(utils.str_cleanup(' s.ss ') == 's.ss') self.assertTrue(utils.str_cleanup(None) is None) self.assertTrue(utils.check_online('google.com')) self.assertTrue(utils.check_online('google.com', 80)) self.assertTrue(not utils.check_online('google.com', 81))
def wrapper(*args, **kwargs): if check_online(): return func(*args, **kwargs) else: logger.warn("No Internet or unstable Internet") dummpy_response = { 'text': '', 'trace': 'No Internet', } return dummpy_response
def ask(self, question): try: response = requests.get('http://api.duckduckgo.com', params={ 'q': question, 'format': 'json' }, timeout=1) except: self.online = check_online('duckduckgo.com') return '' json = response.json() if json['AnswerType'] not in ['calc']: return json['Abstract'] or json['Answer'] else: return ''
def respond(self, question, lang, session=None, *args, **kwargs): ret = {} ret['botid'] = self.id ret['botname'] = self.name ret['text'] = '' if lang not in self.languages: return ret elif re.search(r'\[.*\]', question): return ret if self.last_check_time is None or ( dt.datetime.now() - self.last_check_time).total_seconds() > 60: self.last_check_time = dt.datetime.now() self.online = check_online('duckduckgo.com') if self.online: if self.is_favorite(question.lower()): answer = self.ask(question) answer, res = shorten(answer, self.response_limit) if self.non_repeat and session and answer: if not session.check(question, answer): ret['repeat'] = answer answer = '' self.logger.warn("Repeat answer") if res and session is not None: self.set_context(session, {'continue': res}) self.logger.info("Set continue={}".format(res)) ret['text'] = answer else: ret['trace'] = "Can't answer" else: ret['trace'] = "Offline" return ret