File size: 19,501 Bytes
436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 aebec90 436b034 |
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current working directory: /Users/lucas/Code/jeopardy\n"
]
}
],
"source": [
"import json\n",
"from string import punctuation, whitespace\n",
"import os\n",
"from copy import deepcopy\n",
"from hashlib import md5\n",
"import re\n",
"from functools import reduce\n",
"import datasets\n",
"import smart_open\n",
"\n",
"punctuation, whitespace = map(set, [punctuation, whitespace])\n",
"print(f\"Current working directory: {os.getcwd()}\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"with smart_open.open(\"raw/test.jsonl\", \"r\") as f:\n",
" new_questions_raw = [json.loads(line) for line in f]\n",
" new_questions = {''.join([ch for ch in f\"{q['context']} {q['continuation']}\" if ch not in punctuation and ch not in whitespace]): q for q in new_questions_raw}\n",
"\n",
"with smart_open.open(\"raw/JEOPARDY_QUESTIONS1.json.zst\", \"r\") as f:\n",
" old_questions_raw = json.load(f)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"double_quote, single_quote = '\"', \"'\"\n",
"\n",
"actually_to_fix = {\n",
" \"White alba\": \"White\",\n",
" \"The Netherlands Holland\": \"The Netherlands\",\n",
" \"Peter I Peter the Great\": \"Peter I\",\n",
" \"Russia or the Soviet Union\": \"Russia\",\n",
" \"Ramses the Great\": \"Ramses\",\n",
" \"a bar or a saloon\": \"a bar\",\n",
" \"West Virginia paid debt to Virginia\": \"West Virginia\",\n",
" \"Robert Fulton\": \"Fulton\",\n",
" \"CIA Central Intelligence Agency\": \"CIA\",\n",
" \"NATO North Atlantic Treaty Organisation\": \"NATO\",\n",
" \"NRA National Recovery Administration\": \"NRA\",\n",
" \"the ERA Equal Rights Amendment\": \"the ERA\",\n",
" \"Ford\\'s Theatre the Ford Theatre accepted\": \"Fords Theatre\",\n",
" \"NATO North Atlantic Treaty Organization\": \"NATO\",\n",
" \"Lyndon Johnson LBJ\": \"Lyndon Johnson\",\n",
" \"Lewis Carroll Charles L. Dodgson\": \"Lewis Carroll\",\n",
" \"Tropic of Cancer by Henry Miller\": \"Tropic of Cancer\",\n",
" \"an inclined plane ramp later ruled acceptable\": \"an inclined plane\",\n",
" \"nitrous oxide or laughing gas\": \"nitrous oxide\",\n",
" \"dividing splitting in two\": \"dividing\",\n",
" \"monosodium glutamate or MSG\": \"monosodium glutamate\",\n",
" \"carbohydrates or starches\": \"carbohydrates\",\n",
" \"vamoose vamonos\": \"vamoose\",\n",
" \"Dunce from John Duns Scotus\": \"Dunce\",\n",
" \"The People's Republic of China\": \"China\",\n",
" \"The People\\\\'s Republic of China\": \"China\",\n",
" \"Mary Stuart Mary, Queen of Scots\": \"Mary Stuart\",\n",
" \"Robert F. Kennedy Attorney General\": \"Robert F. Kennedy\",\n",
" \"Ford\\\\'s Theatre the Ford Theatre accepted\": \"Fords Theatre\",\n",
" \"The Stranger L\\\\'Etranger\": \"The Stranger\",\n",
" \"Côte d\\\\'Ivoire or the Ivory Coast\": \"Côte d'Ivoire\",\n",
" \"Henry David Thoreau\": \"Thoreau\",\n",
" \"escaping the Earth\\\\'s gravity and go off into outer space, on your way to the moon, for instance\":\"escaping the Earths gravity\",\n",
" \"Ernst Mach\": \"Mach\",\n",
" \"talons or claws\": \"talons\",\n",
" \"Mall from Pall Mall\": \"Mall\",\n",
" \"Crony from the Greek god Cronus\": \"Crony\",\n",
" \"hurry or quick or fast\": \"hurry\",\n",
" \"Dr. Samuel Mudd\": \"Mudd\",\n",
" \"Anniversary annus - year & verso - turn\": \"Anniversary\",\n",
"}\n",
"\n",
"replacements = {\n",
" \"White alba\": \"White\",\n",
" \"Valentina Tereshkova\": \"Tereshkova\",\n",
" \"Mikhail Gorbachev\": \"Gorbachev\",\n",
" \"Admiral Byrd\": \"Byrd\",\n",
" \"Red China\": \"China\",\n",
" \"The Netherlands Holland\": \"The Netherlands\",\n",
" \"Heinrich Schliemann\": \"Schliemann\",\n",
" \"Peter I Peter the Great\": \"Peter I\",\n",
" \"Russia or the Soviet Union\": \"Russia\",\n",
" \"Ferdinand II\": \"Ferdinand\",\n",
" \"Ramses the Great\": \"Ramses\",\n",
" \"Jose de San Martin\": \"San Martin\",\n",
" \"Douglas McArthur\": \"McArthur\",\n",
" \"Rutherford B. Hayes\": \"Hayes\",\n",
" \"Peter Stuyvesant\": \"Stuyvesant\",\n",
" \"a bar or a saloon\": \"a bar \",\n",
" \"West Virginia paid debt to Virginia\": \"West Virginia \",\n",
" \"Robert Fulton\": \"Fulton\",\n",
" \"CIA Central Intelligence Agency\": \"CIA \",\n",
" # remove later\n",
" \"1 of want, speech, religion, or fear\": \"1 of \",\n",
" \"James Madison\": \"Madison\",\n",
" \"Alger Hiss\": \"Hiss\",\n",
" \"NATO North Atlantic Treaty Organisation\": \"NATO \",\n",
" \"James Oglethorpe\": \"Oglethorpe\",\n",
" \"NRA National Recovery Administration\": \"NRA \",\n",
" \"the ERA Equal Rights Amendment\": \"the ERA \",\n",
" \"Ford\\'s Theatre the Ford Theatre accepted\": \"Fords Theatre \",\n",
" \"NATO North Atlantic Treaty Organization\": \"NATO \",\n",
" \"Lyndon Johnson LBJ\": \"Lyndon Johnson \",\n",
" \"Nathaniel Hawthorne\": \"Hawthorne\",\n",
" \"Lewis Carroll Charles L. Dodgson\": \"Lewis Carroll\",\n",
" \"Tropic of Cancer by Henry Miller\": \"Tropic of Cancer\",\n",
" \"James Fenimore Cooper\": \"Cooper\",\n",
" \"Rudyard Kipling\": \"Kipling\",\n",
" \"William Burroughs\": \"Burroughs\",\n",
" \"purple Riders of the Purple Sage\": \"purple\",\n",
" \"Sir Arthur Conan Doyle\": \"Doyle\",\n",
" \"Samuel Beckett\": \"Beckett\",\n",
" \"Percy Shelley\": \"Shelley\",\n",
" \"an inclined plane ramp later ruled acceptable\": \"an inclined plane\",\n",
" \"nitrous oxide or laughing gas\": \"nitrous oxide\",\n",
" \"dividing splitting in two\": \"dividing\",\n",
" \"Edward Jenner\": \"Jenner\",\n",
" \"Spiral\": \"Spiral\",\n",
" \"monosodium glutamate or MSG\": \"monosodium glutamate\",\n",
" \"carbohydrates or starches\": \"carbohydrates\",\n",
" \"Dog Laika\": \"Dog\",\n",
" \"Horse Mare\": \"Horse\",\n",
" \"Sir Isaac Newton\": \"Newton\",\n",
" \"vamoose vamonos\": \"vamoose\",\n",
" \"Dunce from John Duns Scotus\": \"Dunce\",\n",
" \"The People's Republic of China\": \"China\",\n",
" \"The People\\\\'s Republic of China\": \"China\",\n",
" \"Mary Stuart Mary, Queen of Scots\": \"Mary Stuart\",\n",
" \"Tomás de Torquemada\": \"Torquemada\",\n",
" \"Robert F. Kennedy Attorney General\": \"Robert F. Kennedy\",\n",
" \"Ford\\\\'s Theatre the Ford Theatre accepted\": \"Fords Theatre \",\n",
" \"Alexandre Dumas\": \"Dumas\",\n",
" 'Percy Bysshe Shelley': \"Shelley\",\n",
" \"The Stranger L\\\\'Etranger\": \"The Stranger\",\n",
" \"Rudyard Kipling\": \"Kipling\",\n",
" \"Elizabeth I\": \"Elizabeth\",\n",
" \"Pearl Buck\": \"Buck\",\n",
" \"Theodore Dreiser\": \"Dreiser\",\n",
" \"Geoffrey Chaucer\": \"Chaucer\",\n",
" \"John Updike\": \"Updike\",\n",
" \"Côte d\\\\'Ivoire or the Ivory Coast\": \"Côte dIvoire\",\n",
" \"Henry David Thoreau\": \"Thoreau\",\n",
" \"escaping the Earth\\\\'s gravity and go off into outer space, on your way to the moon, for instance\":\"escaping the Earths gravity\",\n",
" \"Ernst Mach\": \"Mach\",\n",
" \"talons or claws\": \"talons\",\n",
" \"Mall from Pall Mall\": \"Mall\",\n",
" \"Crony from the Greek god Cronus\": \"Crony\",\n",
" \"hurry or quick or fast\": \"hurry\",\n",
" \"Dr. Samuel Mudd\": \"Mudd\",\n",
" \"Anniversary annus - year & verso - turn\": \"Anniversary\",\n",
"}\n",
"\n",
"reversed_replacements = {\n",
" \"3430-c34791\": {\"Hayes\": \"Rutherford B. Hayes\"},\n",
" # \"5098-f4fecb\": {\"Ford\\'s Theatre the Ford Theatre accepted\": \"Fords Theatre \"},\n",
" \"3786-565e32\": {\"Beckett\": \"Samuel Beckett\"},\n",
" \"4956-718970\": {\"nitrous oxide\": \"nitrous oxide\"},\n",
" \"3601-8602cf\": {\"Spiral\": \"Spiral\"},\n",
" \"3444-401076\": {\"Newton\": \"Sir Isaac Newton\"},\n",
" \"2543-59f257\": {\"Newton\": \"Sir Isaac Newton\"},\n",
" \"1906-9014ac\": {\"Kipling\": \"Rudyard Kipling\"},\n",
" \"2635-70b5a3\": {\"Kipling\": \"Rudyard Kipling\"},\n",
" \"3160-7a4598\": {\"Kipling\": \"Rudyard Kipling\"},\n",
" \"2958-433cff\": {\"Kipling\": \"Rudyard Kipling\"},\n",
" \"4627-76ba39\": {\"Kipling\": \"Rudyard Kipling\"},\n",
" \"4968-d9d217\": {\"Buck\": \"Pearl Buck\"},\n",
" \"2866-b796c5\": {\"Cooper\": \"James Fenimore Cooper\"},\n",
" \"2927-c6e128\": {\"Cooper\": \"James Fenimore Cooper\"},\n",
" \"2841-5fbe33\": {\"Updike\": \"John Updike\"},\n",
"\n",
"}\n",
"\n",
"context_replacements = {\n",
" \"Asia's population giant\": \"Asias population giant\",\n",
" \"Spain's Phillip II\": \"Spains Phillip II\",\n",
" \"It's been estimated that this grand inquisitor\": \"Its been estimated that this grand inquisitor\",\n",
" 'He wrote \"The 3 Musketeers\"; his son wrote \"Camille\"': \"He wrote The 3 Musketeers; his son wrote Camille\",\n",
" 'Hmm... he dedicated his 1820 poem \"The Witch of Atlas\" to his wife Mary': \"Hmm... he dedicated his 1820 poem The Witch of Atlas to his wife Mary\",\n",
" 'This 1942 French novel begins, \"Mother died today. Or, maybe, yesterday; I can\\'t be sure.\"': 'This 1942 French novel begins, Mother died today. Or, maybe, yesterday; I cant be sure.',\n",
" \"Mowgli's song Against People appears in this author's Second Jungle Book\": \"Mowglis song Against People appears in this authors Second Jungle Book\",\n",
" 'Wordsworth\\'s poem about her begins, \"Hail, Virgin Queen! O\\'er many an envious bar triumphant\"': \"Wordsworths poem about her begins, Hail, Virgin Queen! Oer many an envious bar triumphant\",\n",
" 'Mercedes of Castile is a lesser-known novel by this author of \"The Leather-Stocking Tales\"': \"Mercedes of Castile is a lesser-known novel by this author of The Leather-Stocking Tales\",\n",
" '\"The Knight\\'s Tale\" by this 14th century author is based on Boccaccio\\'s poem': 'The Knights Tale by this 14th century author is based on Boccaccios poem',\n",
" 'He wrote \"Bech: A Book\", Bech Is Back & Bech At Bay in addition to his Rabbit novels': 'He wrote Bech: A Book, Bech Is Back & Bech At Bay in addition to his Rabbit novels',\n",
" \"Bernard Binlin Dadie's novel Climbie depicts this Ivorian country, his homeland, during colonial times\": \"Bernard Binlin Dadies novel Climbie depicts this Ivorian country, his homeland, during colonial times\",\n",
" 'His Jungle Book prose begins, \"It was seven o\\'clock of a very warm evening in the Seeonee Hills...\"': \"His Jungle Book prose begins, It was seven oclock of a very warm evening in the Seeonee Hills...\"\n",
"}\n",
"\n",
"reversed_context_replacements = {}\n",
"\n",
"old_questions = []\n",
"for q in old_questions_raw:\n",
" nq = deepcopy(q)\n",
" question = q['question'].strip(single_quote)\n",
"\n",
" # Remove hyperlinks from the question, keeping only the text between the tags\n",
" if '<a' in question and '</a>' in question:\n",
" question = re.sub(r'<a[^>]*>(.*?)</a>', r'\\1', question)\n",
"\n",
" # Remove accidental backslashes from the question\n",
" question = question.replace('\\\\', '')\n",
"\n",
" # Remove text within parentheses from the question\n",
" light_edit_question = question = re.sub(r'(\\s*)\\([^)]*\\)\\s*', r'\\1', question).strip()\n",
"\n",
" # Remove text within double quotes from the question\n",
" question = re.sub(r'\"([^\"]+)\"', r'\\1', question).strip()\n",
"\n",
" # Remove aphostrophies from the question\n",
" question = re.sub(r\"(\\w+)'(\\w+)\", r'\\1\\2', question).strip()\n",
"\n",
" nq['context'] = f\"{q['category'].replace(double_quote, '')}: {question}\"\n",
" for k, v in context_replacements.items():\n",
" if k in nq['context']:\n",
" nq['context'] = nq['context'].replace(k, v)\n",
"\n",
" nq[\"ee-question\"] = light_edit_question\n",
" nq[\"ee-continuation\"] = reduce(\n",
" lambda x, y: x.replace(y[0], y[1]),\n",
" replacements.items(),\n",
" light_edit_question\n",
" )\n",
" nq[\"ee-category\"] = q['category']\n",
"\n",
" nq['continuation'] = replacements.get((c := q['answer'].replace('(', '').replace(')', '')), c)\n",
" nq['category'] = \"\".join([ch for ch in q['category'] if ch not in punctuation]).lower().replace(' ', '_')\n",
" nq['id'] = f\"{q['show_number']}-{md5(json.dumps(q).encode('utf-8')).hexdigest()[:6]}\"\n",
" nq['og-category'] = q['category']\n",
" for k, v in reversed_replacements.get(nq['id'], {}).items():\n",
" nq['continuation'] = nq['continuation'].replace(k, v)\n",
" for k, v in reversed_context_replacements.get(nq['id'], {}).items():\n",
" nq['context'] = v\n",
" old_questions.append(nq)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Missing: 0\n",
"Old questions: 2220\n",
"New questions: 2113\n"
]
}
],
"source": [
"new_categories = set([q['category'] for q in new_questions.values()])\n",
"old_questions_subset = {''.join([ch for ch in f\"{q['context']} {q['continuation']}\" if ch not in punctuation and ch not in whitespace]):q for q in [q for q in old_questions if q['category'] in new_categories]}\n",
"\n",
"missing = [{**v, 'key': k} for k, v in new_questions.items() if k not in old_questions_subset]\n",
"print(f\"Missing: {len(missing)}\")\n",
"print(f\"Old questions: {len(old_questions_subset)}\")\n",
"print(f\"New questions: {len(new_questions)}\")\n",
"for m in missing:\n",
" print(m['context'])\n",
" print(m['continuation'])\n",
" break\n",
"\n",
"# print('-----')\n",
"# query = ' '.join(m['context'].split(':', 1)[1].split(' ')[5:9]).strip()\n",
"# for row in [o for o in old_questions if query in o['question']]:\n",
"# print(row['id'])\n",
"# print(f\"{row['context']} {'SAME' if row['context'] == m['context'] else 'DIFFERENT'}\")\n",
"# print(f\"{row['continuation']} {'SAME' if row['continuation'] == m['continuation'] else 'DIFFERENT'}\")\n",
"# print()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mosaicml Gauntlet: 2116\n",
"All questions: 216930\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b0db52ff66c84083984ce81539bb3129",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Uploading the dataset shards: 0%| | 0/1 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e07846cf593e47e78cc7c62d179c5baa",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Creating parquet from Arrow format: 0%| | 0/3 [00:00<?, ?ba/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "af4036c1ed13441c9a6cc1de5558dfa3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Uploading the dataset shards: 0%| | 0/1 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e23eca2e0aac433baba52069b40c2e3a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Creating parquet from Arrow format: 0%| | 0/217 [00:00<?, ?ba/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7a6982d43029428c98258352ec90bf28",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"README.md: 0%| | 0.00/2.34k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"CommitInfo(commit_url='https://huggingface.co/datasets/soldni/jeopardy/commit/95da23525ad41487becfaf821be025ccefda6a34', commit_message='Upload dataset', commit_description='', oid='95da23525ad41487becfaf821be025ccefda6a34', pr_url=None, pr_revision=None, pr_num=None)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_questions = []\n",
"mosaicml_gauntlet = []\n",
"for row in old_questions:\n",
" key = ''.join([ch for ch in f\"{row['context']} {row['continuation']}\" if ch not in punctuation and ch not in whitespace])\n",
" all_questions.append(row)\n",
" row['mosaicml_gauntlet'] = key in new_questions\n",
" if row['mosaicml_gauntlet']:\n",
" mosaicml_gauntlet.append(row)\n",
"\n",
"print(f\"Mosaicml Gauntlet: {len(mosaicml_gauntlet)}\")\n",
"print(f\"All questions: {len(all_questions)}\")\n",
"\n",
"mosaicml_gauntlet_dataset = datasets.Dataset.from_list(mosaicml_gauntlet)\n",
"all_questions_dataset = datasets.Dataset.from_list(all_questions)\n",
"\n",
"mosaicml_gauntlet_dataset.push_to_hub(\n",
" \"soldni/jeopardy\",\n",
" config_name=\"mosaicml_gauntlet\",\n",
" data_dir=\"data/mosaicml_gauntlet\"\n",
")\n",
"all_questions_dataset.push_to_hub(\n",
" \"soldni/jeopardy\",\n",
" config_name=\"all_questions\",\n",
" data_dir=\"data/all_questions\"\n",
")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "dolma",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|