├── Data_Generator.ipynb ├── Definition_Extraction.ipynb ├── GPT2-DefinitionModel.ipynb ├── Glossary_Extraction.ipynb ├── LICENSE ├── README.md └── glossary.jpeg /Data_Generator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 56, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from bs4 import BeautifulSoup\n", 10 | "import requests\n", 11 | "import pandas as pd\n", 12 | "import glob\n", 13 | "import string\n", 14 | "import os\n", 15 | "import codecs" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "## Project Gutenberg" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 37, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "BASE_BOOK_URL = 'https://www.gutenberg.org/browse/scores/top'\n", 32 | "\n", 33 | "html = requests.get(BASE_BOOK_URL).text\n", 34 | "soup = BeautifulSoup(html)\n", 35 | "\n", 36 | "unq_code = {}\n", 37 | "for s in soup.findAll('li'):\n", 38 | " url = s.a['href']\n", 39 | " if 'ebooks' in url:\n", 40 | " unq_code[int(url.split('/')[-1])] = s.a.text" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 38, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "BOOK_TXT_BASE = 'https://www.gutenberg.org/files/'\n", 50 | "book_urls = []\n", 51 | "for code in unq_code:\n", 52 | " book_urls.append(os.path.join(BOOK_TXT_BASE, f'{code}/{code}-0.txt'))" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 40, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "for b in book_urls:\n", 62 | " name = b.split('/')[-2]\n", 63 | " html = requests.get(b).text\n", 64 | " with codecs.open(f'data/books/{name}.txt', 'w', 'utf-8') as infile:\n", 65 | " infile.write(html)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "## GradeSaver Glossary" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 67, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Skipped: the-awakening-and-selected-short-stories\n", 85 | "Done: the-adventures-of-sherlock-holmes\n", 86 | "Skipped: songs-of-innocence-and-songs-of-experience\n", 87 | "Skipped: ang-filibusterismo-karugtóng-ng-noli-me-tangere\n", 88 | "Skipped: the-£1000000-banknote-and-other-new-stories\n", 89 | "Skipped: ulysses\n", 90 | "Skipped: the-call-of-the-wild\n", 91 | "Done: pygmalion\n", 92 | "Done: the-souls-of-black-folk\n", 93 | "Skipped: also-sprach-zarathustra-english\n", 94 | "Skipped: moby-dick-or-the-whale\n", 95 | "Skipped: ukridge\n", 96 | "Skipped: the-life-and-adventures-of-robinson-crusoe\n", 97 | "Done: the-odyssey\n", 98 | "invalid literal for int() with base 10: 'HP7'\n", 99 | "Skipped: frankenstein-or-the-modern-prometheus\n", 100 | "Skipped: the-interesting-narrative-of-the-life-of-olaudah-equiano-or-gustavus-vassa-the-african\n", 101 | "Skipped: anna-karenina\n", 102 | "Skipped: essays-of-michel-de-montaigne-—-complete\n", 103 | "Skipped: metamorphosis\n", 104 | "Skipped: great-expectations\n", 105 | "Done: the-scarlet-letter\n", 106 | "Done: little-women\n", 107 | "Skipped: the-prophet\n", 108 | "Skipped: a-tale-of-two-cities\n", 109 | "Done: common-sense\n", 110 | "Skipped: treasure-island\n", 111 | "Skipped: dubliners\n", 112 | "Skipped: the-modern-traveller\n", 113 | "Skipped: ethan-frome\n", 114 | "Done: pride-and-prejudice\n", 115 | "Skipped: outland\n", 116 | "Skipped: a-dictionary-of-islam\n", 117 | "Skipped: uncle-tom-cabin\n", 118 | "Skipped: the-turn-of-the-screw\n", 119 | "Done: david-copperfield\n", 120 | "Skipped: walden-and-on-the-duty-of-civil-disobedience\n", 121 | "Skipped: an-occurrence-at-owl-creek-bridge\n", 122 | "Skipped: sense-and-sensibility\n", 123 | "Done: emma\n", 124 | "Skipped: the-green-world\n", 125 | "invalid literal for int() with base 10: 'HP4'\n", 126 | "Skipped: how-the-other-half-lives-studies-among-the-tenements-of-new-york\n", 127 | "Skipped: the-roman-index-of-forbidden-books\n", 128 | "Skipped: ion\n", 129 | "Skipped: the-iliad\n", 130 | "Done: oliver-twist\n", 131 | "Skipped: incidents-in-the-life-of-a-slave-girl-written\n", 132 | "Skipped: prestuplenie-i-nakazanie-english\n", 133 | "Skipped: il-principe-english\n", 134 | "Skipped: a-modest-proposal\n", 135 | "Skipped: the-adventures-of-tom-sawyer\n", 136 | "Skipped: index-of-the-project-gutenberg-works-of-h-g-wells\n", 137 | "Skipped: siddhartha\n", 138 | "Skipped: the-kama-sutra-of-vatsyayana\n", 139 | "Skipped: et-dukkehjem-english\n", 140 | "Skipped: frankenstein-or-the-modern-prometheus\n", 141 | "Skipped: the-strange-case-of-dr-jekyll-and-mr-hyde\n", 142 | "Done: anthem\n", 143 | "Done: beyond-good-and-evil\n", 144 | "Done: wuthering-heights\n", 145 | "Skipped: les-misérables\n", 146 | "Done: the-picture-of-dorian-gray\n", 147 | "Skipped: narrative-of-the-life-of-frederick-douglass-an-american-slave\n", 148 | "Skipped: index-of-project-gutenberg-works-on-black-history\n", 149 | "Done: the-seagull\n", 150 | "Skipped: the-slang-dictionary-etymological-historical-and-andecdotal\n", 151 | "Done: the-hound-of-the-baskervilles\n", 152 | "Done: anne-of-green-gables\n", 153 | "Skipped: gulliver-travels-into-several-remote-nations-of-the-world\n", 154 | "Skipped: beowulf-an-anglosaxon-epic-poem-17198\n", 155 | "Skipped: the-problems-of-philosophy\n", 156 | "Done: meditations\n", 157 | "Skipped: del-sentimiento-trágico-de-la-vida-english\n", 158 | "Skipped: the-history-of-modern-painting-volume-3-of-4\n", 159 | "Skipped: ballads-of-a-bohemian\n", 160 | "Skipped: the-confessions-of-st-augustine\n", 161 | "invalid literal for int() with base 10: 'HP1'\n", 162 | "Skipped: through-the-lookingglass\n", 163 | "Skipped: why-colored-people-in-philadelphia-are-excluded-from-the-street-cars\n", 164 | "Done: leviathan\n", 165 | "Done: the-wonderful-wizard-of-oz\n", 166 | "Done: heart-of-darkness\n", 167 | "Skipped: the-republic\n", 168 | "Done: grimms-fairy-tales\n", 169 | "invalid literal for int() with base 10: 'HP6'\n", 170 | "Skipped: dracula\n", 171 | "Skipped: middlemarch\n", 172 | "Skipped: don-juan\n", 173 | "Skipped: alice-adventures-in-wonderland\n", 174 | "Done: a-study-in-scarlet\n", 175 | "Skipped: don-quixote\n", 176 | "Skipped: the-count-of-monte-cristo-illustrated\n", 177 | "Done: the-legend-of-sleepy-hollow\n", 178 | "Done: peter-pan\n", 179 | "Skipped: the-devil-dictionary\n", 180 | "Done: noli-me-tangere\n", 181 | "Done: the-secret-garden\n", 182 | "Skipped: the-complete-works-of-william-shakespeare\n", 183 | "invalid literal for int() with base 10: 'HP2'\n", 184 | "Done: the-brothers-karamazov\n", 185 | "Skipped: autobiography-of-benjamin-franklin\n", 186 | "Skipped: the-memoirs-correspondence-and-miscellanies-from-the-papers-of-thomas-jefferson\n", 187 | "Skipped: emergency-childbirth\n", 188 | "Skipped: tractatus-logicophilosophicus\n", 189 | "Skipped: a-pickle-for-the-knowing-ones\n", 190 | "Skipped: the-works-of-edgar-allan-poe-the-raven-edition\n", 191 | "Skipped: a-christmas-carol-in-prose-being-a-ghost-story-of-christmas\n", 192 | "Done: second-treatise-of-government\n", 193 | "Skipped: the-importance-of-being-earnest-a-trivial-comedy-for-serious-people\n", 194 | "Done: the-yellow-wallpaper\n", 195 | "Skipped: la-colline-inspirée\n", 196 | "Skipped: divine-comedy-longfellow-translation-hell\n", 197 | "Skipped: the-moth-decides-a-novel\n", 198 | "Skipped: an-index-of-the-divine-comedy\n", 199 | "Skipped: hard-times\n", 200 | "invalid literal for int() with base 10: 'HP3'\n", 201 | "invalid literal for int() with base 10: 'HP5'\n", 202 | "Done: the-war-of-the-worlds\n", 203 | "Skipped: a-history-of-the-philippines\n", 204 | "Skipped: jane-eyre-an-autobiography\n", 205 | "Done: a-journal-of-the-plague-year\n", 206 | "Skipped: the-time-machine\n", 207 | "Done: the-jungle\n", 208 | "Skipped: candide\n", 209 | "Done: war-and-peace\n", 210 | "Skipped: adventures-of-huckleberry-finn\n", 211 | "Skipped: narrative-of-the-captivity-and-restoration-of-mrs-mary-rowlandson\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "BASE_GLOSS_URL = 'https://www.gradesaver.com/'\n", 217 | "TERMINAL = '/study-guide/glossary-of-terms'\n", 218 | "\n", 219 | "def punctuations(data_str):\n", 220 | " data_str = data_str.replace(\"'s\", \"\")\n", 221 | " for x in data_str.lower():\n", 222 | " if x in string.punctuation: \n", 223 | " data_str = data_str.replace(x, \"\")\n", 224 | " return data_str\n", 225 | "\n", 226 | "for book in glob.glob('data/books/*'):\n", 227 | " code = book.split('/')[-1].split('.')[0]\n", 228 | " try:\n", 229 | " bookname = unq_code[int(code)]\n", 230 | " bookname = bookname.split(' by ')[0].lower()\n", 231 | " bookname = punctuations(bookname)\n", 232 | " bookname = bookname.replace(\" \", \"-\")\n", 233 | " html = requests.get(BASE_GLOSS_URL+bookname+TERMINAL).text\n", 234 | " soup = BeautifulSoup(html)\n", 235 | " tt = []\n", 236 | " for term in soup.findAll(\"section\", {\"class\": \"linkTarget\"}):\n", 237 | " tt.append([term.h2.text.lower().strip(), term.p.text.lower().strip()])\n", 238 | " if len(tt):\n", 239 | " print (f'Done: {bookname}')\n", 240 | " data = pd.DataFrame(tt, columns=['word', 'def'])\n", 241 | " data.to_csv(f'data/ground_truth/{code}.csv', sep='\\t', encoding='utf-8', index=False)\n", 242 | " else:\n", 243 | " print (f'Skipped: {bookname}')\n", 244 | " except Exception as e: print (e)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": {}, 251 | "outputs": [], 252 | "source": [] 253 | } 254 | ], 255 | "metadata": { 256 | "kernelspec": { 257 | "display_name": "Python 3", 258 | "language": "python", 259 | "name": "python3" 260 | }, 261 | "language_info": { 262 | "codemirror_mode": { 263 | "name": "ipython", 264 | "version": 3 265 | }, 266 | "file_extension": ".py", 267 | "mimetype": "text/x-python", 268 | "name": "python", 269 | "nbconvert_exporter": "python", 270 | "pygments_lexer": "ipython3", 271 | "version": "3.6.9" 272 | } 273 | }, 274 | "nbformat": 4, 275 | "nbformat_minor": 4 276 | } 277 | -------------------------------------------------------------------------------- /GPT2-DefinitionModel.ipynb: -------------------------------------------------------------------------------- 1 | {"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n for filename in filenames:\n print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":1,"outputs":[{"output_type":"stream","text":"/kaggle/input/urban-dictionary-words-dataset/urbandict-word-defs.csv\n","name":"stdout"}]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/urban-dictionary-words-dataset/urbandict-word-defs.csv', nrows=100000, error_bad_lines=False)","execution_count":2,"outputs":[{"output_type":"stream","text":"b'Skipping line 3692: expected 6 fields, saw 7\\nSkipping line 5546: expected 6 fields, saw 7\\nSkipping line 7198: expected 6 fields, saw 7\\nSkipping line 9758: expected 6 fields, saw 7\\nSkipping line 13350: expected 6 fields, saw 7\\nSkipping line 20000: expected 6 fields, saw 7\\nSkipping line 20088: expected 6 fields, saw 7\\nSkipping line 21776: expected 6 fields, saw 8\\nSkipping line 23826: expected 6 fields, saw 8\\nSkipping line 25255: expected 6 fields, saw 7\\nSkipping line 25643: expected 6 fields, saw 7\\nSkipping line 25777: expected 6 fields, saw 7\\nSkipping line 30965: expected 6 fields, saw 7\\nSkipping line 35485: expected 6 fields, saw 7\\nSkipping line 36022: expected 6 fields, saw 8\\nSkipping line 36072: expected 6 fields, saw 7\\nSkipping line 40152: expected 6 fields, saw 7\\nSkipping line 40695: expected 6 fields, saw 7\\nSkipping line 41942: expected 6 fields, saw 7\\nSkipping line 43660: expected 6 fields, saw 7\\nSkipping line 46529: expected 6 fields, saw 7\\nSkipping line 48482: expected 6 fields, saw 7\\nSkipping line 49277: expected 6 fields, saw 7\\nSkipping line 49718: expected 6 fields, saw 9\\nSkipping line 50662: expected 6 fields, saw 7\\nSkipping line 50899: expected 6 fields, saw 7\\nSkipping line 53871: expected 6 fields, saw 8\\nSkipping line 54199: expected 6 fields, saw 8\\nSkipping line 54595: expected 6 fields, saw 8\\nSkipping line 56867: expected 6 fields, saw 7\\nSkipping line 57140: expected 6 fields, saw 7\\nSkipping line 60471: expected 6 fields, saw 7\\nSkipping line 65130: expected 6 fields, saw 7\\nSkipping line 65934: expected 6 fields, saw 9\\nSkipping line 68114: expected 6 fields, saw 7\\nSkipping line 68537: expected 6 fields, saw 7\\nSkipping line 74771: expected 6 fields, saw 8\\nSkipping line 88345: expected 6 fields, saw 11\\nSkipping line 89570: expected 6 fields, saw 7\\nSkipping line 89989: expected 6 fields, saw 7\\nSkipping line 92650: expected 6 fields, saw 7\\nSkipping line 94928: expected 6 fields, saw 8\\nSkipping line 95130: expected 6 fields, saw 7\\nSkipping line 97923: expected 6 fields, saw 7\\n'\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train.columns\nnew_train = train[['word', 'definition']]","execution_count":3,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train['word'] = new_train.word.str.lower()\nnew_train['definition'] = new_train.definition.str.lower()","execution_count":4,"outputs":[{"output_type":"stream","text":"/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \nA value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_indexer,col_indexer] = value instead\n\nSee the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n \"\"\"Entry point for launching an IPython kernel.\n/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: \nA value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_indexer,col_indexer] = value instead\n\nSee the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n \n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# new_train.to_csv('data.csv', index=False, sep='~')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nfrom transformers import GPT2Tokenizer, GPT2LMHeadModel\nimport numpy as np\n\nimport os\nfrom tqdm import tqdm\n\nimport logging\nlogging.getLogger().setLevel(logging.CRITICAL)\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\ndevice = 'cpu'\nif torch.cuda.is_available():\n device = 'cuda'","execution_count":5,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#new_train.head()\nnew_train.shape\nnew_train.columns","execution_count":11,"outputs":[{"output_type":"execute_result","execution_count":11,"data":{"text/plain":"Index(['word', 'definition'], dtype='object')"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium')\nmodel = GPT2LMHeadModel.from_pretrained('gpt2-medium')","execution_count":12,"outputs":[{"output_type":"display_data","data":{"text/plain":"HBox(children=(FloatProgress(value=0.0, description='Downloading', max=1042301.0, style=ProgressStyle(descript…","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"86aee3b216c64b22a846b0c95ef1cae5"}},"metadata":{}},{"output_type":"stream","text":"\n","name":"stdout"},{"output_type":"display_data","data":{"text/plain":"HBox(children=(FloatProgress(value=0.0, description='Downloading', max=456318.0, style=ProgressStyle(descripti…","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"45238eec42214045a1da2fbe6b90ed53"}},"metadata":{}},{"output_type":"stream","text":"\n","name":"stdout"},{"output_type":"display_data","data":{"text/plain":"HBox(children=(FloatProgress(value=0.0, description='Downloading', max=718.0, style=ProgressStyle(description_…","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"7efa1e0635cc4e699c2324d30cc9ffae"}},"metadata":{}},{"output_type":"stream","text":"\n","name":"stdout"},{"output_type":"display_data","data":{"text/plain":"HBox(children=(FloatProgress(value=0.0, description='Downloading', max=1520013706.0, style=ProgressStyle(descr…","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"d49f6f0ded934ee0bcf11be24b55ce30"}},"metadata":{}},{"output_type":"stream","text":"\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"from torch.utils.data import Dataset\nfrom torch.utils.data import Dataset, DataLoader\nimport os\nimport json\nimport csv\n\nclass GlossaryDataset(Dataset):\n def __init__(self, dataframe):\n super().__init__()\n\n\n self.data_list = []\n self.end_of_text_token = \"<|endoftext|>\"\n self.start_of_text_token = \"<|startoftext|>\"\n \n for i in range(dataframe.shape[0]):\n data_str = f\"{self.start_of_text_token} {new_train.iloc[i]['word']} [DEFINE] {new_train.iloc[i]['definition']} {self.end_of_text_token}\"\n self.data_list.append(data_str)\n \n def __len__(self):\n return len(self.data_list)\n\n def __getitem__(self, item):\n return self.data_list[item]","execution_count":21,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = GlossaryDataset(dataframe=new_train)\ndata_loader = DataLoader(dataset, batch_size=1, shuffle=True)","execution_count":22,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# max len\nmax([len(i[0].split()) for i in data_loader])\n","execution_count":28,"outputs":[{"output_type":"execute_result","execution_count":28,"data":{"text/plain":"5"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 64\nEPOCHS = 10\nLEARNING_RATE = 2e-5\nWARMUP_STEPS = 1000\nMAX_SEQ_LEN = 500\nseq_cnt = 0\nbatch_count = 0\nsum_loss = 0.0\n\nfrom transformers import AdamW, get_linear_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup\n\ndevice = 'cpu'\nif torch.cuda.is_available():\n device = 'cuda'\n\nmodel = model.to(device)\nmodel.train()\noptimizer = AdamW(model.parameters(), lr=LEARNING_RATE)\nscheduler = get_cosine_with_hard_restarts_schedule_with_warmup(optimizer, num_warmup_steps=WARMUP_STEPS, num_training_steps = -1)","execution_count":29,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for epoch in range(EPOCHS):\n print (f'Running {epoch} epoch')\n\n for idx,joke in enumerate(data_loader):\n joke_ = torch.tensor(tokenizer.encode(joke[0]))\n joke_=joke_.unsqueeze(0).to(device)\n outputs = model(joke_, labels=joke_)\n loss, logits = outputs[:2]\n loss.backward()\n sum_loss += loss.data\n\n seq_cnt += 1\n if seq_cnt == BATCH_SIZE:\n seq_cnt = 0 \n batch_count += 1\n optimizer.step()\n scheduler.step() \n optimizer.zero_grad()\n model.zero_grad()\n\n if batch_count == 100:\n print(f\"sum loss {sum_loss}\")\n batch_count = 0\n sum_loss = 0.0\n","execution_count":30,"outputs":[{"output_type":"stream","text":"Running 0 epoch\nsum loss 30025.76953125\nsum loss 19658.46875\nsum loss 18658.2421875\nsum loss 18422.333984375\nsum loss 18021.263671875\nsum loss 17886.521484375\nsum loss 17843.765625\nsum loss 17621.337890625\nsum loss 17567.53515625\nsum loss 17471.611328125\nsum loss 17448.0234375\nsum loss 17558.81640625\nsum loss 17404.85546875\nsum loss 17499.3203125\nsum loss 17449.3984375\nRunning 1 epoch\nsum loss 17445.13671875\nsum loss 17323.49609375\nsum loss 17311.849609375\nsum loss 17224.064453125\nsum loss 17368.869140625\nsum loss 17327.068359375\nsum loss 17295.970703125\nsum loss 17419.18359375\n","name":"stdout"},{"output_type":"error","ename":"KeyboardInterrupt","evalue":"","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mjoke_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtokenizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjoke\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mjoke_\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mjoke_\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munsqueeze\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjoke_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mjoke_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogits\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 530\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 531\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 532\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 533\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 534\u001b[0m \u001b[0mhook_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/transformers/modeling_gpt2.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, past, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, labels)\u001b[0m\n\u001b[1;32m 597\u001b[0m \u001b[0mposition_ids\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mposition_ids\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 598\u001b[0m \u001b[0mhead_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhead_mask\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 599\u001b[0;31m \u001b[0minputs_embeds\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minputs_embeds\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 600\u001b[0m )\n\u001b[1;32m 601\u001b[0m \u001b[0mhidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtransformer_outputs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 530\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 531\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 532\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 533\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 534\u001b[0m \u001b[0mhook_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/transformers/modeling_gpt2.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, past, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds)\u001b[0m\n\u001b[1;32m 482\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 483\u001b[0m outputs = block(\n\u001b[0;32m--> 484\u001b[0;31m \u001b[0mhidden_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlayer_past\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlayer_past\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mattention_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mattention_mask\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhead_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhead_mask\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 485\u001b[0m )\n\u001b[1;32m 486\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 530\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 531\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 532\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 533\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 534\u001b[0m \u001b[0mhook_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/transformers/modeling_gpt2.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, x, layer_past, attention_mask, head_mask)\u001b[0m\n\u001b[1;32m 224\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlayer_past\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mattention_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhead_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 225\u001b[0m output_attn = self.attn(\n\u001b[0;32m--> 226\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mln_1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlayer_past\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlayer_past\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mattention_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mattention_mask\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhead_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhead_mask\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 227\u001b[0m )\n\u001b[1;32m 228\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0moutput_attn\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# output_attn: a, present, (attentions)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 530\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 531\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 532\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 533\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 534\u001b[0m \u001b[0mhook_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/transformers/modeling_gpt2.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, x, layer_past, attention_mask, head_mask)\u001b[0m\n\u001b[1;32m 177\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlayer_past\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mattention_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhead_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 178\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mc_attn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 179\u001b[0;31m \u001b[0mquery\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit_size\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdim\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 180\u001b[0m \u001b[0mquery\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit_heads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mquery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 181\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit_heads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.7/site-packages/torch/tensor.py\u001b[0m in \u001b[0;36msplit\u001b[0;34m(self, split_size, dim)\u001b[0m\n\u001b[1;32m 373\u001b[0m \"\"\"\n\u001b[1;32m 374\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msplit_size\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 375\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msplit_size\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 376\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 377\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit_with_sizes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msplit_size\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mKeyboardInterrupt\u001b[0m: "]}]},{"metadata":{"trusted":true},"cell_type":"code","source":"def choose_from_top_k_top_n(probs, k=2, p=0.8):\n ind = np.argpartition(probs, -k)[-k:]\n top_prob = probs[ind]\n top_prob = {i: top_prob[idx] for idx,i in enumerate(ind)}\n sorted_top_prob = {k: v for k, v in sorted(top_prob.items(), key=lambda item: item[1], reverse=True)}\n \n t=0\n f=[]\n pr = []\n for k,v in sorted_top_prob.items():\n t+=v\n f.append(k)\n pr.append(v)\n if t>=p:\n break\n top_prob = pr / np.sum(pr)\n token_id = np.random.choice(f, 1, p = top_prob)\n\n return int(token_id)","execution_count":139,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.eval()\n# model.train()\ndef predict(sentence): \n joke_num = 0\n with torch.no_grad():\n for joke_idx in range(1):\n joke_finished = False\n cur_ids = torch.tensor(tokenizer.encode(sentence)).unsqueeze(0).to(device)\n for i in range(500):\n outputs = model(cur_ids, labels=cur_ids)\n loss, logits = outputs[:2]\n\n softmax_logits = torch.softmax(logits[0,-1], dim=0) #Take the first(from only one in this case) batch and the last predicted embedding\n if i < 5:\n n = 10\n else:\n n = 5\n next_token_id = choose_from_top_k_top_n(softmax_logits.to('cpu').numpy()) #top-k-top-n sampling\n cur_ids = torch.cat([cur_ids, torch.ones((1,1)).long().to(device) * next_token_id], dim = 1) # Add the last word to the running sequence\n\n if next_token_id in tokenizer.encode('<|endoftext|>'):\n joke_finished = True\n break\n\n if joke_finished:\n \n joke_num = joke_num + 1\n \n output_list = list(cur_ids.squeeze().to('cpu').numpy())\n output_text = tokenizer.decode(output_list)\n return output_text\n else:\n output_list = list(cur_ids.squeeze().to('cpu').numpy())\n output_text = tokenizer.decode(output_list)\n return output_text","execution_count":140,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict('<|startoftoken|> guh [DEFINE]')","execution_count":195,"outputs":[{"output_type":"execute_result","execution_count":195,"data":{"text/plain":"\"<|startoftoken|> guh [DEFINE] the word 'guh' means 'good' or 'great'. ;; also, 'guh' is a very common greeting in the south of the united states<|endoftext|>\""},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted = '<|startoftoken|> rhitard [DEFINE] the most annoying person on earth.<|endoftext|>'\nactual = 'idiot' or 'retard'\n\npredicted = '<|startoftoken|> dsl [DEFINE] a dsl is a computer that is used to connect to the internet. ;; a dsl is also used to connect to a network.<|endoftext|>'\nactual = 'high speed connection to the internet '\n\npredicted = '<|startoftoken|> janky [DEFINE] a person who is janky<|endoftext|>'\nactual = 'undesirable'\n\npredicted = '<|startoftoken|> scrilla [DEFINE] a small, white, round, and round shaped object.<|endoftext|>'\nactual = 'cash money'\n\npredicted = '\"<|startoftoken|> guh [DEFINE] the word 'guh' means 'good' or 'great'. ;; also, 'guh' is a very common greeting in the south of the united states<|endoftext|>\"'\nactual = \"to be used in replacement of 'uhhh'\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train.head(90)","execution_count":165,"outputs":[{"output_type":"execute_result","execution_count":165,"data":{"text/plain":" word definition\n0 janky undesirable; less-than optimum.\n1 slumpin' low down and funky, but [knee deep] enough to ...\n2 yayeeyay affirmation; suggestion of encouragement, appr...\n3 hard-core anything out of our league that can be good or...\n4 brutal anything that makes you sweat\n.. ... ...\n85 real men 1. guys with chest hair ;; 2. guys who thig th...\n86 watchers 1. a group of individuals who monitor the comm...\n87 dsl 1. high speed connection to the internet ;; 2....\n88 warn 1.an action that is taken on the internet to p...\n89 check 1.to put someone in their place ;; 2.to realiz...\n\n[90 rows x 2 columns]","text/html":"
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
worddefinition
0jankyundesirable; less-than optimum.
1slumpin'low down and funky, but [knee deep] enough to ...
2yayeeyayaffirmation; suggestion of encouragement, appr...
3hard-coreanything out of our league that can be good or...
4brutalanything that makes you sweat
.........
85real men1. guys with chest hair ;; 2. guys who thig th...
86watchers1. a group of individuals who monitor the comm...
87dsl1. high speed connection to the internet ;; 2....
88warn1.an action that is taken on the internet to p...
89check1.to put someone in their place ;; 2.to realiz...
\n

