Пример #1
0
  def __init__(self,name):
    self.blist = {}
    self.response = ''
    self.typerate = 0.05
    
    self._dir = dir(self)

    # Initialize the AIML interpreter  
    self.kernel = aiml.Kernel()
    self.kernel.verbose(1)
    self.kernel.setPredicate("secure", "yes") # secure the global session
    self.kernel.bootstrap(learnFiles="data/aiml/startup.xml", commands="bootstrap")
    self.kernel.setPredicate("secure", "no") # and unsecure it.
    self.kernel.setBotPredicate("name",name)

    # Initialise the Bayes parser
    self.bayes = AIMLBayes(name)
Пример #2
0
    def __init__(self):  # name
        self.blist = {}
        self.response = ''
        self.typerate = 0.05
        self._dir = dir(self)

        print("Initialize the back-ends.")
        # Fetch the configuration info
        config = lib.aiml.configFile.get()
        # Initialize the AIML interpreter
        self.kernel = aiml.Kernel()
        #extract config options
        try:
            verbose = config["general.verbose"] == "yes" or config[
                "cla.verboseMode"] == "yes"
        except:
            verbose = False
        try:
            botName = config["general.botname"]
        except:
            botName = "Nameless"
        try:
            botMaster = config["general.botmaster"]
        except:
            botMaster = "The Master"
        try:
            sessionsPersist = config["general.sessionspersist"].lower() in [
                "yes", "y", "true"
            ]
        except:
            sessionsPersist = False
        try:
            sessionsDir = config["general.sessionsdir"]
        except:
            sessionsDir = "var/sessions"
        self.kernel.verbose(1)
        self.kernel.setPredicate("secure", "yes")  # secure the global session
        self.kernel.bootstrap(learnFiles="lib/aiml/data/aiml/startup.xml",
                              commands="bootstrap")
        self.kernel.setPredicate("secure", "no")  # and unsecure it.
        self.kernel.setBotPredicate("name", name)

        # Initialize bot predicates
        for k, v in config.items():
            if k[:8] != "botinfo.":
                continue
            kernel.setBotPredicate(k[8:], v)

        # Initialise the Bayes parser
        self.bayes = AIMLBayes(name)

        # Load persistent session data, if necessary
        if sessionsPersist:
            try:
                for session in os.listdir(sessionsDir):
                    # Session files are named "*****@*****.**", where
                    # user@protocol is also the internal name of the session.
                    root, ext = os.path.splitext(session)
                    if ext != ".ses":
                        # This isn't a session file.
                        continue
                    # Load the contents of the session file (a single dictionary
                    # containing all the predicates for this session).
                    if verbose: print("Loading session:"), root
                    f = file("%s/%s" % (sessionsDir, session), "rb")
                    d = marshal.load(f)
                    f.close()
                    # update the predicate values in the Kernel.
                    for k, v in d.items():
                        kernel.setPredicate(k, v, root)
            except OSError:
                print("WARNING: Error loading session data from"), sessionsDir

        # Handle local mode: only start the tty backend
        if config['cla.localMode'].lower() in ["yes", "y", "true"]:
            try:
                _addBackEnd("tty", "BackEndTTY")
            except:
                print(
                    "ERROR initializing backend class backends.tty.BackEndTTY")
                traceback.print_tb(sys.exc_info()[2])
        else:
            # Initialize the back-ends.  Pythonic black magic ensues...
            # First we iterate over all backend modules.
            for be in backends.__all__:
                # If this backend isn't activated in the configuration file,
                # ignore it.
                try:
                    isActive = (config["%s.active" % be].lower()
                                in ["yes", "y", "true"])
                except KeyError:
                    print(
                        "WARNING: no 'active' entry found for module %s in configuration file."
                    ) % be
                    isActive = False
                if not isActive:
                    if config['cla.verboseMode'] == 'yes':
                        print("Skipping inactive frontend: %s") % be
                    continue

                # Attempt to extract the name of the back-end class defined in this module.
                # If no such class is defined, or if the class is not a subclass of IackEnd,
                # skip this module.
                try:
                    cls = eval("backends.%s.backEndClass" % be)
                    if not issubclass(eval("backends.%s.%s" % (be, cls)),
                                      backends.backend.IBackEnd):
                        continue
                except AttributeError:
                    # no valid back-end class defined in this file.
                    print(
                        "WARNING: could not find valid back-end class in module %s"
                    ) % be
                    continue

                # Create an instance of this class in the _backends dictionary
                try:
                    _addBackEnd(be, cls)
                except:
                    # raise # uncomment for details on error
                    print("ERROR initializing backend class backends.%s.%s"
                          ) % (be, cls)
                    traceback.print_tb(sys.exc_info()[2])
                    continue