Browse Source

[enh] add statistics answerer

Adam Tauber 8 years ago
parent
commit
7eed8a5dd9
1 changed files with 51 additions and 0 deletions
  1. 51
    0
      searx/answerers/statistics/answerer.py

+ 51
- 0
searx/answerers/statistics/answerer.py View File

@@ -0,0 +1,51 @@
1
+from functools import reduce
2
+from operator import mul
3
+
4
+from flask_babel import gettext
5
+
6
+keywords = ('min',
7
+            'max',
8
+            'avg',
9
+            'sum',
10
+            'prod')
11
+
12
+
13
+# required answerer function
14
+# can return a list of results (any result type) for a given query
15
+def answer(query):
16
+    parts = query.query.split()
17
+
18
+    if len(parts) < 2:
19
+        return []
20
+
21
+    try:
22
+        args = map(float, parts[1:])
23
+    except:
24
+        return []
25
+
26
+    func = parts[0]
27
+    answer = None
28
+
29
+    if func == 'min':
30
+        answer = min(args)
31
+    elif func == 'max':
32
+        answer = max(args)
33
+    elif func == 'avg':
34
+        answer = sum(args)/len(args)
35
+    elif func == 'sum':
36
+        answer = sum(args)
37
+    elif func == 'prod':
38
+        answer = reduce(mul, args, 1)
39
+
40
+    if answer is None:
41
+        return []
42
+
43
+    return [{'answer': unicode(answer)}]
44
+
45
+
46
+# required answerer function
47
+# returns information about the answerer
48
+def self_info():
49
+    return {'name': gettext('Statistics functions'),
50
+            'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)),
51
+            'examples': ['avg 123 548 2.04 24.2']}