|
12 | 12 | configure_logging(level=logging.INFO) |
13 | 13 | log = logging.getLogger(__name__) |
14 | 14 |
|
| 15 | + |
| 16 | +TMP_FILE = "./test_file.json" |
| 17 | + |
15 | 18 | class DataStore: |
16 | 19 | """ |
17 | 20 | Stores/retrieves documents, queries, and rating scores. |
@@ -117,72 +120,75 @@ def has_rating_score(self, query_id: str, doc_id: str) -> bool: |
117 | 120 | return context.has_rating_score(doc_id) |
118 | 121 |
|
119 | 122 |
|
120 | | - def save_queries_and_docs(self, filepath: str | Path) -> None: |
121 | | - """ |
122 | | - Saves all query contexts (query_id, text, doc_ids) to a JSON file. |
123 | | - """ |
124 | | - data = [ |
125 | | - { |
126 | | - "query_id": ctx.get_query_id(), |
127 | | - "query_text": ctx.get_query(), |
128 | | - "doc_ids": ctx.get_doc_ids() |
129 | | - } |
130 | | - for ctx in self._queries_by_id.values() |
131 | | - ] |
| 123 | + @staticmethod |
| 124 | + def query_context_docs_to_dict(query: QueryRatingContext, documents: List[Document]) -> dict: |
| 125 | + return { |
| 126 | + "query_id": query.get_query_id(), |
| 127 | + "query_text": query.get_query(), |
| 128 | + "doc_ids": query.get_doc_ids(), |
| 129 | + "doc_ratings": query._doc_id_to_rating_score.copy(), # copy to avoid mutation |
| 130 | + "documents": [doc.model_dump_json() for doc in documents if doc is not None] |
| 131 | + } |
| 132 | + |
| 133 | + @staticmethod |
| 134 | + def dict_to_query_context_docs(dict_: dict) -> tuple[str, str, list[str], dict[str, int], list[dict]]: |
| 135 | + query_id = dict_["query_id"] |
| 136 | + query_text = dict_["query_text"] |
| 137 | + doc_ids = dict_["doc_ids"] |
| 138 | + doc_ratings = dict_["doc_ratings"] |
| 139 | + documents = dict_.get("documents", []) |
| 140 | + return query_id, query_text, doc_ids, doc_ratings, documents |
| 141 | + |
| 142 | + |
| 143 | + def save_tmp_file_content(self, filepath: str | Path = None) -> None: |
| 144 | + """ |
| 145 | + Save _queries_by_id, _query_text_to_query_id, and self._documents from a unified JSON file in disk |
| 146 | + """ |
| 147 | + global TMP_FILE |
| 148 | + if filepath is None: |
| 149 | + filepath = TMP_FILE |
| 150 | + |
| 151 | + all_content = [] |
| 152 | + for query_ctx in self._queries_by_id.values(): |
| 153 | + docs_ = [self.get_document(doc_id) for doc_id in query_ctx.get_doc_ids()] |
| 154 | + data = self.query_context_docs_to_dict(query_ctx, docs_) |
| 155 | + all_content.append(data) |
| 156 | + |
132 | 157 | with open(filepath, "w", encoding="utf-8") as f: |
133 | | - json.dump(data, f, indent=2) |
| 158 | + json.dump(all_content, f, indent=2) |
134 | 159 |
|
135 | 160 |
|
136 | | - def load_queries_and_docs(self, filepath: str | Path) -> None: |
| 161 | + |
| 162 | + def load_tmp_file_content(self, filepath: str | Path = None) -> None: |
137 | 163 | """ |
138 | | - Loads query contexts from a JSON file. Reconstructs query_id, query text, and associated doc_ids. |
| 164 | + Reconstruct _queries_by_id, _query_text_to_query_id, and self._documents from disk file |
139 | 165 | """ |
140 | | - with open(filepath, "r", encoding="utf-8") as f: |
141 | | - data = json.load(f) |
142 | | - |
143 | | - for item in data: |
144 | | - context = QueryRatingContext(item["query_text"], doc_id=None) |
145 | | - context._id = item["query_id"] |
146 | | - for doc_id in item["doc_ids"]: |
147 | | - context.add_doc_id(doc_id) |
148 | | - self._queries_by_id[context.get_query_id()] = context |
149 | | - self._query_text_to_query_id[context.get_query()] = context.get_query_id() |
150 | | - |
151 | | - |
152 | | - def save_rating_triples(self, filepath: str | Path) -> None: |
153 | | - """ |
154 | | - Saves all (query_id, doc_id, score) triples to a JSON file. |
155 | | - """ |
156 | | - triples = [] |
157 | | - for _context in self._queries_by_id.values(): |
158 | | - query_id = _context.get_query_id() |
159 | | - for doc_id in _context.get_doc_ids(): |
160 | | - try: |
161 | | - score = _context.get_rating_score(doc_id) |
162 | | - triples.append({ |
163 | | - "query_id": query_id, |
164 | | - "doc_id": doc_id, |
165 | | - "score": score |
166 | | - }) |
167 | | - except KeyError: |
168 | | - continue |
169 | | - with open(filepath, "w", encoding="utf-8") as f: |
170 | | - json.dump(triples, f, indent=2) |
| 166 | + global TMP_FILE |
171 | 167 |
|
| 168 | + if filepath is None: |
| 169 | + filepath = TMP_FILE |
172 | 170 |
|
173 | | - def load_rating_triples(self, filepath: str | Path) -> None: |
174 | | - """ |
175 | | - Loads rating triples from a JSON file and updates existing QueryRatingContext entries. |
176 | | - """ |
177 | 171 | with open(filepath, "r", encoding="utf-8") as f: |
178 | | - triples = json.load(f) |
179 | | - |
180 | | - for triple in triples: |
181 | | - query_id = triple["query_id"] |
182 | | - doc_id = triple["doc_id"] |
183 | | - score = triple["score"] |
184 | | - if query_id not in self._queries_by_id: |
185 | | - raise ValueError(f"Query ID {query_id} not found when loading triples.") |
186 | | - self._queries_by_id[query_id].add_rating_score(doc_id, score) |
| 172 | + all_content = json.load(f) |
| 173 | + |
| 174 | + # Loop to reconstruct each register |
| 175 | + for entry in all_content: |
| 176 | + query_id, query_text, doc_ids, doc_ratings, documents = self.dict_to_query_context_docs(entry) |
| 177 | + |
| 178 | + # Register docs |
| 179 | + for doc_data in documents: |
| 180 | + document = Document.model_validate(doc_data) |
| 181 | + self.add_document(document.id, document) |
| 182 | + |
| 183 | + # Reconstruct the QueryRating |
| 184 | + context = QueryRatingContext(query=query_text) |
| 185 | + context._id = query_id # generated UUID |
| 186 | + |
| 187 | + for doc_id in doc_ids: |
| 188 | + context.add_doc_id(doc_id) |
187 | 189 |
|
| 190 | + for doc_id, rating in doc_ratings.items(): |
| 191 | + context.add_rating_score(doc_id, rating) |
188 | 192 |
|
| 193 | + self._queries_by_id[query_id] = context |
| 194 | + self._query_text_to_query_id[query_text] = query_id |
0 commit comments