summaryrefslogtreecommitdiff
path: root/searx/answerers/__init__.py
blob: 8f5951c75d54da8b082eee06998b0169b48d41c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from os import listdir
from os.path import realpath, dirname, join, isdir
from searx.utils import load_module
from collections import defaultdict


answerers_dir = dirname(realpath(__file__))


def load_answerers():
    answerers = []
    for filename in listdir(answerers_dir):
        if not isdir(join(answerers_dir, filename)):
            continue
        module = load_module('answerer.py', join(answerers_dir, filename))
        if not hasattr(module, 'keywords') or not isinstance(module.keywords, tuple) or not len(module.keywords):
            exit(2)
        answerers.append(module)
    return answerers


def get_answerers_by_keywords(answerers):
    by_keyword = defaultdict(list)
    for answerer in answerers:
        for keyword in answerer.keywords:
            for keyword in answerer.keywords:
                by_keyword[keyword].append(answerer.answer)
    return by_keyword


def ask(query):
    results = []
    query_parts = filter(None, query.query.split())

    if query_parts[0] not in answerers_by_keywords:
        return results

    for answerer in answerers_by_keywords[query_parts[0]]:
        result = answerer(query)
        if result:
            results.append(result)
    return results


answerers = load_answerers()
answerers_by_keywords = get_answerers_by_keywords(answerers)