{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "hVsVzJGs-B9b",
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#Sample of nltk usage by ChatGPT\n",
    "\n",
    "import nltk\n",
    "from nltk.tokenize import word_tokenize\n",
    "from nltk.corpus import stopwords\n",
    "from nltk.stem import PorterStemmer, WordNetLemmatizer\n",
    "from nltk.util import ngrams\n",
    "from string import punctuation\n",
    "\n",
    "\n",
    "# Download necessary NLTK data\n",
    "nltk.download('punkt_tab')\n",
    "nltk.download('stopwords')\n",
    "nltk.download('wordnet')\n",
    "nltk.download('averaged_perceptron_tagger')\n",
    "nltk.download('averaged_perceptron_tagger_eng')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Lh1GSEyS-Dxs"
   },
   "outputs": [],
   "source": [
    "# Step 1: Define the text to be processed\n",
    "text = \"Natural Language Processing, or NLP, is a fascinating field of Artificial Intelligence!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "1vxsmwDk-HH3"
   },
   "outputs": [],
   "source": [
    "# Step 2: Tokenization - Split the text into tokens (words)\n",
    "tokens = word_tokenize(text)\n",
    "print(\"Tokens:\", tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ebdQumFF-LpJ"
   },
   "outputs": [],
   "source": [
    "# Step 3: Lowercasing - Convert all tokens to lowercase\n",
    "tokens = [token.lower() for token in tokens]\n",
    "print(\"\\nLowercased Tokens:\", tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "I94R1K8t-Pjh"
   },
   "outputs": [],
   "source": [
    "# Step 4: Stopword Removal - Remove common English stopwords\n",
    "stop_words = set(stopwords.words('english'))\n",
    "filtered_tokens = [token for token in tokens if token not in stop_words]\n",
    "print(\"\\nTokens after Stopword Removal:\", filtered_tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "gq3cMiZi-Rif"
   },
   "outputs": [],
   "source": [
    "# Step 5: Punctuation Removal - Remove punctuation marks\n",
    "tokens_no_punct = [token for token in filtered_tokens if token not in punctuation]\n",
    "print(\"\\nTokens after Punctuation Removal:\", tokens_no_punct)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "TyxhXLNr-SXt"
   },
   "outputs": [],
   "source": [
    "# Step 6a: Stemming - Apply stemming using PorterStemmer\n",
    "stemmer = PorterStemmer()\n",
    "stemmed_tokens = [stemmer.stem(token) for token in tokens_no_punct]\n",
    "print(\"\\nStemmed Tokens:\", stemmed_tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "eyP3THTP-VO0"
   },
   "outputs": [],
   "source": [
    "# Step 6b: Lemmatization - Apply lemmatization using WordNetLemmatizer\n",
    "lemmatizer = WordNetLemmatizer()\n",
    "lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens_no_punct]\n",
    "print(\"\\nLemmatized Tokens:\", lemmatized_tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "yWLZ9Jjo-YPT"
   },
   "outputs": [],
   "source": [
    "# Step 7: Part-of-Speech (POS) Tagging - Tag each token with its POS\n",
    "pos_tags = nltk.pos_tag(tokens_no_punct)\n",
    "print(\"\\nPart-of-Speech Tags:\", pos_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "7WqYl2eE-uWA"
   },
   "outputs": [],
   "source": [
    "# Step 8: N-gram Generation\n",
    "# Generate bigrams (2-grams)\n",
    "bigrams = list(ngrams(tokens_no_punct, 2))\n",
    "print(\"\\nBigrams (2-grams):\", bigrams)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ZHbECtQq-yeb"
   },
   "outputs": [],
   "source": [
    "# Generate trigrams (3-grams)\n",
    "trigrams = list(ngrams(tokens_no_punct, 3))\n",
    "print(\"\\nTrigrams (3-grams):\", trigrams)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "dFWNvsLj-0MK"
   },
   "outputs": [],
   "source": [
    "# Generate n-grams (for n = 1 to 3)\n",
    "for n in range(1, 4):\n",
    "    n_grams = list(ngrams(tokens_no_punct, n))\n",
    "    print(f\"\\n{n}-grams:\", n_grams)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
