{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 20NG (Twenty Newsgroups). Preprocessing\n", "\n", "Here goes an example of data preprocessing and converting it to TopicNet's Dataset format.\n", "\n", "* Example of a toy dataset: [test_dataset.csv](https://github.com/machine-intelligence-laboratory/TopicNet/blob/master/topicnet/tests/test_data/test_dataset.csv)\n", "* Dataset source file (with some explanations in docstring): [dataset.py](https://github.com/machine-intelligence-laboratory/TopicNet/blob/master/topicnet/cooking_machine/dataset.py)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Contents\n", "\n", "* [Loading data](#data-loading)\n", "* [Preparing data](#data-preparation)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import re\n", "import shutil\n", "import string\n", "\n", "from collections import Counter\n", "from glob import glob\n", "\n", "from sklearn import datasets\n", "from sklearn.datasets import fetch_20newsgroups" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nltk\n", "\n", "from nltk.collocations import (\n", " BigramAssocMeasures,\n", " BigramCollocationFinder,\n", ")\n", "from nltk.corpus import (\n", " stopwords,\n", " wordnet,\n", ")\n", "from nltk.stem import WordNetLemmatizer" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "from matplotlib import cm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading data\n", "\n", "
\n", " | raw_text | \n", "filenames | \n", "target | \n", "id | \n", "tokenized | \n", "lemmatized | \n", "bigram | \n", "vw_text | \n", "
---|---|---|---|---|---|---|---|---|
0 | \n", "I was wondering if anyone out there could enli... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "7 | \n", "rec_autos_102994 | \n", "[(was, VBD), (wondering, VBG), (if, IN), (anyo... | \n", "[wonder, anyone, could, enlighten, car, saw, d... | \n", "[wonder_anyone, anyone_could, sport_car, car_l... | \n", "rec_autos_102994 |@lemmatized wonder:1 anyone:... | \n", "
1 | \n", "A fair number of brave souls who upgraded thei... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "4 | \n", "comp_sys_mac_hardware_51861 | \n", "[(fair, JJ), (number, NN), (of, IN), (brave, J... | \n", "[fair, number, brave, soul, upgrade, si, clock... | \n", "[clock_oscillator, please_send, top_speed, hea... | \n", "comp_sys_mac_hardware_51861 |@lemmatized fair:... | \n", "
2 | \n", "well folks, my mac plus finally gave up the gh... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "4 | \n", "comp_sys_mac_hardware_51879 | \n", "[(well, RB), (folks, NNS), (my, PRP$), (mac, J... | \n", "[well, folk, mac, plus, finally, give, ghost, ... | \n", "[mac_plus, life_way, way_back, market_new, new... | \n", "comp_sys_mac_hardware_51879 |@lemmatized well:... | \n", "
3 | \n", "\\nDo you have Weitek's address/phone number? ... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "1 | \n", "comp_graphics_38242 | \n", "[(do, VBP), (you, PRP), (have, VB), (weitek, V... | \n", "[weitek, address, phone, number, like, get, in... | \n", "[address_phone, phone_number, number_like, lik... | \n", "comp_graphics_38242 |@lemmatized weitek:1 addr... | \n", "
4 | \n", "From article <C5owCB.n3p@world.std.com>, by to... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "14 | \n", "sci_space_60880 | \n", "[(from, IN), (article, NN), (by, IN), (tom, NN... | \n", "[article, tom, baker, understanding, expected,... | \n", "[system_software, thing_check, introduce_new, ... | \n", "sci_space_60880 |@lemmatized article:1 tom:1 b... | \n", "
\n", " | raw_text | \n", "filenames | \n", "target | \n", "id | \n", "tokenized | \n", "lemmatized | \n", "bigram | \n", "vw_text | \n", "
---|---|---|---|---|---|---|---|---|
0 | \n", "I am a little confused on all of the models of... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "7 | \n", "rec.autos.103343 | \n", "[(am, VBP), (little, JJ), (confused, VBN), (on... | \n", "[little, confuse, model, bonnevilles, hear, le... | \n", "[could_someone, someone_tell, tell_difference,... | \n", "rec.autos.103343 |@lemmatized little:1 confuse... | \n", "
1 | \n", "I'm not familiar at all with the format of the... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "5 | \n", "comp.windows.x.67445 | \n", "[(not, RB), (familiar, JJ), (at, IN), (all, DT... | \n", "[familiar, format, face, thingies, see, folk, ... | \n", "[get_see, make_one, one_get, seem_find, could_... | \n", "comp.windows.x.67445 |@lemmatized familiar:1 f... | \n", "
2 | \n", "\\nIn a word, yes.\\n | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "0 | \n", "alt.atheism.53603 | \n", "[(in, IN), (word, NN), (yes, NN)] | \n", "[word, yes] | \n", "[] | \n", "alt.atheism.53603 |@lemmatized word:1 yes:1 |... | \n", "
3 | \n", "\\nThey were attacking the Iraqis to drive them... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "17 | \n", "talk.politics.mideast.77355 | \n", "[(they, PRP), (were, VBD), (attacking, VBG), (... | \n", "[attack, iraqi, drive, kuwait, country, whose,... | \n", "[think_u, saudi_arabia, much_anything, saudi_a... | \n", "talk.politics.mideast.77355 |@lemmatized attac... | \n", "
4 | \n", "\\nI've just spent two solid months arguing tha... | \n", "/home/bulatov/scikit_learn_data/20news_home/20... | \n", "19 | \n", "talk.religion.misc.84194 | \n", "[(ve, NN), (just, RB), (spent, VBN), (two, CD)... | \n", "[spend, two, solid, month, argue, thing, objec... | \n", "[moral_system] | \n", "talk.religion.misc.84194 |@lemmatized spend:1 ... | \n", "