Passed
Pull Request — master (#496)
by Juho
01:44
created

annif.analyzer._parse_analyzer_args()   A

Complexity

Conditions 5

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nop 1
dl 0
loc 13
rs 9.2833
c 0
b 0
f 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