def __init__(self, username=None, path=os.path.join(get_main_dir(), 'data'), write=False, speak=False): if username is None: if _user is None: self.user = raw_input('Enter a username: '******'generic.chal')) as fp: self.generic = [i.strip() for i in fp.readlines() if i.strip() and i[0] != '#'] self.generic.append("I can't seem to understand.") except IOError: self.generic = ["I have a problem with my brain, I can't think..."] self.macro = HALmacro(self, username, write) self.speak = speak self.advspeak = False self.sphandle = None self.speak_opt = dict(volume=100, speed=175, gender=True, lang='en-us') self.data_folder = path self.debug_write = write self._init_handler_plugin() self._init_autotalk() self._init_repetition() self.semantics = False self.rndname = True self.previn = deque(maxlen=100) self.prevout = deque(maxlen=100) self.lastlang = 'en'
def main(data=None): sys.stdout = codecs.getwriter('mbcs' if os.name == 'nt' else 'utf-8')(sys.stdout, 'replace') if data is None: data = os.path.join(get_main_dir(), 'data') if not os.path.exists(data): raise SystemExit('Your need a full package with the data folder') print '[SYSTEM]', 'Booted on', get_system_info(), '[/SYSTEM]' print print '[SYSTEM]' hal = HAL(write=True, speak=True) print '[/SYSTEM]' print print '-HAL: Hello %s. I am HAL %s.'%(hal.user, hal.version) print prompt = '-%s:'%hal.user halpro = '-HAL:' length = max(len(prompt), len(halpro)) if len(prompt) < length: prompt += ' '*(length-len(prompt)) if len(halpro) < length: halpro += ' '*(length-len(halpro)) prompt += ' ' try: while hal.running: line = raw_input(prompt) for i in hal.ask(line): print halpro, i print raise EOFError except EOFError: print '-HAL:', hal.shutdown()
def __init__(self, parent, user=None, write=False): self.basic = { '$USERNAME$' : user if user is not None else getuser(), '$USER$' : user if user is not None else getuser(), '$GENDER$' : 'male', '$GENUS$' : 'robot', '$SPECIES$' : 'chatterbot', '$NAME$' : 'HAL', '$MASTER$' : 'Tudor and Guanzhong', '$BIRTHPLACE$' : 'Toronto', '$FAVORITEFOOD$' : 'electricity', '$FAVORITECOLOR$' : 'blue', '$BOTMASTER$' : 'creator', '$WEBSITE$' : 'dev.halbot.co.cc', '$RELIGION$' : 'atheist', '$BIRTHDAY$' : self.halbday.strftime('%B %d, %Y'), '$AGE$' : lambda: str(age_from_dob(self.halbday)), } self.extended = { self.rerandint: lambda m: str(random.randint(int(m.group(1)), int(m.group(2)))), self.rebotprev: lambda m: ' '.join(self.hal.prevout[int(m.group(1))]) if int(m.group(1)) < len(self.hal.prevout) else '', self.reusrprev: lambda m: self.hal.previn[int(m.group(1))] if int(m.group(1)) < len(self.hal.previn) else '', } self.hal = parent # Plugin interface dir = os.path.join(get_main_dir(), 'plugins/macro') files = filter(bool, map(module_filter, glob(os.path.join(dir, '*')))) for file in files: if write: print 'Loading macro extension "%s"...'%file try: data = imp.find_module(file, [dir]) module = imp.load_module(str(uuid.uuid1()), *data) if hasattr(module, 'basic'): self.basic.update(module.basic) if hasattr(module, 'extended'): self.extended.update(module.extended) if hasattr(module, 'halfiles'): for file in module.halfiles: self.hal.load(file) except: print 'Error in macro extension', file traceback.print_exc()
def __init__(self, write): dir = os.path.join(get_main_dir(), 'plugins/math') files = filter(bool, map(module_filter, glob(os.path.join(dir, '*')))) for file in files: self.funcs = dict(funcs) if write: print 'Loading math extension "%s"...'%file try: data = imp.find_module(file, [dir]) module = imp.load_module(str(uuid.uuid1()), *data) if hasattr(module, 'funcs') and type(module.funcs) is dict: self.funcs.update(module.funcs) except: print 'Error in math extension', file traceback.print_exc() refuncs = '|'.join(self.funcs) self.requation = re.compile(r'(?:[0-9]|(?<=[0-9)a-z])[+/\-*^](?=[0-9(]|'+refuncs+r')|(?<=[0-9])!|[()]|(?:'+refuncs+r')(?=[(])|(?<=[0-9\s)]),(?=[\s0-9a-z])|'+'|'.join(const)+r')+') self.refunc = re.compile(r'(?:'+refuncs+r')\(.*\)', re.I) self.funcs.update(const)
def _init_handler_plugin(self): # Plugin interface dir = os.path.join(get_main_dir(), 'plugins/handler') #files = os.path.join(dir, '*.py') #files = [os.path.basename(i).replace('.py', '') for i in glob(files)] files = filter(bool, map(module_filter, glob(os.path.join(dir, '*')))) for file in files: try: print 'Loading extension "%s"...'%file data = imp.find_module(file, [dir]) module = imp.load_module(str(uuid.uuid1()), *data) except: print 'Error in handler extension', file traceback.print_exc() else: if not (hasattr(module, 'check') and callable(module.check)): print 'Error in handler extension %s: check() does NOT exist!'%file continue if not (hasattr(module, 'answer') and callable(module.answer)): print 'Error in handler extension %s: check() does NOT exist!'%file continue self.handlers.insert(0, module)
#import language import HALspeak import HALadvspeak import HALspam import HALtran try: _user = getuser() except: _user = None __builtin__.IN_HAL = True # Initialization __framework_dir = os.path.join(get_main_dir(), 'plugins/frameworks') sys.path.append(__framework_dir) del __framework_dir def ratio_correct(input): size, words, correct = check_all(input) return len(filter(bool, correct))/size # TODO: Combine multiple matches into one # i.e. 2 #HELLO sections will be combined into in pick_match() class HALintel(HALBot): junks = '!@#$%^()_~`./\\?,' regroups = re.compile(r'\\g<([1-9][0-9]*?)>') def __init__(self, path, user, write, *args, **kwargs): HALBot.__init__(self) #HALBot.__init__(self, path, user, write, *args, **kwargs)