-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation.py
50 lines (42 loc) · 1.39 KB
/
evaluation.py
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
47
48
49
50
from config import *
from abc import ABC, abstractmethod
import json
def _decode(o):
"""
Convert int in json as int and not as string
"""
if isinstance(o, str):
try:
return int(o)
except ValueError:
return o
elif isinstance(o, dict):
return {_decode(k): _decode(v) for k, v in o.items()}
elif isinstance(o, list):
return [_decode(v) for v in o]
else:
return o
class Evaluation(ABC):
def __init__(self, request, collection):
self.request = request
self.collection = collection
self.documents = self.load("documents")
self.terms = self.load("terms")
self.index = self.load("index")
def load(self, file):
with open(index_path + "/" + file + "_" + self.collection + ".json") as f:
if file == "terms":
text = json.load(f)
else:
text = json.load(f, object_hook=_decode)
f.close()
return text
def display_results(self, doc_ids, total_results):
if total_results is None:
total_results = len(doc_ids)
for doc_id in doc_ids:
print(doc_id)
print(self.documents[doc_id])
print()
print(str(len(doc_ids)) + " result(s) displayed")
print("There are {} result(s) corresponding to the request.".format(total_results))