debeir.core.results

 1from typing import List
 2
 3from debeir.core.document import Document, document_factory
 4
 5LAZY_STATIC_DOCUMENT_KEY = "document_objects"
 6LAZY_STATIC_DOCUMENT_TOPICS = "document_topics"
 7LAZY_STATIC_DOCUMENT_HASHMAP = "document_topics"
 8
 9
10class Results:
11    document_cls: Document
12
13    def __init__(self, results: List, query_cls, engine_name):
14        self.results = results
15        self.document_cls: Document = document_factory[engine_name]
16        self.__doc_cur = 0
17        self.__topic_num = None
18        self.lazy_static = {}
19        self.query_cls = query_cls
20        self.topic_flag = False
21
22    def _as_documents(self, recompile=False):
23        if recompile or 'document_objects' not in self.lazy_static:
24            self.lazy_static[LAZY_STATIC_DOCUMENT_KEY] = self.document_cls.from_results(self.results,
25                                                                                        self.query_cls,
26                                                                                        ignore_facets=False)
27            self.lazy_static[LAZY_STATIC_DOCUMENT_TOPICS] = list(self.lazy_static[LAZY_STATIC_DOCUMENT_KEY].keys())
28
29        return self.lazy_static[LAZY_STATIC_DOCUMENT_KEY]
30
31    def get_topic_ids(self):
32        if LAZY_STATIC_DOCUMENT_KEY not in self.lazy_static:
33            self._as_documents()
34
35        return self.lazy_static[LAZY_STATIC_DOCUMENT_TOPICS]
36
37    def __iter__(self):
38        self._as_documents()
39        self.__doc_cur = 0
40
41        if not self.__topic_num:
42            self.__topic_num = 0
43
44        return self
45
46    def __next__(self):
47        if self.topic_flag:
48            topic_num = self.__topic_num
49        else:
50            topic_num = self.get_topic_ids()[self.__topic_num]
51
52        if self.__doc_cur >= len(self._as_documents()[topic_num]):
53            self.__doc_cur = 0
54            self.__topic_num += 1
55
56            if self.topic_flag or self.__topic_num >= len(self.get_topic_ids()):
57                raise StopIteration
58
59            topic_num = self.get_topic_ids()[self.__topic_num]
60
61        item = self._as_documents()[topic_num][self.__doc_cur]
62        self.__doc_cur += 1
63
64        return item
65
66    def __call__(self, topic_num=None):
67        self.__topic_num = topic_num
68        if topic_num:
69            self.topic_flag = True
70
71        return self
72
73    def __getitem__(self, item):
74        return self._as_documents()[item]
class Results:
11class Results:
12    document_cls: Document
13
14    def __init__(self, results: List, query_cls, engine_name):
15        self.results = results
16        self.document_cls: Document = document_factory[engine_name]
17        self.__doc_cur = 0
18        self.__topic_num = None
19        self.lazy_static = {}
20        self.query_cls = query_cls
21        self.topic_flag = False
22
23    def _as_documents(self, recompile=False):
24        if recompile or 'document_objects' not in self.lazy_static:
25            self.lazy_static[LAZY_STATIC_DOCUMENT_KEY] = self.document_cls.from_results(self.results,
26                                                                                        self.query_cls,
27                                                                                        ignore_facets=False)
28            self.lazy_static[LAZY_STATIC_DOCUMENT_TOPICS] = list(self.lazy_static[LAZY_STATIC_DOCUMENT_KEY].keys())
29
30        return self.lazy_static[LAZY_STATIC_DOCUMENT_KEY]
31
32    def get_topic_ids(self):
33        if LAZY_STATIC_DOCUMENT_KEY not in self.lazy_static:
34            self._as_documents()
35
36        return self.lazy_static[LAZY_STATIC_DOCUMENT_TOPICS]
37
38    def __iter__(self):
39        self._as_documents()
40        self.__doc_cur = 0
41
42        if not self.__topic_num:
43            self.__topic_num = 0
44
45        return self
46
47    def __next__(self):
48        if self.topic_flag:
49            topic_num = self.__topic_num
50        else:
51            topic_num = self.get_topic_ids()[self.__topic_num]
52
53        if self.__doc_cur >= len(self._as_documents()[topic_num]):
54            self.__doc_cur = 0
55            self.__topic_num += 1
56
57            if self.topic_flag or self.__topic_num >= len(self.get_topic_ids()):
58                raise StopIteration
59
60            topic_num = self.get_topic_ids()[self.__topic_num]
61
62        item = self._as_documents()[topic_num][self.__doc_cur]
63        self.__doc_cur += 1
64
65        return item
66
67    def __call__(self, topic_num=None):
68        self.__topic_num = topic_num
69        if topic_num:
70            self.topic_flag = True
71
72        return self
73
74    def __getitem__(self, item):
75        return self._as_documents()[item]
Results(results: List, query_cls, engine_name)
14    def __init__(self, results: List, query_cls, engine_name):
15        self.results = results
16        self.document_cls: Document = document_factory[engine_name]
17        self.__doc_cur = 0
18        self.__topic_num = None
19        self.lazy_static = {}
20        self.query_cls = query_cls
21        self.topic_flag = False
def get_topic_ids(self):
32    def get_topic_ids(self):
33        if LAZY_STATIC_DOCUMENT_KEY not in self.lazy_static:
34            self._as_documents()
35
36        return self.lazy_static[LAZY_STATIC_DOCUMENT_TOPICS]