-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_dataset.py
222 lines (194 loc) · 7.5 KB
/
import_dataset.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Code for Information Retrieval course project @ University of Trieste, MSc in Data Science & Scientific Computing A.Y. 2023/2024.
Author: Michele Alessi
This file contains the code to import the datasets inside the ../data/ folder.
"""
import re
def import_TIME():
"""
This function imports the content of ../data/time/ folder.
num of articles: 423
num of queries: 83
Returns:
articles: (list) List of strings. articles[i-1] is the i-th article of TIME.ALL file.
splitted_articles: (list) List of lists. splitted_articles[i-1] is the i-th article of TIME.ALL file, splitted in words.
queries: (list) List of strings. queries[i-1] is the i-th query of TIME.QUE file.
splitted_queries: (list) List of lists. splitted_queries[i-1] is the i-th query of TIME.QUE file, splitted in words.
relevances: (list) List of lists. relevances[i] is the list of relevant documents for query in queries[i].
"""
splitted_articles = []
with open('./data/time/TIME.ALL', 'r') as f:
tmp = []
for row in f:
if row.startswith("*TEXT"):
if tmp != []:
splitted_articles.append(tmp)
tmp = []
else:
row = re.sub(r'[^a-zA-Z\s]+', '', row)
tmp += row.split()
if tmp != []:
splitted_articles.append(tmp)
articles = []
for doc in splitted_articles:
articles.append(' '.join(doc))
splitted_queries = []
with open('./data/time/TIME.QUE', 'r') as f:
tmp = []
for row in f:
if row.startswith("*FIND"):
if tmp != []:
splitted_queries.append(tmp)
tmp = []
else:
row = re.sub(r'[^a-zA-Z\s]+', '', row)
tmp += row.split()
if tmp != []:
splitted_queries.append(tmp)
queries = []
for query in splitted_queries:
queries.append(' '.join(query))
relevances = []
with open('./data/time/TIME.REL', 'r') as f:
# iterate over the rows in the file
for row in f:
# if the row is empty, skip it
if not row.strip():
continue
# read the content of the row as integers and split it
content = [int(x) for x in row.split()]
relevances.append(content[1:])
return articles, splitted_articles, queries, splitted_queries, relevances
def import_MED():
"""
This function imports the content of ../data/med/ folder.
num of articles: 1033
num of queries: 29
Returns:
articles: (list) List of strings. articles[i-1] is the i-th article of MED.ALL file.
splitted_articles: (list) List of lists. splitted_articles[i-1] is the i-th article of MED.ALL file, splitted in words.
queries: (list) List of strings. queries[i-1] is the i-th query of MED.QUE file.
splitted_queries: (list) List of lists. splitted_queries[i-1] is the i-th query of MED.QUE file, splitted in words.
relevances: (list) List of lists. relevances[i] is the list of relevant documents for query in queries[i].
"""
splitted_articles = []
with open('./data/med/MED.ALL', 'r') as f:
tmp = []
for row in f:
if row.startswith(".I"):
while not row.startswith(".W"):
row = f.readline()
if tmp != []:
splitted_articles.append(tmp)
tmp = []
else:
row = re.sub(r'[^a-zA-Z\s]+', '', row)
row = row.upper()
tmp += row.split()
if tmp != []:
splitted_articles.append(tmp)
articles = []
for doc in splitted_articles:
articles.append(' '.join(doc))
splitted_queries = []
with open('./data/med/MED.QRY', 'r') as f:
tmp = []
for row in f:
if row.startswith(".I"):
while not row.startswith(".W"):
row = f.readline()
if tmp != []:
splitted_queries.append(tmp)
tmp = []
else:
row = re.sub(r'[^a-zA-Z\s]+', '', row)
row = row.upper()
tmp += row.split()
if tmp != []:
splitted_queries.append(tmp)
queries = []
for query in splitted_queries:
queries.append(' '.join(query))
relevances = []
with open('./data/med/MED.REL', 'r') as f:
tmp = []
firstrow = [int(x) for x in f.readline().split()]
current = firstrow[0]
tmp.append(firstrow[2])
for row in f:
row = [int(x) for x in row.split()]
if row[0] == current:
tmp.append(row[2])
else:
relevances.append(tmp)
tmp = []
current = row[0]
tmp.append(row[2])
return articles, splitted_articles, queries, splitted_queries, relevances
def import_CRAN():
"""
This function imports the content of ../data/cran/ folder.
num of articles: 1398
num of queries: 225
Returns:
articles: (list) List of strings. articles[i-1] is the i-th article of cran.all.1400 file.
splitted_articles: (list) List of lists. splitted_articles[i-1] is the i-th article of cran.all.1400 file, splitted in words.
queries: (list) List of strings. queries[i-1] is the i-th query of cran.qry file.
splitted_queries: (list) List of lists. splitted_queries[i-1] is the i-th query of cran.qry file, splitted in words.
relevances: (list) List of lists. relevances[i] is the list of relevant documents for query in queries[i].
"""
splitted_articles = []
with open('./data/cran/cran.all.1400', 'r') as f:
tmp = []
for row in f:
if row.startswith(".I"):
while not row.startswith(".W"):
row = f.readline()
if tmp != []:
splitted_articles.append(tmp)
tmp = []
else:
row = re.sub(r'[^a-zA-Z\s]+', '', row)
row = row.upper()
tmp += row.split()
if tmp != []:
splitted_articles.append(tmp)
articles = []
for doc in splitted_articles:
articles.append(' '.join(doc))
splitted_queries = []
with open('./data/cran/cran.qry', 'r') as f:
tmp = []
for row in f:
if row.startswith(".I"):
while not row.startswith(".W"):
row = f.readline()
if tmp != []:
splitted_queries.append(tmp)
tmp = []
else:
row = re.sub(r'[^a-zA-Z\s]+', '', row)
row = row.upper()
tmp += row.split()
if tmp != []:
splitted_queries.append(tmp)
queries = []
for query in splitted_queries:
queries.append(' '.join(query))
relevances = []
with open('./data/cran/cran.rel', 'r') as f:
tmp = []
firstrow = [int(x) for x in f.readline().split()]
current = firstrow[0]
tmp.append(firstrow[1])
for row in f:
row = [int(x) for x in row.split()]
if row[0] == current:
tmp.append(row[1])
else:
relevances.append(tmp)
tmp = []
current = row[0]
tmp.append(row[1])
relevances.append(tmp)
return articles, splitted_articles, queries, splitted_queries, relevances