You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
791 B
32 lines
791 B
5 years ago
|
|
||
|
import re
|
||
|
from logging import NullHandler, getLogger
|
||
|
|
||
|
import babelfish
|
||
|
|
||
3 years ago
|
from knowit.core import Rule
|
||
5 years ago
|
|
||
|
logger = getLogger(__name__)
|
||
|
logger.addHandler(NullHandler())
|
||
|
|
||
|
|
||
|
class LanguageRule(Rule):
|
||
|
"""Language rules."""
|
||
|
|
||
|
name_re = re.compile(r'(?P<name>\w+)\b', re.IGNORECASE)
|
||
|
|
||
|
def execute(self, props, pv_props, context):
|
||
|
"""Language detection using name."""
|
||
|
if 'language' in props:
|
||
|
return
|
||
|
|
||
|
if 'name' in props:
|
||
|
name = props.get('name', '')
|
||
|
match = self.name_re.match(name)
|
||
|
if match:
|
||
|
try:
|
||
|
return babelfish.Language.fromname(match.group('name'))
|
||
|
except babelfish.Error:
|
||
|
pass
|
||
|
logger.info('Invalid %s: %r', self.description, name)
|