| Total Complexity | 6 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """Collection of language-specific analyzers and analyzer registry for Annif""" |
||
| 2 | |||
| 3 | import re |
||
| 4 | from . import simple |
||
| 5 | from . import snowball |
||
| 6 | import annif |
||
| 7 | from annif.util import parse_args |
||
| 8 | |||
| 9 | _analyzers = {} |
||
| 10 | |||
| 11 | |||
| 12 | def register_analyzer(analyzer): |
||
| 13 | _analyzers[analyzer.name] = analyzer |
||
| 14 | |||
| 15 | |||
| 16 | def _extend_posargs(posargs): |
||
| 17 | if not posargs: |
||
| 18 | posargs = [None] |
||
| 19 | return posargs |
||
| 20 | |||
| 21 | |||
| 22 | def get_analyzer(analyzerspec): |
||
| 23 | match = re.match(r'(\w+)(\((.*)\))?', analyzerspec) |
||
| 24 | if match is None: |
||
| 25 | raise ValueError( |
||
| 26 | "Invalid analyzer specification {}".format(analyzerspec)) |
||
| 27 | |||
| 28 | analyzer = match.group(1) |
||
| 29 | posargs, kwargs = parse_args(match.group(3)) |
||
| 30 | posargs = _extend_posargs(posargs) |
||
| 31 | try: |
||
| 32 | return _analyzers[analyzer](*posargs, **kwargs) |
||
| 33 | except KeyError: |
||
| 34 | raise ValueError("No such analyzer {}".format(analyzer)) |
||
| 35 | |||
| 36 | |||
| 37 | register_analyzer(simple.SimpleAnalyzer) |
||
| 38 | register_analyzer(snowball.SnowballAnalyzer) |
||
| 39 | |||
| 40 | # Optional analyzers |
||
| 41 | try: |
||
| 42 | from . import voikko |
||
| 43 | register_analyzer(voikko.VoikkoAnalyzer) |
||
| 44 | except ImportError: |
||
| 45 | annif.logger.debug("voikko not available, not enabling voikko analyzer") |
||
| 46 |