{
"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 '' in question:\n",
" question = re.sub(r']*>(.*?)', 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
}