Markus28 commited on
Commit
2b5c64d
1 Parent(s): 8583301

supportboth s2s and s2p

Browse files
Files changed (1) hide show
  1. spanish_passage_retrieval.py +32 -15
spanish_passage_retrieval.py CHANGED
@@ -37,7 +37,7 @@ class PRES(datasets.GeneratorBasedBuilder):
37
  name=name,
38
  description=f'{name.title()} of the Spanish Passage Retrieval dataset.',
39
  )
40
- for name in ['corpus', 'queries', 'qrels']
41
  ]
42
 
43
  BUILDER_CONFIG_CLASS = PRESConfig
@@ -91,9 +91,14 @@ class PRES(datasets.GeneratorBasedBuilder):
91
  with open(os.path.join(extracted_path, 'relevance_passages.json')) as f:
92
  rel_passages = json.load(f)
93
 
94
- corpus = []
 
 
 
 
95
  queries = dict()
96
- qrels = dict()
 
97
  topic_to_queries = dict()
98
  for topic in topics['topics']:
99
  topic_to_queries[topic['number']] = []
@@ -101,24 +106,38 @@ class PRES(datasets.GeneratorBasedBuilder):
101
  qid = query['number']
102
  queries[qid] = query['text']
103
  topic_to_queries[topic['number']].append(qid)
104
- qrels[qid] = []
 
105
 
106
  for annotated_topic in rel_passages['topics']:
107
  topic = annotated_topic['number']
108
  for annotation in annotated_topic['annotations']:
109
  did = f'doc_{annotation["docNo"]}_{annotation["start"]}_{annotation["end"]}'
110
- corpus.append({'_id': did, 'text': annotation['text']})
111
  for qid in topic_to_queries[topic]:
112
- qrels[qid].append(did)
 
 
 
 
 
 
 
 
113
 
114
  self._data = {
115
- 'corpus': corpus,
 
116
  'queries': queries,
117
- 'qrels': qrels,
 
118
  }
119
 
120
- if self.config.name == 'corpus':
121
- for line in self._data['corpus']:
 
 
 
122
  yield line['_id'], line
123
  elif self.config.name == 'queries':
124
  for qid, query in self._data['queries'].items():
@@ -126,11 +145,9 @@ class PRES(datasets.GeneratorBasedBuilder):
126
  "_id": qid,
127
  "text": query,
128
  }
129
- elif self.config.name == 'qrels':
130
- for qid, dids in self._data['qrels'].items():
131
  yield qid, {
132
  "_id": qid,
133
  "text": ' '.join(dids),
134
- }
135
- else:
136
- raise ValueError(f'Unknown config name: {self.config.name}')
 
37
  name=name,
38
  description=f'{name.title()} of the Spanish Passage Retrieval dataset.',
39
  )
40
+ for name in ['corpus.sentences', 'corpus.documents', 'queries', 'qrels.s2s', 'qrels.s2p']
41
  ]
42
 
43
  BUILDER_CONFIG_CLASS = PRESConfig
 
91
  with open(os.path.join(extracted_path, 'relevance_passages.json')) as f:
92
  rel_passages = json.load(f)
93
 
94
+ with open(os.path.join(extracted_path, 'relevance_documents.tsv')) as f:
95
+ rel_docs = [(item, item[2]) for item in list(csv.reader(f))[1:]]
96
+
97
+ corpus_sentences = []
98
+ corpus_documents = []
99
  queries = dict()
100
+ qrels_s2s = dict()
101
+ qrels_s2p = dict()
102
  topic_to_queries = dict()
103
  for topic in topics['topics']:
104
  topic_to_queries[topic['number']] = []
 
106
  qid = query['number']
107
  queries[qid] = query['text']
108
  topic_to_queries[topic['number']].append(qid)
109
+ qrels_s2s[qid] = []
110
+ qrels_s2p[qid] = []
111
 
112
  for annotated_topic in rel_passages['topics']:
113
  topic = annotated_topic['number']
114
  for annotation in annotated_topic['annotations']:
115
  did = f'doc_{annotation["docNo"]}_{annotation["start"]}_{annotation["end"]}'
116
+ corpus_sentences.append({'_id': did, 'text': annotation['text']})
117
  for qid in topic_to_queries[topic]:
118
+ qrels_s2s[qid].append(did)
119
+
120
+ for doc in docs['documents']:
121
+ did = f'doc_{doc["docId"]}'
122
+ corpus_documents.append({'_id': did, 'text': doc['text']})
123
+
124
+ for topic_id, doc_id in rel_docs:
125
+ for q_num in topic_to_queries[topic_id]:
126
+ qrels_s2p[f'PR_ES_{q_num}'].append(f'doc_{doc_id}')
127
 
128
  self._data = {
129
+ 'corpus.sentences': corpus_sentences,
130
+ 'corpus.documents': corpus_documents,
131
  'queries': queries,
132
+ 'qrels.s2s': qrels_s2s,
133
+ 'qrels.s2p': qrels_s2p
134
  }
135
 
136
+ if self._config.name not in self._data:
137
+ raise ValueError(f'Unknown config name: {self.config.name}')
138
+
139
+ if self.config.name.startswith('corpus'):
140
+ for line in self._data[self.config.name]:
141
  yield line['_id'], line
142
  elif self.config.name == 'queries':
143
  for qid, query in self._data['queries'].items():
 
145
  "_id": qid,
146
  "text": query,
147
  }
148
+ elif self.config.name.startswith('qrels'):
149
+ for qid, dids in self._data[self.config.name].items():
150
  yield qid, {
151
  "_id": qid,
152
  "text": ' '.join(dids),
153
+ }