-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
182 lines (150 loc) · 5.59 KB
/
reader.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
Authors:
Gonçalo Leal - 98008
Ricardo Rodriguez - 98388
"""
import os
import gzip
import json
from time import time
from utils import dynamically_init_class
def dynamically_init_reader(**kwargs):
return dynamically_init_class(__name__, **kwargs)
class Reader:
"""
Top-level Reader class
This loosly defines a class over the concept of
a reader.
Since there are multiple ways for implementing
this class, we did not defined any specific method
in this started code.
"""
def __init__(self,
path_to_collection:str,
**kwargs):
super().__init__()
self.path_to_collection = path_to_collection
class PubMedReader(Reader):
def __init__(self,
path_to_collection:str,
**kwargs):
super().__init__(path_to_collection, **kwargs)
print("init PubMedReader|", f"{self.path_to_collection=}")
if kwargs:
print(f"{self.__class__.__name__} also caught the following additional arguments {kwargs}")
self.extract_file()
def read_next_pub(self):
line = self.file.readline()
if not line:
return None, None # EOF (end of file)
pub_json = json.loads(line)
pmid = pub_json['pmid']
return pmid, pub_json["title"] + " " + pub_json["abstract"]
def extract_file(self):
self.file = gzip.open(self.path_to_collection, mode="rt")
def close_file(self):
self.file.close()
class QuestionsReader(Reader):
"""
This class will read and provide every query to the searcher function
"""
def __init__(
self,
path_to_questions:str,
**kwargs
):
super().__init__(path_to_questions, **kwargs)
# I do not want to refactor Reader and here path_to_collection does not make any sense.
# So consider using self.path_to_questions instead
# (but both variables point to the same thing, it just to not break old code)
self.path_to_questions = self.path_to_collection
print("init QuestionsReader|", f"{self.path_to_questions=}")
if kwargs:
print(
f"{self.__class__.__name__} also caught the following additional arguments {kwargs}"
)
self.open_file()
self.id = 0
self.has_results = False
def read_next_question(self):
"""
Read next query/question from questions file
Returns the id of the question (incremental starting in 1) and
a list with all the words on the query
"""
line = self.questions_file.readline()
if not line:
self.questions_file.close()
return None, None # end of file
self.id += 1
return self.id, line.strip()
def open_file(self):
"""
Check if file exists and open it
"""
if self.path_to_questions == "":
return
if not os.path.exists(self.path_to_questions):
raise FileNotFoundError
if not os.path.isfile(self.path_to_questions):
raise IsADirectoryError
if os.path.splitext(self.path_to_questions)[1] != ".txt":
raise Exception("only txt files allowed")
self.questions_file = open(self.path_to_questions, "r")
class GsQuestionsReader(Reader):
"""
This class will read and provide every query to the searcher function
"""
def __init__(
self,
path_to_questions:str,
**kwargs
):
super().__init__(path_to_questions, **kwargs)
# I do not want to refactor Reader and here path_to_collection does not make any sense.
# So consider using self.path_to_questions instead
# (but both variables point to the same thing, it just to not break old code)
self.path_to_questions = self.path_to_collection
print("init GsQuestionsReader|", f"{self.path_to_questions=}")
if kwargs:
print(
f"{self.__class__.__name__} also caught the following additional arguments {kwargs}"
)
self.open_file()
self.has_results = False
# this will work as a cache for the results of the current question
self.current_line = ""
def read_next_question(self):
"""
Read next query/question from questions file
Returns the question id and a string with the query
"""
line = self.questions_file.readline()
if not line:
self.questions_file.close()
self.current_line = None
return None, None # EOF (end of file)
self.current_line = json.loads(line)
self.has_results = "documents_pmid" in self.current_line
return self.current_line['query_id'], self.current_line["query_text"]
def read_current_result(self):
"""
Read the results from the current query from questions file
Returns the question id and a list with all the results
"""
if not self.current_line:
return None, None
return self.current_line['query_id'], self.current_line["documents_pmid"]
def open_file(self):
"""
Check if file exists and open it
"""
if self.path_to_questions == "":
return
if not os.path.exists(self.path_to_questions):
raise FileNotFoundError
if not os.path.isfile(self.path_to_questions):
raise IsADirectoryError
if os.path.splitext(self.path_to_questions)[1] != ".jsonl":
raise Exception("only jsonl files allowed")
self.questions_file = open(self.path_to_questions, "r")