{ "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=, 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='