90 rows × 2 columns

\n
"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train.iloc[111]['word']","execution_count":191,"outputs":[{"output_type":"execute_result","execution_count":191,"data":{"text/plain":"'guh'"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train.iloc[111]['definition']","execution_count":192,"outputs":[{"output_type":"execute_result","execution_count":192,"data":{"text/plain":"\"to be used in replacement of 'uhhh'\""},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4} -------------------------------------------------------------------------------- /Glossary_Extraction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "ename": "ImportError", 10 | "evalue": "No module named spacy", 11 | "output_type": "error", 12 | "traceback": [ 13 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 14 | "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", 15 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0mguten_words\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcollections\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCounter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mlemmatizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlemmatize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mwords_g\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 28\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mspacy\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 29\u001b[0m \u001b[0mnlp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mspacy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"en_core_web_sm\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0mstop\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstopwords\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'english'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 16 | "\u001b[0;31mImportError\u001b[0m: No module named spacy" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "# importing libraries\n", 22 | "import os\n", 23 | "import re\n", 24 | "import codecs\n", 25 | "import matplotlib.pyplot as plt\n", 26 | "import collections\n", 27 | "from string import punctuation\n", 28 | "import nltk\n", 29 | "from nltk.corpus import stopwords\n", 30 | "import pandas as pd\n", 31 | "from nltk.stem import WordNetLemmatizer \n", 32 | "lemmatizer = WordNetLemmatizer() \n", 33 | "from nltk.corpus import brown\n", 34 | "from nltk.corpus import reuters\n", 35 | "from nltk.corpus import webtext\n", 36 | "from nltk.corpus import gutenberg\n", 37 | "\n", 38 | "words_r = reuters.words()\n", 39 | "words_b = brown.words()\n", 40 | "words_w = webtext.words()\n", 41 | "words_g = gutenberg.words()\n", 42 | "\n", 43 | "brown_words = dict(collections.Counter([lemmatizer.lemmatize(i.lower()) for i in words_b]))\n", 44 | "reuters_words = dict(collections.Counter([lemmatizer.lemmatize(i.lower()) for i in words_r]))\n", 45 | "web_words = dict(collections.Counter([lemmatizer.lemmatize(i.lower()) for i in words_w]))\n", 46 | "guten_words = dict(collections.Counter([lemmatizer.lemmatize(i.lower()) for i in words_g]))\n", 47 | "\n", 48 | "import spacy\n", 49 | "nlp = spacy.load(\"en_core_web_sm\")\n", 50 | "stop = set(stopwords.words('english'))\n", 51 | "\n", 52 | "%matplotlib inline" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "stop_brown = [i[0] for i in sorted(brown_words.items(), key=lambda k: k[1], reverse=True) if i[1] > 100]\n", 62 | "stop_reuters = [i[0] for i in sorted(reuters_words.items(), key=lambda k: k[1], reverse=True) if i[1] > 250]\n", 63 | "stop_web = [i[0] for i in sorted(web_words.items(), key=lambda k: k[1], reverse=True) if i[1] > 50]\n", 64 | "stop_guten = [i[0] for i in sorted(guten_words.items(), key=lambda k: k[1], reverse=True) if i[1] > 100]\n", 65 | "\n", 66 | "len(stop_brown), len(stop_reuters), len(stop_web), len(stop_guten)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "BASE_DIR = 'data'\n", 76 | "DATA_DIR = codecs.open(os.path.join(BASE_DIR, 'books/HP1.txt'), 'rb', encoding='utf-8').readlines()\n", 77 | "COMMON_DIR = codecs.open(os.path.join(BASE_DIR, 'google_10000.txt'), 'rb', encoding='utf-8').readlines()\n", 78 | "true_data = pd.read_csv(os.path.join(BASE_DIR, 'ground_truth/HP1.csv'), sep='\\t')\n", 79 | "stop_words = codecs.open(os.path.join(BASE_DIR, 'stop.txt'), 'rb', encoding='utf-8').readlines()" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "stop.update(list(set([i.strip().lower() for i in stop_words])))\n", 89 | "stop.update(list(set([i.strip().lower() for i in stop_brown])))\n", 90 | "stop.update(list(set([i.strip().lower() for i in stop_web])))\n", 91 | "stop.update(list(set([i.strip().lower() for i in stop_reuters])))\n", 92 | "stop.update(list(set([i.strip().lower() for i in stop_guten])))" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "len(set(stop))" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "true_data_lst = true_data.iloc[:, 0].tolist()" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "len(true_data_lst)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 15, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "contraction_mapping = {\"ain't\": \"is not\", \"aren't\": \"are not\",\"can't\": \"cannot\", \"'cause\": \"because\", \"could've\": \"could have\", \"couldn't\": \"could not\", \"didn't\": \"did not\", \"doesn't\": \"does not\", \"don't\": \"do not\", \"hadn't\": \"had not\", \"hasn't\": \"has not\", \"haven't\": \"have not\", \"he'd\": \"he would\",\"he'll\": \"he will\", \"he's\": \"he is\", \"how'd\": \"how did\", \"how'd'y\": \"how do you\", \"how'll\": \"how will\", \"how's\": \"how is\", \"I'd\": \"I would\", \"I'd've\": \"I would have\", \"I'll\": \"I will\", \"I'll've\": \"I will have\",\"I'm\": \"I am\", \"I've\": \"I have\", \"i'd\": \"i would\", \"i'd've\": \"i would have\", \"i'll\": \"i will\", \"i'll've\": \"i will have\",\"i'm\": \"i am\", \"i've\": \"i have\", \"isn't\": \"is not\", \"it'd\": \"it would\", \"it'd've\": \"it would have\", \"it'll\": \"it will\", \"it'll've\": \"it will have\",\"it's\": \"it is\", \"let's\": \"let us\", \"ma'am\": \"madam\", \"mayn't\": \"may not\", \"might've\": \"might have\",\"mightn't\": \"might not\",\"mightn't've\": \"might not have\", \"must've\": \"must have\", \"mustn't\": \"must not\", \"mustn't've\": \"must not have\", \"needn't\": \"need not\", \"needn't've\": \"need not have\",\"o'clock\": \"of the clock\", \"oughtn't\": \"ought not\", \"oughtn't've\": \"ought not have\", \"shan't\": \"shall not\", \"sha'n't\": \"shall not\", \"shan't've\": \"shall not have\", \"she'd\": \"she would\", \"she'd've\": \"she would have\", \"she'll\": \"she will\", \"she'll've\": \"she will have\", \"she's\": \"she is\", \"should've\": \"should have\", \"shouldn't\": \"should not\", \"shouldn't've\": \"should not have\", \"so've\": \"so have\",\"so's\": \"so as\", \"this's\": \"this is\",\"that'd\": \"that would\", \"that'd've\": \"that would have\", \"that's\": \"that is\", \"there'd\": \"there would\", \"there'd've\": \"there would have\", \"there's\": \"there is\", \"here's\": \"here is\",\"they'd\": \"they would\", \"they'd've\": \"they would have\", \"they'll\": \"they will\", \"they'll've\": \"they will have\", \"they're\": \"they are\", \"they've\": \"they have\", \"to've\": \"to have\", \"wasn't\": \"was not\", \"we'd\": \"we would\", \"we'd've\": \"we would have\", \"we'll\": \"we will\", \"we'll've\": \"we will have\", \"we're\": \"we are\", \"we've\": \"we have\", \"weren't\": \"were not\", \"what'll\": \"what will\", \"what'll've\": \"what will have\", \"what're\": \"what are\", \"what's\": \"what is\", \"what've\": \"what have\", \"when's\": \"when is\", \"when've\": \"when have\", \"where'd\": \"where did\", \"where's\": \"where is\", \"where've\": \"where have\", \"who'll\": \"who will\", \"who'll've\": \"who will have\", \"who's\": \"who is\", \"who've\": \"who have\", \"why's\": \"why is\", \"why've\": \"why have\", \"will've\": \"will have\", \"won't\": \"will not\", \"won't've\": \"will not have\", \"would've\": \"would have\", \"wouldn't\": \"would not\", \"wouldn't've\": \"would not have\", \"y'all\": \"you all\", \"y'all'd\": \"you all would\",\"y'all'd've\": \"you all would have\",\"y'all're\": \"you all are\",\"y'all've\": \"you all have\",\"you'd\": \"you would\", \"you'd've\": \"you would have\", \"you'll\": \"you will\", \"you'll've\": \"you will have\", \"you're\": \"you are\", \"you've\": \"you have\" }\n", 129 | "punctuation = \"'!()-[]{};:'\\,<>./?@#$%^&*_~\"\n", 130 | "\n", 131 | "def expand_contractions(data_str):\n", 132 | " specials = [\"’\", \"‘\", \"´\", \"`\", \"’\"]\n", 133 | " for s in specials:\n", 134 | " data_str = data_str.replace(s, \"'\")\n", 135 | " data_str = ' '.join([contraction_mapping[t] if t in contraction_mapping else t for t in data_str.split(\" \")])\n", 136 | " return data_str\n", 137 | "\n", 138 | "def extra_spaces(data_str):\n", 139 | " return re.sub(r'\\s{1,}', ' ', data_str)\n", 140 | "\n", 141 | "def punctuations(data_str):\n", 142 | " data_str = data_str.replace(\"'s\", \"\")\n", 143 | " for x in data_str.lower():\n", 144 | " if x in punctuation: \n", 145 | " data_str = data_str.replace(x, \"\")\n", 146 | " return data_str\n", 147 | "\n", 148 | "def remove_stop(data_str):\n", 149 | " word_tokens = nltk.word_tokenize(data_str)\n", 150 | " filtered_sentence = [w for w in word_tokens if not w in stop] \n", 151 | " return ' '.join(filtered_sentence)\n", 152 | "\n", 153 | "def preprocess(data_str):\n", 154 | " data_str = expand_contractions(data_str)\n", 155 | " data_str = remove_stop(data_str)\n", 156 | " data_str = punctuations(data_str)\n", 157 | " data_str = extra_spaces(data_str)\n", 158 | " return data_str\n", 159 | "\n", 160 | "def precision(pred, actual):\n", 161 | " N = [i for i in pred if i in actual]\n", 162 | " _D = [i for i in pred if i not in actual]\n", 163 | " return len(N) / (len(N)+len(_D)), N, _D\n", 164 | "\n", 165 | "def recall(pred, actual):\n", 166 | " N = [i for i in pred if i in actual]\n", 167 | " _D = [i for i in actual if i not in pred]\n", 168 | " return len(N) / (len(N)+len(_D)), N, _D\n", 169 | "\n", 170 | "def f1score(precision, recall):\n", 171 | " return (2*precision*recall) / (precision + recall)\n", 172 | "\n", 173 | "def ner(data):\n", 174 | " ner_chunks = set()\n", 175 | " ner_chunks_cat = set()\n", 176 | " doc = nlp(data)\n", 177 | " for ent in doc.ents:\n", 178 | " ner_chunks.add(ent.text)\n", 179 | " ner_chunks_cat.add(ent.label_)\n", 180 | " return ner_chunks, ner_chunks_cat\n", 181 | "\n", 182 | "full_data = ' '.join([i.strip() for i in DATA_DIR if len(i.strip())>1])\n", 183 | "# ner_chunks, ner_chunks_cat = ner(full_data)\n", 184 | "# for i in ner_chunks:\n", 185 | "# full_data = full_data.replace(i, '')\n", 186 | "full_data_ = full_data.lower()\n", 187 | "data = nltk.sent_tokenize(full_data_)\n", 188 | "#data = [i for i in data]" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 16, 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "sss = set([i for i in true_data_lst if i in ' '.join(data).split()])" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 37, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "# from gensim.summarization import keywords\n", 207 | "# np_chunkss = keywords(' '.join(data), lemmatize=True).split('\\n')" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 38, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "635" 219 | ] 220 | }, 221 | "execution_count": 38, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "# len(np_chunkss)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 49, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [ 236 | "# #####\n", 237 | "# # np_chunks = set()\n", 238 | "# # doc = nlp(data)\n", 239 | "# # for chunk in doc.noun_chunks:\n", 240 | "# # np_chunks.add(chunk.text)\n", 241 | "\n", 242 | "# ######\n", 243 | "# #np_chunks = set(nltk.word_tokenize(data))\n", 244 | "\n", 245 | "# ######\n", 246 | "np_chunks = []\n", 247 | "is_noun = lambda pos: pos[:2] in ['NN', 'JJ']\n", 248 | "for sent in data:\n", 249 | " tokenized = nltk.word_tokenize(sent)\n", 250 | " np_chunks.append([word for (word, pos) in nltk.pos_tag(tokenized) if is_noun(pos)])" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 50, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "np_chunks = set([lemmatizer.lemmatize(item.strip(punctuation)) for sublist in np_chunks for item in sublist])\n", 260 | "np_chunks = [i for i in np_chunks if '-' not in i]" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 51, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "# np_chunks = set([lemmatizer.lemmatize(item.strip(punctuation)) for item in ['sense']])\n" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 52, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [ 278 | "# np_chunks.extend(np_chunkss)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 53, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [ 287 | "np_chunks = set(np_chunks)" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 54, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "name": "stdout", 297 | "output_type": "stream", 298 | "text": [ 299 | "Precision = 0.020177562550443905\n", 300 | "Recall = 0.7352941176470589\n", 301 | "F1 = 0.03927729772191673\n", 302 | "TP=75, FP=3642, FN=27\n" 303 | ] 304 | } 305 | ], 306 | "source": [ 307 | "prec, TP, FP = precision(np_chunks, true_data_lst)\n", 308 | "rec, TP, FN = recall(np_chunks, true_data_lst)\n", 309 | "\n", 310 | "print (f\"Precision = {prec}\")\n", 311 | "print (f\"Recall = {rec}\")\n", 312 | "print (f\"F1 = {f1score(prec, rec)}\")\n", 313 | "print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")\n" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 71, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "def preprocess(np_chunks):\n", 323 | " keep, remove = [], []\n", 324 | " for i in np_chunks:\n", 325 | " if i not in stop and '-' not in i and len(i) > 3:\n", 326 | " keep.append(i)\n", 327 | " else:\n", 328 | " remove.append(i)\n", 329 | " return keep, remove\n", 330 | "\n", 331 | "data_preprocess_removed, preprocess_removed = preprocess(np_chunks)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 72, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "data": { 341 | "text/plain": [ 342 | "(2096, 1328)" 343 | ] 344 | }, 345 | "execution_count": 72, 346 | "metadata": {}, 347 | "output_type": "execute_result" 348 | } 349 | ], 350 | "source": [ 351 | "len(data_preprocess_removed), len(preprocess_removed)" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 73, 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "name": "stdout", 361 | "output_type": "stream", 362 | "text": [ 363 | "Precision = 0.03482824427480916\n", 364 | "Recall = 0.7156862745098039\n", 365 | "F1 = 0.06642402183803459\n", 366 | "TP=73, FP=2023, FN=29\n" 367 | ] 368 | } 369 | ], 370 | "source": [ 371 | "prec, TP, FP = precision(data_preprocess_removed, true_data_lst)\n", 372 | "rec, TP, FN = recall(data_preprocess_removed, true_data_lst)\n", 373 | "\n", 374 | "print (f\"Precision = {prec}\")\n", 375 | "print (f\"Recall = {rec}\")\n", 376 | "print (f\"F1 = {f1score(prec, rec)}\")\n", 377 | "print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 74, 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [ 386 | "common = [i.strip() for i in COMMON_DIR]\n", 387 | "\n", 388 | "def remove_google_common(np_chunks):\n", 389 | " keep, remove = [], []\n", 390 | " for i in np_chunks:\n", 391 | " if i not in common:\n", 392 | " keep.append(i)\n", 393 | " else:\n", 394 | " remove.append(i)\n", 395 | " \n", 396 | " return keep, remove\n", 397 | "\n", 398 | "data_common_removed, common_removed = remove_google_common(data_preprocess_removed)" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 75, 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "data": { 408 | "text/plain": [ 409 | "(1413, 683)" 410 | ] 411 | }, 412 | "execution_count": 75, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [ 418 | "len(data_common_removed), len(common_removed)" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 76, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "name": "stdout", 428 | "output_type": "stream", 429 | "text": [ 430 | "Precision = 0.05024769992922859\n", 431 | "Recall = 0.696078431372549\n", 432 | "F1 = 0.09372937293729372\n", 433 | "TP=71, FP=1342, FN=31\n" 434 | ] 435 | } 436 | ], 437 | "source": [ 438 | "prec, TP, FP = precision(data_common_removed, true_data_lst)\n", 439 | "rec, TP, FN = recall(data_common_removed, true_data_lst)\n", 440 | "\n", 441 | "print (f\"Precision = {prec}\")\n", 442 | "print (f\"Recall = {rec}\")\n", 443 | "print (f\"F1 = {f1score(prec, rec)}\")\n", 444 | "print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 79, 450 | "metadata": {}, 451 | "outputs": [], 452 | "source": [ 453 | "DDD = nltk.sent_tokenize(full_data)\n" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 110, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "ner_chunks = set()\n", 463 | "ner_chunks_cat = set()\n", 464 | "for sent in DDD:\n", 465 | " doc = nlp(sent)\n", 466 | " for ent in doc.ents:\n", 467 | " ner_chunks.add(ent.text)\n", 468 | " ner_chunks_cat.add(ent.label_)" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 86, 474 | "metadata": {}, 475 | "outputs": [ 476 | { 477 | "data": { 478 | "text/plain": [ 479 | "1261" 480 | ] 481 | }, 482 | "execution_count": 86, 483 | "metadata": {}, 484 | "output_type": "execute_result" 485 | } 486 | ], 487 | "source": [ 488 | "len(ner_chunks)" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": 112, 494 | "metadata": {}, 495 | "outputs": [], 496 | "source": [ 497 | "ner_chunks = [i.lower() for i in ner_chunks]" 498 | ] 499 | }, 500 | { 501 | "cell_type": "code", 502 | "execution_count": 134, 503 | "metadata": {}, 504 | "outputs": [], 505 | "source": [ 506 | "def remove_ner(np_chunks):\n", 507 | " keep, remove = [], []\n", 508 | " for i in np_chunks:\n", 509 | " if i not in sss:\n", 510 | " keep.append(i)\n", 511 | " else:\n", 512 | " remove.append(i)\n", 513 | " return keep, remove\n", 514 | "\n", 515 | "data_ner_removed, ner_removed = remove_ner(data_common_removed)" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 135, 521 | "metadata": {}, 522 | "outputs": [ 523 | { 524 | "data": { 525 | "text/plain": [ 526 | "(145, 1268)" 527 | ] 528 | }, 529 | "execution_count": 135, 530 | "metadata": {}, 531 | "output_type": "execute_result" 532 | } 533 | ], 534 | "source": [ 535 | "len(data_ner_removed), len(ner_removed)" 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": 136, 541 | "metadata": {}, 542 | "outputs": [], 543 | "source": [ 544 | "# ner_removed" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": 137, 550 | "metadata": {}, 551 | "outputs": [ 552 | { 553 | "name": "stdout", 554 | "output_type": "stream", 555 | "text": [ 556 | "Precision = 0.07586206896551724\n", 557 | "Recall = 0.10784313725490197\n", 558 | "F1 = 0.08906882591093118\n", 559 | "TP=11, FP=134, FN=91\n" 560 | ] 561 | } 562 | ], 563 | "source": [ 564 | "prec, TP, FP = precision(data_ner_removed, true_data_lst)\n", 565 | "rec, TP, FN = recall(data_ner_removed, true_data_lst)\n", 566 | "\n", 567 | "print (f\"Precision = {prec}\")\n", 568 | "print (f\"Recall = {rec}\")\n", 569 | "print (f\"F1 = {f1score(prec, rec)}\")\n", 570 | "print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": 97, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [ 579 | "# wc" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 98, 585 | "metadata": {}, 586 | "outputs": [], 587 | "source": [ 588 | "wc = dict(collections.Counter(nltk.word_tokenize(' '.join([lemmatizer.lemmatize(i).strip(punctuation) for i in ' '.join(data).split()]))))\n", 589 | "# maxx = sorted(wc.items(), key=lambda k: k[1], reverse=True)[0][1]\n", 590 | "# wc_freq = {k: v/maxx for k, v in wc.items()}\n", 591 | "\n", 592 | "# plt.bar(range(len(wc_freq)), list(wc_freq.values()), align='center')\n", 593 | "# plt.xticks(range(len(wc_freq)), list(wc_freq.keys()))\n", 594 | "\n", 595 | "# plt.show()" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": null, 601 | "metadata": {}, 602 | "outputs": [], 603 | "source": [] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": 99, 608 | "metadata": {}, 609 | "outputs": [], 610 | "source": [ 611 | "# def remove_high_low_freq(np_chunks):\n", 612 | "# keep, remove = [], []\n", 613 | "# for i in np_chunks:\n", 614 | "# if i in wc_freq:\n", 615 | "# if wc_freq[i] < 0.95:\n", 616 | "# keep.append(i)\n", 617 | "# else:\n", 618 | "# remove.append(i)\n", 619 | "# else:\n", 620 | "# remove.append(i)\n", 621 | "# return keep, remove\n", 622 | "\n", 623 | "\n", 624 | "# data_freq_removed, freq_removed = remove_high_low_freq(data_preprocess_removed)" 625 | ] 626 | }, 627 | { 628 | "cell_type": "code", 629 | "execution_count": 100, 630 | "metadata": {}, 631 | "outputs": [], 632 | "source": [ 633 | "# for i in data_freq_removed:\n", 634 | "# if '-' in i:\n", 635 | "# print (i)" 636 | ] 637 | }, 638 | { 639 | "cell_type": "code", 640 | "execution_count": 101, 641 | "metadata": {}, 642 | "outputs": [], 643 | "source": [ 644 | "# len(data_freq_removed), len(freq_removed)" 645 | ] 646 | }, 647 | { 648 | "cell_type": "code", 649 | "execution_count": 102, 650 | "metadata": {}, 651 | "outputs": [], 652 | "source": [ 653 | "# prec, TP, FP = precision(data_freq_removed, true_data_lst)\n", 654 | "# rec, TP, FN = recall(data_freq_removed, true_data_lst)\n", 655 | "\n", 656 | "# print (f\"Precision = {prec}\")\n", 657 | "# print (f\"Recall = {rec}\")\n", 658 | "# print (f\"F1 = {f1score(prec, rec)}\")\n", 659 | "# print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")" 660 | ] 661 | }, 662 | { 663 | "cell_type": "code", 664 | "execution_count": 103, 665 | "metadata": {}, 666 | "outputs": [], 667 | "source": [ 668 | "def remove_repeated_noisy_chars(np_chunks):\n", 669 | " keep, remove = [], []\n", 670 | " for i in np_chunks:\n", 671 | " if len(re.findall(r'(.)\\1{2,}', i)):\n", 672 | " remove.append(i)\n", 673 | " else:\n", 674 | " keep.append(i)\n", 675 | " return keep, remove\n", 676 | "\n", 677 | "data_noisy_removed, noisy_removed = remove_repeated_noisy_chars(data_ner_removed)" 678 | ] 679 | }, 680 | { 681 | "cell_type": "code", 682 | "execution_count": 104, 683 | "metadata": {}, 684 | "outputs": [ 685 | { 686 | "data": { 687 | "text/plain": [ 688 | "(1162, 7)" 689 | ] 690 | }, 691 | "execution_count": 104, 692 | "metadata": {}, 693 | "output_type": "execute_result" 694 | } 695 | ], 696 | "source": [ 697 | "len(data_noisy_removed), len(noisy_removed)" 698 | ] 699 | }, 700 | { 701 | "cell_type": "code", 702 | "execution_count": 105, 703 | "metadata": {}, 704 | "outputs": [ 705 | { 706 | "data": { 707 | "text/plain": [ 708 | "['haaa', 'hmmm', 'shhhh', 'haaaaaa', 'aaaargh', 'aaaaaaaaaaargh', 'shhh']" 709 | ] 710 | }, 711 | "execution_count": 105, 712 | "metadata": {}, 713 | "output_type": "execute_result" 714 | } 715 | ], 716 | "source": [ 717 | "noisy_removed" 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 106, 723 | "metadata": {}, 724 | "outputs": [ 725 | { 726 | "name": "stdout", 727 | "output_type": "stream", 728 | "text": [ 729 | "Precision = 0.043029259896729774\n", 730 | "Recall = 0.49019607843137253\n", 731 | "F1 = 0.07911392405063292\n", 732 | "TP=50, FP=1112, FN=52\n" 733 | ] 734 | } 735 | ], 736 | "source": [ 737 | "prec, TP, FP = precision(data_noisy_removed, true_data_lst)\n", 738 | "rec, TP, FN = recall(data_noisy_removed, true_data_lst)\n", 739 | "\n", 740 | "print (f\"Precision = {prec}\")\n", 741 | "print (f\"Recall = {rec}\")\n", 742 | "print (f\"F1 = {f1score(prec, rec)}\")\n", 743 | "print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")" 744 | ] 745 | }, 746 | { 747 | "cell_type": "code", 748 | "execution_count": 107, 749 | "metadata": {}, 750 | "outputs": [], 751 | "source": [ 752 | "def remove_specific_corpus(np_chunks):\n", 753 | " keep, remove = [], []\n", 754 | " for i in np_chunks:\n", 755 | " if i in wc:\n", 756 | " c1 = wc[i]\n", 757 | " if i in brown_words:\n", 758 | " c2 = brown_words[i]\n", 759 | " if c1 > c2:\n", 760 | " keep.append(i)\n", 761 | " else: remove.append(i)\n", 762 | " else:\n", 763 | " keep.append(i)\n", 764 | " else:\n", 765 | " keep.append(i)\n", 766 | " \n", 767 | " return keep, remove\n", 768 | "\n", 769 | "data_specific_removed, specific_removed = remove_specific_corpus(data_noisy_removed)" 770 | ] 771 | }, 772 | { 773 | "cell_type": "code", 774 | "execution_count": 108, 775 | "metadata": {}, 776 | "outputs": [ 777 | { 778 | "data": { 779 | "text/plain": [ 780 | "(467, 695)" 781 | ] 782 | }, 783 | "execution_count": 108, 784 | "metadata": {}, 785 | "output_type": "execute_result" 786 | } 787 | ], 788 | "source": [ 789 | "len(data_specific_removed), len(specific_removed)" 790 | ] 791 | }, 792 | { 793 | "cell_type": "code", 794 | "execution_count": 109, 795 | "metadata": {}, 796 | "outputs": [ 797 | { 798 | "name": "stdout", 799 | "output_type": "stream", 800 | "text": [ 801 | "Precision = 0.06638115631691649\n", 802 | "Recall = 0.30392156862745096\n", 803 | "F1 = 0.10896309314586994\n", 804 | "TP=31, FP=436, FN=71\n" 805 | ] 806 | } 807 | ], 808 | "source": [ 809 | "prec, TP, FP = precision(data_specific_removed, true_data_lst)\n", 810 | "rec, TP, FN = recall(data_specific_removed, true_data_lst)\n", 811 | "\n", 812 | "print (f\"Precision = {prec}\")\n", 813 | "print (f\"Recall = {rec}\")\n", 814 | "print (f\"F1 = {f1score(prec, rec)}\")\n", 815 | "print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")" 816 | ] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "execution_count": 1, 821 | "metadata": {}, 822 | "outputs": [], 823 | "source": [ 824 | "import gensim\n", 825 | "from gensim.models import Word2Vec\n", 826 | "from gensim.utils import simple_preprocess\n", 827 | "from gensim.models.keyedvectors import KeyedVectors\n", 828 | "import numpy as np\n", 829 | "\n", 830 | "filepath = \"/home/prakhar/Downloads/GoogleNews-vectors-negative300.bin\"\n", 831 | "\n", 832 | "from gensim.models import KeyedVectors\n", 833 | "wv_from_bin = KeyedVectors.load_word2vec_format(filepath, binary=True) \n", 834 | "#extracting words7 vectors from google news vector\n", 835 | "embeddings_index = {}\n", 836 | "for word, vector in zip(wv_from_bin.vocab, wv_from_bin.vectors):\n", 837 | " coefs = np.asarray(vector, dtype='float32')\n", 838 | " embeddings_index[word] = coefs\n", 839 | " " 840 | ] 841 | }, 842 | { 843 | "cell_type": "code", 844 | "execution_count": 438, 845 | "metadata": {}, 846 | "outputs": [], 847 | "source": [ 848 | "def avg_feature_vector(sentence, model, num_features):\n", 849 | " words = sentence.split()\n", 850 | " #feature vector is initialized as an empty array\n", 851 | " feature_vec = np.zeros((num_features, ), dtype='float32')\n", 852 | " n_words = 0\n", 853 | " for word in words:\n", 854 | " if word in embeddings_index.keys():\n", 855 | " n_words += 1\n", 856 | " feature_vec = np.add(feature_vec, model[word])\n", 857 | " if (n_words > 0):\n", 858 | " feature_vec = np.divide(feature_vec, n_words)\n", 859 | " return feature_vec" 860 | ] 861 | }, 862 | { 863 | "cell_type": "code", 864 | "execution_count": 439, 865 | "metadata": {}, 866 | "outputs": [], 867 | "source": [ 868 | "context_vec = avg_feature_vector(full_data, model= embeddings_index, num_features=300)" 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 440, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "import pandas as pd\n", 878 | "from scipy.spatial import distance\n", 879 | "\n", 880 | "d = pd.DataFrame(data_specific_removed, columns=['glossary'])\n", 881 | "d['sim'] = d['glossary'].apply(lambda x: distance.cosine(avg_feature_vector(x, model= embeddings_index, num_features=300), context_vec))" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 441, 887 | "metadata": {}, 888 | "outputs": [ 889 | { 890 | "data": { 891 | "text/plain": [ 892 | "(878, 2)" 893 | ] 894 | }, 895 | "execution_count": 441, 896 | "metadata": {}, 897 | "output_type": "execute_result" 898 | } 899 | ], 900 | "source": [ 901 | "d.shape" 902 | ] 903 | }, 904 | { 905 | "cell_type": "code", 906 | "execution_count": null, 907 | "metadata": {}, 908 | "outputs": [], 909 | "source": [] 910 | }, 911 | { 912 | "cell_type": "code", 913 | "execution_count": 445, 914 | "metadata": {}, 915 | "outputs": [], 916 | "source": [ 917 | "dd = d.sort_values('sim', ascending=False).head(800)" 918 | ] 919 | }, 920 | { 921 | "cell_type": "code", 922 | "execution_count": 446, 923 | "metadata": {}, 924 | "outputs": [ 925 | { 926 | "data": { 927 | "text/plain": [ 928 | "(800, 2)" 929 | ] 930 | }, 931 | "execution_count": 446, 932 | "metadata": {}, 933 | "output_type": "execute_result" 934 | } 935 | ], 936 | "source": [ 937 | "dd.shape" 938 | ] 939 | }, 940 | { 941 | "cell_type": "code", 942 | "execution_count": 447, 943 | "metadata": {}, 944 | "outputs": [ 945 | { 946 | "name": "stdout", 947 | "output_type": "stream", 948 | "text": [ 949 | "Precision = 0.06125\n", 950 | "Recall = 0.4803921568627451\n", 951 | "F1 = 0.10864745011086474\n", 952 | "TP=49, FP=751, FN=53\n" 953 | ] 954 | } 955 | ], 956 | "source": [ 957 | "prec, TP, FP = precision(dd['glossary'].tolist(), true_data_lst)\n", 958 | "rec, TP, FN = recall(dd['glossary'].tolist(), true_data_lst)\n", 959 | "\n", 960 | "print (f\"Precision = {prec}\")\n", 961 | "print (f\"Recall = {rec}\")\n", 962 | "print (f\"F1 = {f1score(prec, rec)}\")\n", 963 | "print (f\"TP={len(TP)}, FP={len(FP)}, FN={len(FN)}\")" 964 | ] 965 | }, 966 | { 967 | "cell_type": "code", 968 | "execution_count": 448, 969 | "metadata": {}, 970 | "outputs": [ 971 | { 972 | "data": { 973 | "text/plain": [ 974 | "['stra',\n", 975 | " 'argus',\n", 976 | " 'infusion',\n", 977 | " 'mortis',\n", 978 | " 'multilevel',\n", 979 | " 'nimbus',\n", 980 | " 'discus',\n", 981 | " 'boater',\n", 982 | " 'ember',\n", 983 | " 'vein',\n", 984 | " 'ensnaring',\n", 985 | " 'heartstrings',\n", 986 | " 'headmistress',\n", 987 | " 'patil',\n", 988 | " 'headmaster',\n", 989 | " 'hygienic',\n", 990 | " 'sens',\n", 991 | " 'stalagmite',\n", 992 | " 'trevor',\n", 993 | " 'sleepiness',\n", 994 | " 'javelin',\n", 995 | " 'emporium',\n", 996 | " 'smelting',\n", 997 | " 'pigtail',\n", 998 | " 'locomotor',\n", 999 | " 'newscaster',\n", 1000 | " 'eyeglass',\n", 1001 | " 'coil',\n", 1002 | " 'weatherman',\n", 1003 | " 'parkinson',\n", 1004 | " 'baron',\n", 1005 | " 'fastening',\n", 1006 | " 'stargazer',\n", 1007 | " 'palomino',\n", 1008 | " 'detention',\n", 1009 | " 'wormwood',\n", 1010 | " 'pupil',\n", 1011 | " 'wastepaper',\n", 1012 | " 'cleansweep',\n", 1013 | " 'supple',\n", 1014 | " 'albus',\n", 1015 | " 'ruff',\n", 1016 | " 'tinned',\n", 1017 | " 'passageway',\n", 1018 | " 'phial',\n", 1019 | " 'sheared',\n", 1020 | " 'fungi',\n", 1021 | " 'pellet',\n", 1022 | " 'tendril',\n", 1023 | " 'fang',\n", 1024 | " 'warty',\n", 1025 | " 'squid',\n", 1026 | " 'tabby',\n", 1027 | " 'witchcraft',\n", 1028 | " 'friar',\n", 1029 | " 'unseated',\n", 1030 | " 'goshawk',\n", 1031 | " 'marge',\n", 1032 | " 'dolphin',\n", 1033 | " 'spying',\n", 1034 | " 'beetle',\n", 1035 | " 'aconite',\n", 1036 | " 'elixir',\n", 1037 | " 'transfiguration',\n", 1038 | " 'seared',\n", 1039 | " 'gorgon',\n", 1040 | " 'teabags',\n", 1041 | " 'defrosting',\n", 1042 | " 'snout',\n", 1043 | " 'quaffle',\n", 1044 | " 'bluebell',\n", 1045 | " 'bott',\n", 1046 | " 'bowler',\n", 1047 | " 'wizarding',\n", 1048 | " 'escalator',\n", 1049 | " 'archway',\n", 1050 | " 'warlock',\n", 1051 | " 'fingertip',\n", 1052 | " 'peppermint',\n", 1053 | " 'clinking',\n", 1054 | " 'chessboard',\n", 1055 | " 'midair',\n", 1056 | " 'unsticking',\n", 1057 | " 'scrawl',\n", 1058 | " 'dundee',\n", 1059 | " 'dormitory',\n", 1060 | " 'snakelike',\n", 1061 | " 'alchemist',\n", 1062 | " 'bloodcurdling',\n", 1063 | " 'stalactite',\n", 1064 | " 'spiny',\n", 1065 | " 'drawling',\n", 1066 | " 'goggle',\n", 1067 | " 'refereed',\n", 1068 | " 'muggles',\n", 1069 | " 'bungler',\n", 1070 | " 'prefect',\n", 1071 | " 'sorcerer',\n", 1072 | " 'yvonne',\n", 1073 | " 'thickset',\n", 1074 | " 'stabbing',\n", 1075 | " 'privet',\n", 1076 | " 'bustled',\n", 1077 | " 'disapproving',\n", 1078 | " 'parchment',\n", 1079 | " 'reptile',\n", 1080 | " 'voicing',\n", 1081 | " 'sizing',\n", 1082 | " 'hourglass',\n", 1083 | " 'merlin',\n", 1084 | " 'bodyguard',\n", 1085 | " 'sirius',\n", 1086 | " 'dungeon',\n", 1087 | " 'thinnest',\n", 1088 | " 'frying',\n", 1089 | " 'earmuff',\n", 1090 | " 'staircase',\n", 1091 | " 'festoon',\n", 1092 | " 'crossbow',\n", 1093 | " 'grayish',\n", 1094 | " 'goblet',\n", 1095 | " 'rebellion',\n", 1096 | " 'expelled',\n", 1097 | " 'tartan',\n", 1098 | " 'beak',\n", 1099 | " 'mossy',\n", 1100 | " 'unwrapped',\n", 1101 | " 'waster',\n", 1102 | " 'collapsible',\n", 1103 | " 'furling',\n", 1104 | " 'dudley',\n", 1105 | " 'reckons',\n", 1106 | " 'invisibility',\n", 1107 | " 'whittled',\n", 1108 | " 'monkshood',\n", 1109 | " 'shelling',\n", 1110 | " 'philosopher',\n", 1111 | " 'beastie',\n", 1112 | " 'alibi',\n", 1113 | " 'foghorn',\n", 1114 | " 'bloodiness',\n", 1115 | " 'gamekeeper',\n", 1116 | " 'corridor',\n", 1117 | " 'dimpled',\n", 1118 | " 'duster',\n", 1119 | " 'moleskin',\n", 1120 | " 'bane',\n", 1121 | " 'whisperer',\n", 1122 | " 'sickle',\n", 1123 | " 'ledger',\n", 1124 | " 'werewolf',\n", 1125 | " 'bewitching',\n", 1126 | " 'commentating',\n", 1127 | " 'checkmate',\n", 1128 | " 'trowel',\n", 1129 | " 'tureen',\n", 1130 | " 'bushy',\n", 1131 | " 'hoodlum',\n", 1132 | " 'rowboat',\n", 1133 | " 'icicle',\n", 1134 | " 'peeve',\n", 1135 | " 'flute',\n", 1136 | " 'biased',\n", 1137 | " 'behead',\n", 1138 | " 'dentist',\n", 1139 | " 'gargoyle',\n", 1140 | " 'loosening',\n", 1141 | " 'pinned',\n", 1142 | " 'sacked',\n", 1143 | " 'abou',\n", 1144 | " 'enclose',\n", 1145 | " 'wafting',\n", 1146 | " 'centaur',\n", 1147 | " 'wizardry',\n", 1148 | " 'wheezing',\n", 1149 | " 'turban',\n", 1150 | " 'marmalade',\n", 1151 | " 'cauldron',\n", 1152 | " 'tuft',\n", 1153 | " 'flint',\n", 1154 | " 'thronging',\n", 1155 | " 'referee',\n", 1156 | " 'whelk',\n", 1157 | " 'foretold',\n", 1158 | " 'scarlet',\n", 1159 | " 'teacup',\n", 1160 | " 'yellowish',\n", 1161 | " 'swishy',\n", 1162 | " 'milkman',\n", 1163 | " 'galleon',\n", 1164 | " 'filch',\n", 1165 | " 'savaging',\n", 1166 | " 'alchemy',\n", 1167 | " 'spawn',\n", 1168 | " 'flagged',\n", 1169 | " 'springy',\n", 1170 | " 'lullaby',\n", 1171 | " 'twanging',\n", 1172 | " 'getup',\n", 1173 | " 'treacle',\n", 1174 | " 'meringue',\n", 1175 | " 'rocketed',\n", 1176 | " 'nettle',\n", 1177 | " 'simmering',\n", 1178 | " 'blistering',\n", 1179 | " 'hatch',\n", 1180 | " 'knickerbockers',\n", 1181 | " 'steamrollered',\n", 1182 | " 'cloaked',\n", 1183 | " 'cobbled',\n", 1184 | " 'inky',\n", 1185 | " 'staffroom',\n", 1186 | " 'paisley',\n", 1187 | " 'helmeted',\n", 1188 | " 'bandage',\n", 1189 | " 'heartstring',\n", 1190 | " 'shadowy',\n", 1191 | " 'petunia',\n", 1192 | " 'greener',\n", 1193 | " 'stool',\n", 1194 | " 'glimmered',\n", 1195 | " 'shallow',\n", 1196 | " 'snuffbox',\n", 1197 | " 'singsong',\n", 1198 | " 'licorice',\n", 1199 | " 'woodcraft',\n", 1200 | " 'headless',\n", 1201 | " 'tallest',\n", 1202 | " 'pier',\n", 1203 | " 'clanging',\n", 1204 | " 'silvery',\n", 1205 | " 'hooch',\n", 1206 | " 'speeding',\n", 1207 | " 'paler',\n", 1208 | " 'blundering',\n", 1209 | " 'befuddle',\n", 1210 | " 'whippy',\n", 1211 | " 'tailcoat',\n", 1212 | " 'leaking',\n", 1213 | " 'flick',\n", 1214 | " 'transfigured',\n", 1215 | " 'herbology',\n", 1216 | " 'keeper',\n", 1217 | " 'outstretched',\n", 1218 | " 'cackle',\n", 1219 | " 'gawked',\n", 1220 | " 'craning',\n", 1221 | " 'pelted',\n", 1222 | " 'shrank',\n", 1223 | " 'bendy',\n", 1224 | " 'billowing',\n", 1225 | " 'binoculars',\n", 1226 | " 'scattering',\n", 1227 | " 'refereeing',\n", 1228 | " 'tarantula',\n", 1229 | " 'quidditch',\n", 1230 | " 'towered',\n", 1231 | " 'puncture',\n", 1232 | " 'gloomy',\n", 1233 | " 'chaser',\n", 1234 | " 'tulip',\n", 1235 | " 'amigo',\n", 1236 | " 'whooshing',\n", 1237 | " 'robe',\n", 1238 | " 'spindly',\n", 1239 | " 'clouted',\n", 1240 | " 'wizened',\n", 1241 | " 'trodden',\n", 1242 | " 'sprout',\n", 1243 | " 'whisk',\n", 1244 | " 'teapot',\n", 1245 | " 'carrot',\n", 1246 | " 'unwrapping',\n", 1247 | " 'bathrobe',\n", 1248 | " 'zooming',\n", 1249 | " 'asphodel',\n", 1250 | " 'birdcage',\n", 1251 | " 'facedown',\n", 1252 | " 'flap',\n", 1253 | " 'snitch',\n", 1254 | " 'bezoar',\n", 1255 | " 'scruff',\n", 1256 | " 'stuttering',\n", 1257 | " 'darkly',\n", 1258 | " 'sloped',\n", 1259 | " 'gorilla',\n", 1260 | " 'tosh',\n", 1261 | " 'sniffling',\n", 1262 | " 'bobbing',\n", 1263 | " 'tickling',\n", 1264 | " 'ketchup',\n", 1265 | " 'admirer',\n", 1266 | " 'doormat',\n", 1267 | " 'sorting',\n", 1268 | " 'trapdoor',\n", 1269 | " 'sniffy',\n", 1270 | " 'crate',\n", 1271 | " 'cozy',\n", 1272 | " 'pasty',\n", 1273 | " 'jostled',\n", 1274 | " 'musty',\n", 1275 | " 'starry',\n", 1276 | " 'squeaked',\n", 1277 | " 'caretaker',\n", 1278 | " 'windowsill',\n", 1279 | " 'moldy',\n", 1280 | " 'pumpkin',\n", 1281 | " 'wringing',\n", 1282 | " 'slipper',\n", 1283 | " 'babble',\n", 1284 | " 'changin',\n", 1285 | " 'correcting',\n", 1286 | " 'broomstick',\n", 1287 | " 'rumbling',\n", 1288 | " 'stoat',\n", 1289 | " 'crunching',\n", 1290 | " 'snore',\n", 1291 | " 'smarmy',\n", 1292 | " 'vibrate',\n", 1293 | " 'hooded',\n", 1294 | " 'swooped',\n", 1295 | " 'fume',\n", 1296 | " 'flickering',\n", 1297 | " 'straying',\n", 1298 | " 'muggle',\n", 1299 | " 'kindling',\n", 1300 | " 'steeling',\n", 1301 | " 'draco',\n", 1302 | " 'bossy',\n", 1303 | " 'snowy',\n", 1304 | " 'goblin',\n", 1305 | " 'slouched',\n", 1306 | " 'buttered',\n", 1307 | " 'deafening',\n", 1308 | " 'slimy',\n", 1309 | " 'glittered',\n", 1310 | " 'batty',\n", 1311 | " 'spluttering',\n", 1312 | " 'dreadlock',\n", 1313 | " 'passersby',\n", 1314 | " 'oddball',\n", 1315 | " 'muffin',\n", 1316 | " 'porridge',\n", 1317 | " 'carousel',\n", 1318 | " 'wand',\n", 1319 | " 'potion',\n", 1320 | " 'leaky',\n", 1321 | " 'scribbled',\n", 1322 | " 'duffer',\n", 1323 | " 'wolfsbane',\n", 1324 | " 'pinprick',\n", 1325 | " 'pewter',\n", 1326 | " 'tackled',\n", 1327 | " 'thumpin',\n", 1328 | " 'madam',\n", 1329 | " 'turnip',\n", 1330 | " 'bewitch',\n", 1331 | " 'dumpy',\n", 1332 | " 'bludgers',\n", 1333 | " 'toad',\n", 1334 | " 'crinkled',\n", 1335 | " 'skulking',\n", 1336 | " 'cloak',\n", 1337 | " 'blossoming',\n", 1338 | " 'dribbling',\n", 1339 | " 'crutch',\n", 1340 | " 'nursed',\n", 1341 | " 'quiver',\n", 1342 | " 'gamekeeping',\n", 1343 | " 'snare',\n", 1344 | " 'footstool',\n", 1345 | " 'crumpet',\n", 1346 | " 'swapping',\n", 1347 | " 'overtaking',\n", 1348 | " 'ridgeback',\n", 1349 | " 'piercing',\n", 1350 | " 'toppled',\n", 1351 | " 'stutter',\n", 1352 | " 'freckle',\n", 1353 | " 'flyin',\n", 1354 | " 'perk',\n", 1355 | " 'lumpy',\n", 1356 | " 'woken',\n", 1357 | " 'tottered',\n", 1358 | " 'waddling',\n", 1359 | " 'hushing',\n", 1360 | " 'jell',\n", 1361 | " 'shivered',\n", 1362 | " 'tights',\n", 1363 | " 'fruitcake',\n", 1364 | " 'wight',\n", 1365 | " 'gliding',\n", 1366 | " 'brandished',\n", 1367 | " 'glided',\n", 1368 | " 'drooled',\n", 1369 | " 'fudge',\n", 1370 | " 'onward',\n", 1371 | " 'fluffy',\n", 1372 | " 'stumped',\n", 1373 | " 'twinkled',\n", 1374 | " 'tearful',\n", 1375 | " 'stonewall',\n", 1376 | " 'clamber',\n", 1377 | " 'viridian',\n", 1378 | " 'infernal',\n", 1379 | " 'drooling',\n", 1380 | " 'pudding',\n", 1381 | " 'bewitched',\n", 1382 | " 'fluttered',\n", 1383 | " 'shrunk',\n", 1384 | " 'lopsided',\n", 1385 | " 'fidgeted',\n", 1386 | " 'thundered',\n", 1387 | " 'tyke',\n", 1388 | " 'glittery',\n", 1389 | " 'uric',\n", 1390 | " 'beamed',\n", 1391 | " 'shoo',\n", 1392 | " 'buyin',\n", 1393 | " 'sounding',\n", 1394 | " 'skyward',\n", 1395 | " 'snigger',\n", 1396 | " 'sorrowful',\n", 1397 | " 'catcalling',\n", 1398 | " 'flitted',\n", 1399 | " 'poltergeist',\n", 1400 | " 'codswallop',\n", 1401 | " 'scowl',\n", 1402 | " 'hurtled',\n", 1403 | " 'norris',\n", 1404 | " 'emptying',\n", 1405 | " 'riffraff',\n", 1406 | " 'toothless',\n", 1407 | " 'creepy',\n", 1408 | " 'hovered',\n", 1409 | " 'earshot',\n", 1410 | " 'taped',\n", 1411 | " 'tackling',\n", 1412 | " 'untidy',\n", 1413 | " 'bullied',\n", 1414 | " 'tinge',\n", 1415 | " 'leapt',\n", 1416 | " 'waffling',\n", 1417 | " 'swish',\n", 1418 | " 'transfixed',\n", 1419 | " 'swishing',\n", 1420 | " 'chained',\n", 1421 | " 'mantelpiece',\n", 1422 | " 'gambled',\n", 1423 | " 'slithering',\n", 1424 | " 'swapped',\n", 1425 | " 'pointy',\n", 1426 | " 'pounced',\n", 1427 | " 'squinting',\n", 1428 | " 'snuffling',\n", 1429 | " 'hannah',\n", 1430 | " 'flatten',\n", 1431 | " 'wriggle',\n", 1432 | " 'striding',\n", 1433 | " 'jinxing',\n", 1434 | " 'boastful',\n", 1435 | " 'thrashing',\n", 1436 | " 'takin',\n", 1437 | " 'screech',\n", 1438 | " 'dormouse',\n", 1439 | " 'sneeze',\n", 1440 | " 'phoning',\n", 1441 | " 'puffing',\n", 1442 | " 'sobbed',\n", 1443 | " 'dived',\n", 1444 | " 'droned',\n", 1445 | " 'pawed',\n", 1446 | " 'dunderhead',\n", 1447 | " 'mistaking',\n", 1448 | " 'cornflakes',\n", 1449 | " 'corned',\n", 1450 | " 'scrambling',\n", 1451 | " 'budge',\n", 1452 | " 'scurrying',\n", 1453 | " 'scrabbling',\n", 1454 | " 'snored',\n", 1455 | " 'wriggled',\n", 1456 | " 'disgust',\n", 1457 | " 'queasy',\n", 1458 | " 'tellin',\n", 1459 | " 'nibble',\n", 1460 | " 'neville',\n", 1461 | " 'trotting',\n", 1462 | " 'disgruntled',\n", 1463 | " 'crybaby',\n", 1464 | " 'runnin',\n", 1465 | " 'wrenched',\n", 1466 | " 'tiptoed',\n", 1467 | " 'carefull',\n", 1468 | " 'twitching',\n", 1469 | " 'skinnier',\n", 1470 | " 'cept',\n", 1471 | " 'blinding',\n", 1472 | " 'shoveling',\n", 1473 | " 'swooping',\n", 1474 | " 'whinging',\n", 1475 | " 'tingle',\n", 1476 | " 'flinging',\n", 1477 | " 'makin',\n", 1478 | " 'pebble',\n", 1479 | " 'humbug',\n", 1480 | " 'gritted',\n", 1481 | " 'sprinting',\n", 1482 | " 'galloping',\n", 1483 | " 'sprinted',\n", 1484 | " 'fishy',\n", 1485 | " 'yawning',\n", 1486 | " 'minuscule',\n", 1487 | " 'nosy',\n", 1488 | " 'furious',\n", 1489 | " 'burying',\n", 1490 | " 'punching',\n", 1491 | " 'panted',\n", 1492 | " 'cupboard',\n", 1493 | " 'gawking',\n", 1494 | " 'doorpost',\n", 1495 | " 'ginny',\n", 1496 | " 'hooted',\n", 1497 | " 'loopy',\n", 1498 | " 'ajar',\n", 1499 | " 'troll',\n", 1500 | " 'scribbling',\n", 1501 | " 'eclair',\n", 1502 | " 'guarding',\n", 1503 | " 'blinking',\n", 1504 | " 'sniggered',\n", 1505 | " 'unicorn',\n", 1506 | " 'unraveled',\n", 1507 | " 'spluttered',\n", 1508 | " 'wantin',\n", 1509 | " 'snapped',\n", 1510 | " 'rotted',\n", 1511 | " 'beater',\n", 1512 | " 'dodged',\n", 1513 | " 'squawked',\n", 1514 | " 'hammering',\n", 1515 | " 'tweak',\n", 1516 | " 'chimed',\n", 1517 | " 'ticked',\n", 1518 | " 'shuffled',\n", 1519 | " 'readin',\n", 1520 | " 'twig',\n", 1521 | " 'cryin',\n", 1522 | " 'gibber',\n", 1523 | " 'smarten',\n", 1524 | " 'weirdest',\n", 1525 | " 'sobbing',\n", 1526 | " 'dinky',\n", 1527 | " 'killin',\n", 1528 | " 'unseen',\n", 1529 | " 'ridgebacks',\n", 1530 | " 'slinking',\n", 1531 | " 'squashy',\n", 1532 | " 'wolfing',\n", 1533 | " 'berserk',\n", 1534 | " 'tiptoe',\n", 1535 | " 'darting',\n", 1536 | " 'aunt',\n", 1537 | " 'rubbish',\n", 1538 | " 'grubby',\n", 1539 | " 'gruffly',\n", 1540 | " 'meanin',\n", 1541 | " 'bated',\n", 1542 | " 'twitched',\n", 1543 | " 'nitwit',\n", 1544 | " 'tidier',\n", 1545 | " 'tricky',\n", 1546 | " 'politer',\n", 1547 | " 'wriggling',\n", 1548 | " 'whistle',\n", 1549 | " 'cheered',\n", 1550 | " 'snoozed',\n", 1551 | " 'whooped',\n", 1552 | " 'sideways',\n", 1553 | " 'snot',\n", 1554 | " 'squeak',\n", 1555 | " 'badger',\n", 1556 | " 'lookin',\n", 1557 | " 'broom',\n", 1558 | " 'smirking',\n", 1559 | " 'askew',\n", 1560 | " 'snoozing',\n", 1561 | " 'playin',\n", 1562 | " 'shiny',\n", 1563 | " 'ranting',\n", 1564 | " 'scabby',\n", 1565 | " 'conk',\n", 1566 | " 'shrieked',\n", 1567 | " 'rattled',\n", 1568 | " 'weirdo',\n", 1569 | " 'wrestled',\n", 1570 | " 'quivered',\n", 1571 | " 'turnin',\n", 1572 | " 'howling',\n", 1573 | " 'chickened',\n", 1574 | " 'ouch',\n", 1575 | " 'bludger',\n", 1576 | " 'noticing',\n", 1577 | " 'dumbfounded',\n", 1578 | " 'prickle',\n", 1579 | " 'lotta',\n", 1580 | " 'braver',\n", 1581 | " 'hugged',\n", 1582 | " 'everythin',\n", 1583 | " 'messin',\n", 1584 | " 'havin',\n", 1585 | " 'lurking',\n", 1586 | " 'dawned',\n", 1587 | " 'grumpily',\n", 1588 | " 'mutter',\n", 1589 | " 'maniac',\n", 1590 | " 'booger',\n", 1591 | " 'puddin',\n", 1592 | " 'tidy',\n", 1593 | " 'blankly',\n", 1594 | " 'dreading',\n", 1595 | " 'chased',\n", 1596 | " 'whiskery',\n", 1597 | " 'sneaking',\n", 1598 | " 'ruin',\n", 1599 | " 'watchin',\n", 1600 | " 'knowin',\n", 1601 | " 'clapped',\n", 1602 | " 'barked',\n", 1603 | " 'gasped',\n", 1604 | " 'hullo',\n", 1605 | " 'firsties',\n", 1606 | " 'yawned',\n", 1607 | " 'cheering',\n", 1608 | " 'quailed',\n", 1609 | " 'payin',\n", 1610 | " 'askin',\n", 1611 | " 'swallow',\n", 1612 | " 'chipolata',\n", 1613 | " 'growled',\n", 1614 | " 'hissed',\n", 1615 | " 'mandy',\n", 1616 | " 'knocking',\n", 1617 | " 'sneered',\n", 1618 | " 'gettin',\n", 1619 | " 'hurrying',\n", 1620 | " 'somethin',\n", 1621 | " 'terrified',\n", 1622 | " 'blackpool',\n", 1623 | " 'croaked',\n", 1624 | " 'chappie',\n", 1625 | " 'shinin',\n", 1626 | " 'prickled',\n", 1627 | " 'shoulda',\n", 1628 | " 'imagining',\n", 1629 | " 'moaning',\n", 1630 | " 'summat',\n", 1631 | " 'moaned',\n", 1632 | " 'smatter',\n", 1633 | " 'ahem',\n", 1634 | " 'nothin',\n", 1635 | " 'whimpered',\n", 1636 | " 'sayin',\n", 1637 | " 'scuse',\n", 1638 | " 'forgets',\n", 1639 | " 'speakin',\n", 1640 | " 'flinched',\n", 1641 | " 'chucked',\n", 1642 | " 'ickle',\n", 1643 | " 'chasin',\n", 1644 | " 'dunno',\n", 1645 | " 'groaned',\n", 1646 | " 'blimey',\n", 1647 | " 'sorta',\n", 1648 | " 'whispered',\n", 1649 | " 'shoutin',\n", 1650 | " 'panicking',\n", 1651 | " 'idiot',\n", 1652 | " 'percy',\n", 1653 | " 'wham',\n", 1654 | " 'crikey',\n", 1655 | " 'muttered',\n", 1656 | " 'anythin',\n", 1657 | " 'petrified',\n", 1658 | " 'outta',\n", 1659 | " 'aargh',\n", 1660 | " 'urgh',\n", 1661 | " 'chuckling',\n", 1662 | " 'dratted',\n", 1663 | " 'mighta',\n", 1664 | " 'needin',\n", 1665 | " 'guardin',\n", 1666 | " 'ppose',\n", 1667 | " 'caughty',\n", 1668 | " 'pucey',\n", 1669 | " 'crabbe',\n", 1670 | " 'druidess',\n", 1671 | " 'rubeus',\n", 1672 | " 'emeric',\n", 1673 | " 'ghostie',\n", 1674 | " 'rabbitin',\n", 1675 | " 'promisin',\n", 1676 | " 'adalbert',\n", 1677 | " 'flamel',\n", 1678 | " 'mcguffin',\n", 1679 | " 'chessman',\n", 1680 | " 'mentionin',\n", 1681 | " 'railview',\n", 1682 | " 'longbottom',\n", 1683 | " 'totalusl',\n", 1684 | " 'hedwig',\n", 1685 | " 'mcgonagall',\n", 1686 | " 'alberic',\n", 1687 | " 'higgs',\n", 1688 | " 'vindictus',\n", 1689 | " 'goyle',\n", 1690 | " 'deliverin',\n", 1691 | " 'leviosal',\n", 1692 | " 'weasley',\n", 1693 | " 'hoggy',\n", 1694 | " 'dursley',\n", 1695 | " 'morgana',\n", 1696 | " 'griphook',\n", 1697 | " 'forgettin',\n", 1698 | " 'dedalus',\n", 1699 | " 'grunnings',\n", 1700 | " 'voldemort',\n", 1701 | " 'hengist',\n", 1702 | " 'paracelsus',\n", 1703 | " 'terence',\n", 1704 | " 'diagon',\n", 1705 | " 'hufflepuffs',\n", 1706 | " 'figg',\n", 1707 | " 'ptolemy',\n", 1708 | " 'scamander',\n", 1709 | " 'drooble',\n", 1710 | " 'nosie',\n", 1711 | " 'hufflepuff',\n", 1712 | " 'paddington',\n", 1713 | " 'norbert',\n", 1714 | " 'crockford',\n", 1715 | " 'hermione',\n", 1716 | " 'somefink',\n", 1717 | " 'cliodna',\n", 1718 | " 'petrificus',\n", 1719 | " 'bathilda',\n", 1720 | " 'gallopin',\n", 1721 | " 'turpin',\n", 1722 | " 'ravenclaw',\n", 1723 | " 'granger',\n", 1724 | " 'myst',\n", 1725 | " 'finnigan',\n", 1726 | " 'bletchley',\n", 1727 | " 'millicent',\n", 1728 | " 'nonmagic',\n", 1729 | " 'wizardin',\n", 1730 | " 'slytherin',\n", 1731 | " 'grunnion',\n", 1732 | " 'weasleys',\n", 1733 | " 'erised',\n", 1734 | " 'hogwarts',\n", 1735 | " 'eeylops',\n", 1736 | " 'ehru',\n", 1737 | " 'sniveled',\n", 1738 | " 'smeltings',\n", 1739 | " 'circe',\n", 1740 | " 'binns',\n", 1741 | " 'duddy',\n", 1742 | " 'lecturin',\n", 1743 | " 'ronniekins',\n", 1744 | " 'copyin',\n", 1745 | " 'macdougal',\n", 1746 | " 'ollivander',\n", 1747 | " 'swarmin',\n", 1748 | " 'snape',\n", 1749 | " 'leviosav',\n", 1750 | " 'draconis',\n", 1751 | " 'scabbers',\n", 1752 | " 'dittany',\n", 1753 | " 'broomshed',\n", 1754 | " 'remembrall',\n", 1755 | " 'frightenin',\n", 1756 | " 'oddment',\n", 1757 | " 'undursleyish',\n", 1758 | " 'staggerin',\n", 1759 | " 'enid',\n", 1760 | " 'humberto',\n", 1761 | " 'diggle',\n", 1762 | " 'lamplike',\n", 1763 | " 'popkin',\n", 1764 | " 'slytherins',\n", 1765 | " 'parvati',\n", 1766 | " 'dudleykins',\n", 1767 | " 'algie',\n", 1768 | " 'flump',\n", 1769 | " 'elfric',\n", 1770 | " 'baruffio',\n", 1771 | " 'bulstrode',\n", 1772 | " 'insultin',\n", 1773 | " 'spinnet']" 1774 | ] 1775 | }, 1776 | "execution_count": 448, 1777 | "metadata": {}, 1778 | "output_type": "execute_result" 1779 | } 1780 | ], 1781 | "source": [ 1782 | "dd['glossary'].tolist()" 1783 | ] 1784 | }, 1785 | { 1786 | "cell_type": "code", 1787 | "execution_count": null, 1788 | "metadata": {}, 1789 | "outputs": [], 1790 | "source": [] 1791 | } 1792 | ], 1793 | "metadata": { 1794 | "kernelspec": { 1795 | "display_name": "Python 2", 1796 | "language": "python", 1797 | "name": "python2" 1798 | }, 1799 | "language_info": { 1800 | "codemirror_mode": { 1801 | "name": "ipython", 1802 | "version": 2 1803 | }, 1804 | "file_extension": ".py", 1805 | "mimetype": "text/x-python", 1806 | "name": "python", 1807 | "nbconvert_exporter": "python", 1808 | "pygments_lexer": "ipython2", 1809 | "version": "2.7.17" 1810 | } 1811 | }, 1812 | "nbformat": 4, 1813 | "nbformat_minor": 4 1814 | } 1815 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unsupervised technique to Glossary and Definition Extraction 2 | 3 |

4 | 5 |

6 | 7 | ### Code Files 8 | 1. `GPT2-DefinitionModel.ipynb` - GPT-2 model for definition generation. 9 | 2. `Data_Generator.ipynb` - Data Scraper from [GoodReads](https://www.goodreads.com/) and [GradeSaver](https://www.gradesaver.com/) 10 | 3. `Definition_Extraction.ipynb` - WordNet model for definition generation. 11 | 4. `Glossary_Extraction.ipynb` - Chinking strategy pipeline for selection of glossary terms. 12 | 13 | For more details of the project and __results__ you can access project presentation [here](https://docs.google.com/presentation/d/1QfgaVk2QzKw-Rm4MbpTtl5FqUz8wah-q0tKeVw4N_Kc/edit?usp=sharing) also read my [blog](https://prakhartechviz.blogspot.com/2020/06/automatic-glossary-and-definition-extraction.html) 14 | -------------------------------------------------------------------------------- /glossary.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prakhar21/Automatic-Glossary-Generation/19055918493484c30066edc616283f20daad0e6a/glossary.jpeg --------------------------------------------------------------------------------