1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-15 09:10:12 +01:00
searxng/searx/answerers/statistics/answerer.py

54 lines
1.2 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring
2016-11-19 21:03:27 +01:00
from functools import reduce
from operator import mul
from flask_babel import gettext
2016-11-30 18:43:03 +01:00
keywords = ('min', 'max', 'avg', 'sum', 'prod')
2016-11-19 21:03:27 +01:00
# 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:
2016-11-30 18:43:03 +01:00
args = list(map(float, parts[1:]))
except: # pylint: disable=bare-except
2016-11-19 21:03:27 +01:00
return []
func = parts[0]
_answer = None
2016-11-19 21:03:27 +01:00
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)
2016-11-19 21:03:27 +01:00
if _answer is None:
2016-11-19 21:03:27 +01:00
return []
return [{'answer': str(_answer)}]
2016-11-19 21:03:27 +01:00
# 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'],
}