crystina-z commited on
Commit
2e90bb1
·
verified ·
1 Parent(s): ed087a5

Update xor-tydi.py

Browse files
Files changed (1) hide show
  1. xor-tydi.py +31 -22
xor-tydi.py CHANGED
@@ -108,39 +108,49 @@ class XORTyDi(datasets.GeneratorBasedBuilder):
108
  return splits
109
 
110
  def _generate_examples(self, files):
111
- """Yields examples."""
112
- # def process_train_entry(data):
113
- # if data.get('negative_passages') is None:
114
- # data['negative_passages'] = []
115
- # if data.get('positive_passages') is None:
116
- # data['positive_passages'] = []
117
- # if data.get('answers') is None:
118
- # data['answers'] = []
119
- # return data['query_id'], data
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  def process_train_entry(data, _id):
122
  positive_ctxs = data["positive_ctxs"]
123
  hard_negative_ctxs = data["hard_negative_ctxs"]
124
  # each ctx: {'title':... , 'text': ....}
125
 
126
  def process_ctx(ctxs, tag):
127
- # assert len(doc["text"]) == 1
128
- for doc in ctxs:
129
- if isinstance(doc["text"], list):
130
- assert len(doc["text"]) == 1
131
- else:
132
- assert isinstance(doc["text"], str)
133
-
134
  return [
135
  {
136
  "title": doc["title"],
137
- "text": doc["text"][0] if isinstance(doc["text"], list) else doc["text"],
138
- 'docid': f'{tag}-{i}-{random.randint(*RANGE)}'
 
139
  } for i, doc in enumerate(ctxs)
140
  ]
141
 
142
- _id = f"{_id}-{random.randint(*RANGE)}"
143
- # import pdb; pdb.set_trace()
144
  return _id, {
145
  "query_id": _id,
146
  "query": data["question"],
@@ -160,8 +170,7 @@ class XORTyDi(datasets.GeneratorBasedBuilder):
160
  "negative_passages": [],
161
  }
162
 
163
- assert len(files) == 1
164
- filepath = files[0]
165
  try:
166
  with open(filepath, encoding="utf-8") as f:
167
  all_data = json.load(f)
 
108
  return splits
109
 
110
  def _generate_examples(self, files):
111
+ assert len(files) == 1
112
+ filepath = files[0]
 
 
 
 
 
 
 
113
 
114
+
115
+ def process_doc_text(doc):
116
+ if isinstance(doc["text"], list):
117
+ assert len(doc["text"]) == 1
118
+ return doc['text'][0].strip()
119
+ else:
120
+ assert isinstance(doc["text"], str)
121
+ return doc['text'].strip()
122
+
123
+
124
+ # prepare doc
125
+ doc2docid = {}
126
+ with open(filepath, encoding="utf-8") as f:
127
+ all_data = json.load(f)
128
+ for i, data in enumerate(all_data):
129
+ positive_ctxs = data["positive_ctxs"]
130
+ hard_negative_ctxs = data["hard_negative_ctxs"]
131
+ ctxs = positive_ctxs + hard_negative_ctxs
132
+
133
+ for doc for ctxs:
134
+ text = process_doc_text[doc]
135
+ if text not in doc2docid:
136
+ doc2docid[text] = len(doc2docid)
137
+
138
  def process_train_entry(data, _id):
139
  positive_ctxs = data["positive_ctxs"]
140
  hard_negative_ctxs = data["hard_negative_ctxs"]
141
  # each ctx: {'title':... , 'text': ....}
142
 
143
  def process_ctx(ctxs, tag):
144
+ text = process_doc_text(doc)
 
 
 
 
 
 
145
  return [
146
  {
147
  "title": doc["title"],
148
+ "text":
149
+ # 'docid': f'{tag}-{i}-{random.randint(*RANGE)}'
150
+ 'docid': doc2docid[docid]
151
  } for i, doc in enumerate(ctxs)
152
  ]
153
 
 
 
154
  return _id, {
155
  "query_id": _id,
156
  "query": data["question"],
 
170
  "negative_passages": [],
171
  }
172
 
173
+
 
174
  try:
175
  with open(filepath, encoding="utf-8") as f:
176
  all_data = json.load(f)