コード例 #1
0
def country(config, common_words):
    """
    Builder for rebulk object.

    :param config: rule configuration
    :type config: dict
    :param common_words: common words
    :type common_words: set
    :return: Created Rebulk object
    :rtype: Rebulk
    """
    rebulk = Rebulk(disabled=lambda context: is_disabled(context, 'country'))
    rebulk = rebulk.defaults(name='country')

    def find_countries(string, context=None):
        """
        Find countries in given string.
        """
        allowed_countries = context.get(
            'allowed_countries') if context else None
        return CountryFinder(allowed_countries, common_words).find(string)

    rebulk.functional(
        find_countries,
        #  Prefer language and any other property over country if not US or GB.
        conflict_solver=lambda match, other: match
        if other.name != 'language' or match.value not in
        (babelfish.Country('US'), babelfish.Country('GB')) else other,
        properties={'country': [None]},
        disabled=lambda context: not context.get('allowed_countries'))

    babelfish.country_converters['guessit'] = GuessitCountryConverter(
        config['synonyms'])

    return rebulk
コード例 #2
0
def country():
    """
    Builder for rebulk object.
    :return: Created Rebulk object
    :rtype: Rebulk
    """
    rebulk = Rebulk().defaults(name='country')

    rebulk.functional(find_countries,
                      #  Prefer language and any other property over country if not US or GB.
                      conflict_solver=lambda match, other: match
                      if other.name != 'language' or match.value not in [babelfish.Country('US'),
                                                                         babelfish.Country('GB')]
                      else other,
                      properties={'country': [None]})

    return rebulk
コード例 #3
0
    def __init__(self, language, country=None, strict=False):
        language = u(language.strip().lower())
        country = babelfish.Country(country.upper()) if country else None

        try:
            self.lang = babelfish.Language.fromguessit(language)
            # user given country overrides guessed one
            if country:
                self.lang.country = country

        except babelfish.LanguageReverseError:
            msg = 'The given string "%s" could not be identified as a language' % language
            if strict:
                raise ValueError(msg)

            log.debug(msg)
            self.lang = UNDETERMINED
コード例 #4
0
    def reverse(self, name):
        # exceptions come first, as they need to override a potential match
        # with any of the other guessers
        try:
            return self.guessit_exceptions[name.lower()]
        except KeyError:
            pass

        try:
            return babelfish.Country(name.upper()).alpha2
        except ValueError:
            pass

        for conv in [babelfish.Country.fromname]:
            try:
                return conv(name).alpha2
            except babelfish.CountryReverseError:
                pass

        raise babelfish.CountryReverseError(name)
コード例 #5
0
 def convert(self, alpha2):
     if alpha2 == 'GB':
         return 'UK'
     return str(babelfish.Country(alpha2))
コード例 #6
0
 def convert(self, alpha2):
     return str(babelfish.Country(alpha2))