Esempio n. 1
0
    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
Esempio n. 2
0
    def load_sysnonym(self, synonym_file, encoding='utf-8'):
        u"""
        Load a synonym file.

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

        Synonym file must have at least one element. Contains a list of lists.
        
        The patterns are loaded in ``_synonyms``
        """
        try:
            plain_text = codecs.open(synonym_file, 'rb', encoding).read()
            data = yaml.load(plain_text)
        except IOError:
            raise exceptions.FileNotFound(synonym_file)
            
        for synonyms in data:
            if len(synonyms) < 2:
                raise exceptions.InvalidTagValue(
                        u'Synonym list must have more than one element.')

            key = remove_accents(synonyms[0]).lower()
            vals = [remove_accents(value).lower() for value in synonyms[1:]]

            if key in self._synonyms:
                raise exceptions.DuplicatedSynonym(key, synonym_file)
            
            self._synonyms[key] = vals
Esempio n. 3
0
    def __convert_regex(self, p, tag, environ=None):
        u"""
        Converts the values of ``tag`` to ``Regex``s. Accepts a list of string 
        or just a string.
        """
        synonyms = environ['synonyms']
        meanings = environ['meanings']
        if p.has_key(tag):
            tagValues = p[tag]
            if tagValues is None or tagValues == u'':
                raise exceptions.InvalidTagValue(u'Invalid value for tag %s.' %
                                                 tag)

            if isinstance(tagValues, (tuple, list)):
                values = tagValues
            else:
                values = [tagValues]

            normalized = [
                normalize_input(unicode(x), synonyms) for x in values
            ]
            patterns = []
            for x in normalized:
                patterns.extend(get_meanings(x, meanings, self._mean))

            return [Regex(x, self._ignore) for x in patterns]
        else:
            return None
Esempio n. 4
0
    def __convert_action(self, p, tag, environ):
        u"""
        Converts the values of ``tag`` to ``Action``s, an action just can be 
        created when there is a correspondent directive. An ``InvalidTagValue``
        is raised if there is no correspondent directive.

        This method accepts a list of dicts or just a dict. The dictionaries 
        can have more than one key, each key means a name of a directive and 
        the values are the parameters.
        """
        if p.has_key(tag):
            tagValues = p[tag]
            actions = []

            if isinstance(tagValues, dict):
                tagValues = [tagValues]

            if isinstance(tagValues, (tuple, list)):
                for d in tagValues:
                    for k, p in d.iteritems():
                        if isinstance(p, (tuple, list)):
                            params = [Literal(x) for x in p]
                        else:
                            params = [Literal(p)]

                        if k not in environ['directives']:
                            raise exceptions.InvalidTagValue(
                                u'Directive "%s" not found' % str(k))

                        action = Action(environ['directives'][k], params)
                        actions.append(action)
            else:
                raise exceptions.InvalidTagValue(u'Invalid value for tag %s.' %
                                                 tag)

            return actions
        else:
            return None
Esempio n. 5
0
    def __convert_mean(self, p, environ=None):
        meanings = {}
        synonyms = environ['synonyms']
        if p.has_key('mean'):
            tagValues = p['mean']
            if tagValues is None:
                raise exceptions.InvalidTagValue(u'Invalid value for tag mean')

            for k in tagValues:
                key = remove_accents(k)
                meanings[key] = [
                    normalize_input(v, synonyms) for v in tagValues[k]
                ]

            return meanings
        else:
            return None
Esempio n. 6
0
    def __convert_literal(self, p, tag, environ=None):
        u"""
        Converts the values of ``tag`` to ``Literal``s. Accepts a list of 
        string or just a string.
        """
        meanings = environ['meanings']
        if p.has_key(tag):
            tagValues = p[tag]
            if tagValues is None or tagValues == u'':
                raise exceptions.InvalidTagValue('Invalid value for tag %s.' %
                                                 tag)

            if isinstance(tagValues, (tuple, list)):
                values = tagValues
            else:
                values = [tagValues]

            patterns = []
            for x in values:
                patterns.extend(get_meanings(x, meanings, self._mean))

            return [Literal(unicode(x)) for x in patterns]
        else:
            return None