1245 lines
28 KiB
Text
1245 lines
28 KiB
Text
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Remove Stop Words Using NLTK"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from nltk import word_tokenize\n",
|
||
"from nltk.corpus import stopwords"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"['arabic', 'azerbaijani', 'danish', 'dutch', 'english', 'finnish', 'french', 'german', 'greek', 'hungarian', 'indonesian', 'italian', 'kazakh', 'nepali', 'norwegian', 'portuguese', 'romanian', 'russian', 'spanish', 'swedish', 'turkish']\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(stopwords.fileids())"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"['i',\n",
|
||
" 'me',\n",
|
||
" 'my',\n",
|
||
" 'myself',\n",
|
||
" 'we',\n",
|
||
" 'our',\n",
|
||
" 'ours',\n",
|
||
" 'ourselves',\n",
|
||
" 'you',\n",
|
||
" \"you're\",\n",
|
||
" \"you've\",\n",
|
||
" \"you'll\",\n",
|
||
" \"you'd\",\n",
|
||
" 'your',\n",
|
||
" 'yours',\n",
|
||
" 'yourself',\n",
|
||
" 'yourselves',\n",
|
||
" 'he',\n",
|
||
" 'him',\n",
|
||
" 'his',\n",
|
||
" 'himself',\n",
|
||
" 'she',\n",
|
||
" \"she's\",\n",
|
||
" 'her',\n",
|
||
" 'hers',\n",
|
||
" 'herself',\n",
|
||
" 'it',\n",
|
||
" \"it's\",\n",
|
||
" 'its',\n",
|
||
" 'itself',\n",
|
||
" 'they',\n",
|
||
" 'them',\n",
|
||
" 'their',\n",
|
||
" 'theirs',\n",
|
||
" 'themselves',\n",
|
||
" 'what',\n",
|
||
" 'which',\n",
|
||
" 'who',\n",
|
||
" 'whom',\n",
|
||
" 'this',\n",
|
||
" 'that',\n",
|
||
" \"that'll\",\n",
|
||
" 'these',\n",
|
||
" 'those',\n",
|
||
" 'am',\n",
|
||
" 'is',\n",
|
||
" 'are',\n",
|
||
" 'was',\n",
|
||
" 'were',\n",
|
||
" 'be',\n",
|
||
" 'been',\n",
|
||
" 'being',\n",
|
||
" 'have',\n",
|
||
" 'has',\n",
|
||
" 'had',\n",
|
||
" 'having',\n",
|
||
" 'do',\n",
|
||
" 'does',\n",
|
||
" 'did',\n",
|
||
" 'doing',\n",
|
||
" 'a',\n",
|
||
" 'an',\n",
|
||
" 'the',\n",
|
||
" 'and',\n",
|
||
" 'but',\n",
|
||
" 'if',\n",
|
||
" 'or',\n",
|
||
" 'because',\n",
|
||
" 'as',\n",
|
||
" 'until',\n",
|
||
" 'while',\n",
|
||
" 'of',\n",
|
||
" 'at',\n",
|
||
" 'by',\n",
|
||
" 'for',\n",
|
||
" 'with',\n",
|
||
" 'about',\n",
|
||
" 'against',\n",
|
||
" 'between',\n",
|
||
" 'into',\n",
|
||
" 'through',\n",
|
||
" 'during',\n",
|
||
" 'before',\n",
|
||
" 'after',\n",
|
||
" 'above',\n",
|
||
" 'below',\n",
|
||
" 'to',\n",
|
||
" 'from',\n",
|
||
" 'up',\n",
|
||
" 'down',\n",
|
||
" 'in',\n",
|
||
" 'out',\n",
|
||
" 'on',\n",
|
||
" 'off',\n",
|
||
" 'over',\n",
|
||
" 'under',\n",
|
||
" 'again',\n",
|
||
" 'further',\n",
|
||
" 'then',\n",
|
||
" 'once',\n",
|
||
" 'here',\n",
|
||
" 'there',\n",
|
||
" 'when',\n",
|
||
" 'where',\n",
|
||
" 'why',\n",
|
||
" 'how',\n",
|
||
" 'all',\n",
|
||
" 'any',\n",
|
||
" 'both',\n",
|
||
" 'each',\n",
|
||
" 'few',\n",
|
||
" 'more',\n",
|
||
" 'most',\n",
|
||
" 'other',\n",
|
||
" 'some',\n",
|
||
" 'such',\n",
|
||
" 'no',\n",
|
||
" 'nor',\n",
|
||
" 'not',\n",
|
||
" 'only',\n",
|
||
" 'own',\n",
|
||
" 'same',\n",
|
||
" 'so',\n",
|
||
" 'than',\n",
|
||
" 'too',\n",
|
||
" 'very',\n",
|
||
" 's',\n",
|
||
" 't',\n",
|
||
" 'can',\n",
|
||
" 'will',\n",
|
||
" 'just',\n",
|
||
" 'don',\n",
|
||
" \"don't\",\n",
|
||
" 'should',\n",
|
||
" \"should've\",\n",
|
||
" 'now',\n",
|
||
" 'd',\n",
|
||
" 'll',\n",
|
||
" 'm',\n",
|
||
" 'o',\n",
|
||
" 're',\n",
|
||
" 've',\n",
|
||
" 'y',\n",
|
||
" 'ain',\n",
|
||
" 'aren',\n",
|
||
" \"aren't\",\n",
|
||
" 'couldn',\n",
|
||
" \"couldn't\",\n",
|
||
" 'didn',\n",
|
||
" \"didn't\",\n",
|
||
" 'doesn',\n",
|
||
" \"doesn't\",\n",
|
||
" 'hadn',\n",
|
||
" \"hadn't\",\n",
|
||
" 'hasn',\n",
|
||
" \"hasn't\",\n",
|
||
" 'haven',\n",
|
||
" \"haven't\",\n",
|
||
" 'isn',\n",
|
||
" \"isn't\",\n",
|
||
" 'ma',\n",
|
||
" 'mightn',\n",
|
||
" \"mightn't\",\n",
|
||
" 'mustn',\n",
|
||
" \"mustn't\",\n",
|
||
" 'needn',\n",
|
||
" \"needn't\",\n",
|
||
" 'shan',\n",
|
||
" \"shan't\",\n",
|
||
" 'shouldn',\n",
|
||
" \"shouldn't\",\n",
|
||
" 'wasn',\n",
|
||
" \"wasn't\",\n",
|
||
" 'weren',\n",
|
||
" \"weren't\",\n",
|
||
" 'won',\n",
|
||
" \"won't\",\n",
|
||
" 'wouldn',\n",
|
||
" \"wouldn't\"]"
|
||
]
|
||
},
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"stopwords.words('english')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"['إذ',\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",
|
||
" 'بماذا',\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",
|
||
" 'فيها',\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",
|
||
" 'منه',\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",
|
||
" 'يا']"
|
||
]
|
||
},
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"stopwords.words('arabic')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'A bird in hand is worth two in the bush. Good things come to those who wait. These watches cost $1500! There are other fish in the sea. The ball is in your court. Mr. Smith Goes to Washington Doogie Howser M.D.'"
|
||
]
|
||
},
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"text_array = [\"A bird in hand is worth two in the bush.\",\n",
|
||
" \"Good things come to those who wait.\",\n",
|
||
" \"These watches cost $1500! \",\n",
|
||
" \"There are other fish in the sea.\",\n",
|
||
" \"The ball is in your court.\",\n",
|
||
" \"Mr. Smith Goes to Washington \",\n",
|
||
" \"Doogie Howser M.D.\"]\n",
|
||
"\n",
|
||
"text = \" \".join(text_array)\n",
|
||
"\n",
|
||
"text"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"['A',\n",
|
||
" 'bird',\n",
|
||
" 'in',\n",
|
||
" 'hand',\n",
|
||
" 'is',\n",
|
||
" 'worth',\n",
|
||
" 'two',\n",
|
||
" 'in',\n",
|
||
" 'the',\n",
|
||
" 'bush',\n",
|
||
" '.',\n",
|
||
" 'Good',\n",
|
||
" 'things',\n",
|
||
" 'come',\n",
|
||
" 'to',\n",
|
||
" 'those',\n",
|
||
" 'who',\n",
|
||
" 'wait',\n",
|
||
" '.',\n",
|
||
" 'These',\n",
|
||
" 'watches',\n",
|
||
" 'cost',\n",
|
||
" '$',\n",
|
||
" '1500',\n",
|
||
" '!',\n",
|
||
" 'There',\n",
|
||
" 'are',\n",
|
||
" 'other',\n",
|
||
" 'fish',\n",
|
||
" 'in',\n",
|
||
" 'the',\n",
|
||
" 'sea',\n",
|
||
" '.',\n",
|
||
" 'The',\n",
|
||
" 'ball',\n",
|
||
" 'is',\n",
|
||
" 'in',\n",
|
||
" 'your',\n",
|
||
" 'court',\n",
|
||
" '.',\n",
|
||
" 'Mr.',\n",
|
||
" 'Smith',\n",
|
||
" 'Goes',\n",
|
||
" 'to',\n",
|
||
" 'Washington',\n",
|
||
" 'Doogie',\n",
|
||
" 'Howser',\n",
|
||
" 'M.D',\n",
|
||
" '.']"
|
||
]
|
||
},
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"word_tokens = word_tokenize(text)\n",
|
||
"\n",
|
||
"word_tokens"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"['A', 'bird', 'hand', 'worth', 'two', 'bush', '.', 'Good', 'things', 'come', 'wait', '.', 'These', 'watches', 'cost', '$', '1500', '!', 'There', 'fish', 'sea', '.', 'The', 'ball', 'court', '.', 'Mr.', 'Smith', 'Goes', 'Washington', 'Doogie', 'Howser', 'M.D', '.']\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"stop_words = set(stopwords.words('english'))\n",
|
||
"\n",
|
||
"filtered_words = []\n",
|
||
"\n",
|
||
"for word in word_tokens:\n",
|
||
" if word not in stop_words:\n",
|
||
" filtered_words.append(word)\n",
|
||
" \n",
|
||
"print(filtered_words)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 18,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"with open(\"./datasets/file.txt\", \"w\") as f:\n",
|
||
" for word in filtered_words:\n",
|
||
" f.write(word)\n",
|
||
" f.write(' ')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"A bird hand worth two bush . Good things come wait . These watches cost $ 1500 ! There fish sea . The ball court . Mr. Smith Goes Washington Doogie Howser M.D . \n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"with open(\"./datasets/file.txt\", \"r\") as f:\n",
|
||
" file_contents = f.read()\n",
|
||
"\n",
|
||
"print(file_contents)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 36,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"CountVectorizer(analyzer='word', binary=False, decode_error='strict',\n",
|
||
" dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',\n",
|
||
" lowercase=True, max_df=1.0, max_features=None, min_df=1,\n",
|
||
" ngram_range=(1, 1), preprocessor=None, stop_words=None,\n",
|
||
" strip_accents=None, token_pattern='(?u)\\\\b\\\\w\\\\w+\\\\b',\n",
|
||
" tokenizer=None, vocabulary=None)"
|
||
]
|
||
},
|
||
"execution_count": 36,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"from sklearn.feature_extraction.text import CountVectorizer\n",
|
||
"\n",
|
||
"count_vectorizer = CountVectorizer()\n",
|
||
"count_vectorizer.fit([file_contents])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 37,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(7, 25)"
|
||
]
|
||
},
|
||
"execution_count": 37,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"transformed_vector = count_vectorizer.transform(text_array)\n",
|
||
"\n",
|
||
"transformed_vector.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 38,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"feature_names_nltk = count_vectorizer.get_feature_names()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 39,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"{'bird': 2,\n",
|
||
" 'hand': 11,\n",
|
||
" 'worth': 24,\n",
|
||
" 'two': 20,\n",
|
||
" 'bush': 3,\n",
|
||
" 'good': 10,\n",
|
||
" 'things': 19,\n",
|
||
" 'come': 4,\n",
|
||
" 'wait': 21,\n",
|
||
" 'these': 18,\n",
|
||
" 'watches': 23,\n",
|
||
" 'cost': 5,\n",
|
||
" '1500': 0,\n",
|
||
" 'there': 17,\n",
|
||
" 'fish': 8,\n",
|
||
" 'sea': 14,\n",
|
||
" 'the': 16,\n",
|
||
" 'ball': 1,\n",
|
||
" 'court': 6,\n",
|
||
" 'mr': 13,\n",
|
||
" 'smith': 15,\n",
|
||
" 'goes': 9,\n",
|
||
" 'washington': 22,\n",
|
||
" 'doogie': 7,\n",
|
||
" 'howser': 12}"
|
||
]
|
||
},
|
||
"execution_count": 39,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer.vocabulary_"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 40,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,\n",
|
||
" 0, 0, 1],\n",
|
||
" [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,\n",
|
||
" 0, 0, 0],\n",
|
||
" [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n",
|
||
" 0, 1, 0],\n",
|
||
" [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0,\n",
|
||
" 0, 0, 0],\n",
|
||
" [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,\n",
|
||
" 0, 0, 0],\n",
|
||
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0,\n",
|
||
" 1, 0, 0],\n",
|
||
" [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
|
||
" 0, 0, 0]])"
|
||
]
|
||
},
|
||
"execution_count": 40,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"transformed_vector.toarray()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 41,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"[array(['bird', 'bush', 'hand', 'the', 'two', 'worth'], dtype='<U10'),\n",
|
||
" array(['come', 'good', 'things', 'wait'], dtype='<U10'),\n",
|
||
" array(['1500', 'cost', 'these', 'watches'], dtype='<U10'),\n",
|
||
" array(['fish', 'sea', 'the', 'there'], dtype='<U10'),\n",
|
||
" array(['ball', 'court', 'the'], dtype='<U10'),\n",
|
||
" array(['goes', 'mr', 'smith', 'washington'], dtype='<U10'),\n",
|
||
" array(['doogie', 'howser'], dtype='<U10')]"
|
||
]
|
||
},
|
||
"execution_count": 41,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer.inverse_transform(transformed_vector)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Remove Stop Words Using sklearn\n",
|
||
"\n",
|
||
"The stop_words='english' parameter is not recommended"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 44,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(7, 21)"
|
||
]
|
||
},
|
||
"execution_count": 44,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer = CountVectorizer(stop_words='english')\n",
|
||
"\n",
|
||
"transformed_vector = count_vectorizer.fit_transform(text_array)\n",
|
||
"\n",
|
||
"transformed_vector.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 45,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"feature_names_sklearn = count_vectorizer.get_feature_names()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 46,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
|
||
" [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],\n",
|
||
" [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],\n",
|
||
" [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],\n",
|
||
" [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
||
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0],\n",
|
||
" [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]],\n",
|
||
" dtype=int64)"
|
||
]
|
||
},
|
||
"execution_count": 46,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"transformed_vector.toarray()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 48,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"[array(['bush', 'worth', 'hand', 'bird'], dtype='<U10'),\n",
|
||
" array(['wait', 'come', 'things', 'good'], dtype='<U10'),\n",
|
||
" array(['1500', 'cost', 'watches'], dtype='<U10'),\n",
|
||
" array(['sea', 'fish'], dtype='<U10'),\n",
|
||
" array(['court', 'ball'], dtype='<U10'),\n",
|
||
" array(['washington', 'goes', 'smith', 'mr'], dtype='<U10'),\n",
|
||
" array(['howser', 'doogie'], dtype='<U10')]"
|
||
]
|
||
},
|
||
"execution_count": 48,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer.inverse_transform(transformed_vector)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Set difference of both"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 90,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"def set_diff(first, second):\n",
|
||
" second = set(second)\n",
|
||
" return [item for item in first if item not in second]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 91,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"[]"
|
||
]
|
||
},
|
||
"execution_count": 91,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"set_diff(feature_names_sklearn, feature_names_nltk)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 92,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"['the', 'there', 'these', 'two']"
|
||
]
|
||
},
|
||
"execution_count": 92,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"set_diff(feature_names_nltk, feature_names_sklearn)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Filtering words based on frequency"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 73,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from sklearn.datasets import fetch_20newsgroups\n",
|
||
"\n",
|
||
"newsgroups = fetch_20newsgroups(subset='train')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 97,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"dict_keys(['data', 'filenames', 'target_names', 'target', 'DESCR'])"
|
||
]
|
||
},
|
||
"execution_count": 97,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"newsgroups.keys()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 77,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"From: lerxst@wam.umd.edu (where's my thing)\n",
|
||
"Subject: WHAT car is this!?\n",
|
||
"Nntp-Posting-Host: rac3.wam.umd.edu\n",
|
||
"Organization: University of Maryland, College Park\n",
|
||
"Lines: 15\n",
|
||
"\n",
|
||
" I was wondering if anyone out there could enlighten me on this car I saw\n",
|
||
"the other day. It was a 2-door sports car, looked to be from the late 60s/\n",
|
||
"early 70s. It was called a Bricklin. The doors were really small. In addition,\n",
|
||
"the front bumper was separate from the rest of the body. This is \n",
|
||
"all I know. If anyone can tellme a model name, engine specs, years\n",
|
||
"of production, where this car is made, history, or whatever info you\n",
|
||
"have on this funky looking car, please e-mail.\n",
|
||
"\n",
|
||
"Thanks,\n",
|
||
"- IL\n",
|
||
" ---- brought to you by your neighborhood Lerxst ----\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(newsgroups.data[0])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 98,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"['alt.atheism',\n",
|
||
" 'comp.graphics',\n",
|
||
" 'comp.os.ms-windows.misc',\n",
|
||
" 'comp.sys.ibm.pc.hardware',\n",
|
||
" 'comp.sys.mac.hardware',\n",
|
||
" 'comp.windows.x',\n",
|
||
" 'misc.forsale',\n",
|
||
" 'rec.autos',\n",
|
||
" 'rec.motorcycles',\n",
|
||
" 'rec.sport.baseball',\n",
|
||
" 'rec.sport.hockey',\n",
|
||
" 'sci.crypt',\n",
|
||
" 'sci.electronics',\n",
|
||
" 'sci.med',\n",
|
||
" 'sci.space',\n",
|
||
" 'soc.religion.christian',\n",
|
||
" 'talk.politics.guns',\n",
|
||
" 'talk.politics.mideast',\n",
|
||
" 'talk.politics.misc',\n",
|
||
" 'talk.religion.misc']"
|
||
]
|
||
},
|
||
"execution_count": 98,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"newsgroups.target_names"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html#sklearn-feature-extraction-text-countvectorizer"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 78,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(11314, 130107)"
|
||
]
|
||
},
|
||
"execution_count": 78,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer = CountVectorizer()\n",
|
||
"\n",
|
||
"transformed_vector = count_vectorizer.fit_transform(newsgroups.data)\n",
|
||
"\n",
|
||
"transformed_vector.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 84,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(11314, 130094)"
|
||
]
|
||
},
|
||
"execution_count": 84,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer = CountVectorizer(min_df=0.0, max_df=0.7)\n",
|
||
"\n",
|
||
"transformed_vector = count_vectorizer.fit_transform(newsgroups.data)\n",
|
||
"\n",
|
||
"transformed_vector.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 83,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(11314, 2155)"
|
||
]
|
||
},
|
||
"execution_count": 83,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer = CountVectorizer(min_df=0.01, max_df=0.7)\n",
|
||
"\n",
|
||
"transformed_vector = count_vectorizer.fit_transform(newsgroups.data)\n",
|
||
"\n",
|
||
"transformed_vector.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 86,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(11314, 127701)"
|
||
]
|
||
},
|
||
"execution_count": 86,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer = CountVectorizer(min_df=0, max_df=100)\n",
|
||
"\n",
|
||
"transformed_vector = count_vectorizer.fit_transform(newsgroups.data)\n",
|
||
"\n",
|
||
"transformed_vector.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 89,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(11314, 54030)"
|
||
]
|
||
},
|
||
"execution_count": 89,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"count_vectorizer = CountVectorizer(min_df=2, max_df=100)\n",
|
||
"\n",
|
||
"transformed_vector = count_vectorizer.fit_transform(newsgroups.data)\n",
|
||
"\n",
|
||
"transformed_vector.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.7.1"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|