diff options
author | Adam Tauber <asciimoo@gmail.com> | 2016-11-19 21:03:27 +0100 |
---|---|---|
committer | Adam Tauber <asciimoo@gmail.com> | 2016-11-19 21:03:27 +0100 |
commit | 7eed8a5dd909ca70d45fdff5706bb207e6aa8bdf (patch) | |
tree | b5d22bc4ca45ac980fbfb0ec6cc719230ed98737 /searx/answerers | |
parent | 971ed0abd159625bd01d0b3ca52c90b394711d77 (diff) | |
download | searxng-7eed8a5dd909ca70d45fdff5706bb207e6aa8bdf.tar.gz searxng-7eed8a5dd909ca70d45fdff5706bb207e6aa8bdf.zip |
[enh] add statistics answerer
Diffstat (limited to 'searx/answerers')
-rw-r--r-- | searx/answerers/statistics/answerer.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/searx/answerers/statistics/answerer.py b/searx/answerers/statistics/answerer.py new file mode 100644 index 000000000..3fef69c60 --- /dev/null +++ b/searx/answerers/statistics/answerer.py @@ -0,0 +1,51 @@ +from functools import reduce +from operator import mul + +from flask_babel import gettext + +keywords = ('min', + 'max', + 'avg', + 'sum', + 'prod') + + +# required answerer function +# can return a list of results (any result type) for a given query +def answer(query): + parts = query.query.split() + + if len(parts) < 2: + return [] + + try: + args = map(float, parts[1:]) + except: + return [] + + func = parts[0] + answer = None + + if func == 'min': + answer = min(args) + elif func == 'max': + answer = max(args) + elif func == 'avg': + answer = sum(args)/len(args) + elif func == 'sum': + answer = sum(args) + elif func == 'prod': + answer = reduce(mul, args, 1) + + if answer is None: + return [] + + return [{'answer': unicode(answer)}] + + +# required answerer function +# returns information about the answerer +def self_info(): + return {'name': gettext('Statistics functions'), + 'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)), + 'examples': ['avg 123 548 2.04 24.2']} |