{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Lemmatizing Words Using WordNet\n", "* Part-of-speech constants:\n", "* ADJ: a\n", "* ADV: r\n", "* NOUN: n\n", "* VERB: v" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "import nltk\n", "\n", "from nltk.stem import *\n", "\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stemming words" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "definit\n" ] } ], "source": [ "from nltk.stem import PorterStemmer \n", "\n", "stemmer = PorterStemmer() \n", "print(stemmer.stem('definitions'))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package wordnet to\n", "[nltk_data] /Users/loonycorn/nltk_data...\n", "[nltk_data] Package wordnet is already up-to-date!\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nltk.download('wordnet')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lemmatizing Words" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "definition\n" ] } ], "source": [ "from nltk.stem import WordNetLemmatizer\n", "\n", "wnl = WordNetLemmatizer()\n", "print(wnl.lemmatize('definitions'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lemmatizing words by specifying parts-of-speech" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Adjective: running\n", "Adverb: running\n", "Noun: running\n", "Verb: run\n" ] } ], "source": [ "print('Adjective: ', wnl.lemmatize('running', pos='a'))\n", "print('Adverb: ', wnl.lemmatize('running', pos='r'))\n", "print('Noun: ', wnl.lemmatize('running', pos='n'))\n", "print('Verb: ', wnl.lemmatize('running', pos='v'))" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "input_tokens = ['dictionaries', 'dictionary', \n", " 'hushed', 'hush', 'hushing',\n", " 'functional', 'functionally',\n", " 'lying', 'lied', 'lies',\n", " 'flawed', 'flaws', 'flawless', \n", " 'friendship', 'friendships', 'friendly', 'friendless', \n", " 'definitions', 'definition', 'definitely', \n", " 'the', 'these', 'those',\n", " 'motivational', 'motivate', 'motivating']" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [], "source": [ "ss = SnowballStemmer('english')\n", "\n", "ss_stemmed_tokens = []\n", "for token in input_tokens:\n", " ss_stemmed_tokens.append(ss.stem(token))" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "wnl_lemmatized_tokens = []\n", "for token in input_tokens:\n", " wnl_lemmatized_tokens.append(wnl.lemmatize(token, pos='v'))" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | words | \n", "Snowball Stemmer | \n", "WordNet Lemmatizer | \n", "
|---|---|---|---|
| 0 | \n", "dictionaries | \n", "dictionari | \n", "dictionaries | \n", "
| 1 | \n", "dictionary | \n", "dictionari | \n", "dictionary | \n", "
| 2 | \n", "hushed | \n", "hush | \n", "hush | \n", "
| 3 | \n", "hush | \n", "hush | \n", "hush | \n", "
| 4 | \n", "hushing | \n", "hush | \n", "hush | \n", "
| 5 | \n", "functional | \n", "function | \n", "functional | \n", "
| 6 | \n", "functionally | \n", "function | \n", "functionally | \n", "
| 7 | \n", "lying | \n", "lie | \n", "lie | \n", "
| 8 | \n", "lied | \n", "lie | \n", "lie | \n", "
| 9 | \n", "lies | \n", "lie | \n", "lie | \n", "
| 10 | \n", "flawed | \n", "flaw | \n", "flaw | \n", "
| 11 | \n", "flaws | \n", "flaw | \n", "flaw | \n", "
| 12 | \n", "flawless | \n", "flawless | \n", "flawless | \n", "
| 13 | \n", "friendship | \n", "friendship | \n", "friendship | \n", "
| 14 | \n", "friendships | \n", "friendship | \n", "friendships | \n", "
| 15 | \n", "friendly | \n", "friend | \n", "friendly | \n", "
| 16 | \n", "friendless | \n", "friendless | \n", "friendless | \n", "
| 17 | \n", "definitions | \n", "definit | \n", "definitions | \n", "
| 18 | \n", "definition | \n", "definit | \n", "definition | \n", "
| 19 | \n", "definitely | \n", "definit | \n", "definitely | \n", "
| 20 | \n", "the | \n", "the | \n", "the | \n", "
| 21 | \n", "these | \n", "these | \n", "these | \n", "
| 22 | \n", "those | \n", "those | \n", "those | \n", "
| 23 | \n", "motivational | \n", "motiv | \n", "motivational | \n", "
| 24 | \n", "motivate | \n", "motiv | \n", "motivate | \n", "
| 25 | \n", "motivating | \n", "motiv | \n", "motivate | \n", "