| Total Complexity | 5 |
| Total Lines | 40 |
| 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 get_analyzer(analyzerspec): |
||
| 17 | match = re.match(r'(\w+)(\((.*)\))?', analyzerspec) |
||
| 18 | if match is None: |
||
| 19 | raise ValueError( |
||
| 20 | "Invalid analyzer specification {}".format(analyzerspec)) |
||
| 21 | |||
| 22 | analyzer = match.group(1) |
||
| 23 | posargs, kwargs = parse_args(match.group(3)) |
||
| 24 | posargs = posargs if posargs else [None] |
||
| 25 | try: |
||
| 26 | return _analyzers[analyzer](*posargs, **kwargs) |
||
| 27 | except KeyError: |
||
| 28 | raise ValueError("No such analyzer {}".format(analyzer)) |
||
| 29 | |||
| 30 | |||
| 31 | register_analyzer(simple.SimpleAnalyzer) |
||
| 32 | register_analyzer(snowball.SnowballAnalyzer) |
||
| 33 | |||
| 34 | # Optional analyzers |
||
| 35 | try: |
||
| 36 | from . import voikko |
||
| 37 | register_analyzer(voikko.VoikkoAnalyzer) |
||
| 38 | except ImportError: |
||
| 39 | annif.logger.debug("voikko not available, not enabling voikko analyzer") |
||
| 40 |