コード例 #1
0
ファイル: kernel.py プロジェクト: scareface972/aerolito
    def respond(self, value, user_id=None, registry=True):
        u"""
        Returns a response for a given user input.

        Parameters ``value`` is the user input.

        If ``user_id`` is informed, the kernel changes the session for this user,
        if parameter is null, kernel keeps the active user. If user is not 
        informed and no user is active, kernel try to use the *'default'* user,
        if default is not avaliable (out of session pool) an exception is 
        raised.
        
        This method just can be used after environment initialization.
        """

        # Verify initialization
        if not self._environ :
            raise exceptions.InitializationRequired('configuration')
        elif not self._patterns:
            raise exceptions.InitializationRequired('conversation')

        # Verify user's session
        if user_id is not None:
            self.set_user(user_id)
        elif self._environ['user_id'] is None:
            if 'default' in self._environ['session']:
                self.set_user('default')
            else:
                raise exceptions.NoUserActiveInSession()

        output = None
        value = normalize_input(value, self._synonyms)
        for pattern in self._patterns:
            if pattern.match(value, self._environ):
                output = pattern.choice_output(self._environ)
                pattern.execute_post(self._environ)
                break
            
        session = self._environ['session'][self._environ['user_id']]
        if registry:
            session['inputs'].append(value)
        
        if output:
            recursive = re.findall('\(rec\|([^\)]*)\)', output)
            for r in recursive:
                toreplace = u'(rec|%s)'%r
                resp = self.respond(r, registry=False) or ''
                output = output.replace(toreplace, resp)

            if registry:
                session['responses'].append(output)
                session['responses-normalized'].append(normalize_input(output, self._synonyms))

        return output
コード例 #2
0
ファイル: kernel.py プロジェクト: scareface972/aerolito
    def load_meaning(self, meaning_file, encoding='utf-8'):
        u"""
        Load a meaning file.

        Receive as parameters a name (with relative or full path) of a
        meaning file, and their encoding. Default encoding is utf-8.

        Meaning file must have at least one element. Contains a list of lists.

        The patterns are loaded in ``_meanings``
        """
        try:
            plain_text = codecs.open(meaning_file, 'rb', encoding).read()
            data = yaml.load(plain_text)
        except IOError:
            raise exceptions.FileNotFound(meaning_file)
            
        for meanings, values in data.items():
            if len(values) == 0:
                raise exceptions.InvalidTagValue(
                        u'Meaning list must have one or more element.')

            key = remove_accents(meanings).lower()
            vals = [normalize_input(v, self._environ['synonyms']).lower() for v in values]

            if key in self._meanings:
                raise exceptions.DuplicatedMeaning(key, meaning_file)
            
            self._meanings[key] = vals