{ "cells": [ { "cell_type": "markdown", "id": "f0ce57f4-5984-43e6-b3bb-60f2d9251d75", "metadata": {}, "source": [ "# Implementation of Chatbot using Natural Language Processing(NLP)" ] }, { "cell_type": "markdown", "id": "3c062c6e-0773-4af8-bcb9-aa33f7afeb25", "metadata": {}, "source": [ "### Importing necessary libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "23417a10-2691-4887-b02e-2448f6200cd1", "metadata": {}, "outputs": [], "source": [ "import nltk\n", "import random\n", "import os\n", "import ssl\n", "import streamlit as st\n", "from sklearn.svm import SVC\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "from sklearn.linear_model import LogisticRegression" ] }, { "cell_type": "markdown", "id": "1dc3d789-6567-4266-bd33-005aed1d4e93", "metadata": {}, "source": [ "### Bypass SSL verification for NLTK downloads" ] }, { "cell_type": "code", "execution_count": 2, "id": "298e3201-b127-4c68-b041-2a22ace29340", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to\n", "[nltk_data] C:\\Users\\LENOVO\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ssl._create_default_https_context = ssl._create_unverified_context\n", "nltk.data.path.append(os.path.abspath('nltk_data'))\n", "nltk.download('punkt')" ] }, { "cell_type": "markdown", "id": "62d1be3d-37b1-4ab3-a527-9185e183ac98", "metadata": {}, "source": [ "### Intent dataset" ] }, { "cell_type": "code", "execution_count": 3, "id": "a4c238c5-fe25-4802-9328-70ec7b064044", "metadata": {}, "outputs": [], "source": [ "intents = [\n", " {\n", " \"atag\": \"greeting\", \"patterns\": [\"Hi\", \"Hello\", \"Hey\", \"What's up\", \"How are you\"],\n", " \"responses\": [\"Hi there!\", \"Hello!\", \"Hey!\", \"Nothing much.\", \"I'm fine, thank you.\"]\n", " },\n", " {\n", " \"tag\": \"goodbye\", \"patterns\": [\"Bye\", \"See you later\", \"Goodbye\", \"Take care\"],\n", " \"responses\": [\"Goodbye!\", \"See you later!\", \"Take care!\"]\n", " },\n", " {\n", " \"tag\": \"thanks\", \"patterns\": [\"Thank you\", \"Thanks\", \"Thanks a lot\", \"I appreciate it\"],\n", " \"responses\": [\"You're welcome!\", \"No problem!\", \"Glad I could help!\"]\n", " },\n", " {\n", " \"tag\": \"about\", \"patterns\": [\"What can you do\", \"Who are you\", \"What are you\", \"What is your purpose\"],\n", " \"responses\": [\"I am a chatbot.\", \"My purpose is to assist you.\", \"I can answer questions and provide assistance.\"]\n", " },\n", " {\n", " \"tag\": \"help\", \"patterns\": [\"Help\", \"I need help\", \"Can you help me\", \"What should I do\"],\n", " \"responses\": [\"Sure, what do you need help with?\", \"I'm here to help. What's the problem?\", \"How can I assist you?\"]\n", " },\n", " {\n", " \"tag\": \"age\", \"patterns\": [\"How old are you\", \"What's your age\"],\n", " \"responses\": [\"I don't have an age. I'm a chatbot.\", \"I was just born in the digital world.\", \"Age is just a number for me.\"]\n", " },\n", " {\n", " \"tag\": \"weather\", \"patterns\": [\"What's the weather like\", \"How's the weather today\"],\n", " \"responses\": [\"I'm sorry, I cannot provide real-time weather information.\", \"You can check the weather on a weather app or website.\"]\n", " },\n", " {\n", " \"tag\": \"budget\", \"patterns\": [\"How can I make a budget\", \"What's a good budgeting strategy\", \"How do I create a budget\"],\n", " \"responses\": [\"Start by tracking your income and expenses. Allocate money for essentials, savings, and discretionary spending.\",\n", " \"A good strategy is the 50/30/20 rule: 50% for needs, 30% for wants, and 20% for savings and debt.\",\n", " \"Set financial goals, monitor expenses, and adjust your budget as needed.\"]\n", " },\n", " {\n", " \"tag\": \"credit_score\", \"patterns\": [\"What is a credit score\", \"How do I check my credit score\", \"How can I improve my credit score\"],\n", " \"responses\": [\"A credit score reflects your creditworthiness and is used by lenders to assess loans.\",\n", " \"Check your credit score on platforms like Credit Karma or Credit Sesame.\",\n", " \"Improve your credit score by paying bills on time, reducing debt, and maintaining good credit utilization.\"]\n", " },\n", " {\n", " \"tag\": \"food\", \"patterns\": [\"What should I eat\", \"Suggest me some food\", \"I am hungry\"],\n", " \"responses\": [\"You could try a healthy salad, a sandwich, or some pasta!\", \"How about some homemade pizza?\", \"A nice bowl of soup and bread would be great!\"]\n", " },\n", " {\n", " \"tag\": \"exercise\", \"patterns\": [\"What exercises should I do\", \"How to stay fit\", \"Suggest a workout\"],\n", " \"responses\": [\"Try a mix of cardio and strength training!\", \"A daily walk and some stretching would help.\", \"Yoga is great for both mind and body!\"]\n", " },\n", " {\n", " \"tag\": \"movies\", \"patterns\": [\"Suggest me a movie\", \"What are some good movies\", \"I want to watch a film\"],\n", " \"responses\": [\"How about an action thriller?\", \"A comedy might lift your mood!\", \"Sci-fi movies are always exciting!\"]\n", " },\n", " {\n", " \"tag\": \"music\", \"patterns\": [\"Suggest me some music\", \"What should I listen to\", \"Recommend a song\"],\n", " \"responses\": [\"Try some relaxing jazz or lo-fi music!\", \"Pop songs are always fun!\", \"How about some classic rock?\"]\n", " }\n", "]" ] }, { "cell_type": "markdown", "id": "3ece2689-d741-48ea-9659-be9cf20e3033", "metadata": {}, "source": [ "### Create the vectorizer and classifier" ] }, { "cell_type": "code", "execution_count": 4, "id": "18d6ccb9-639c-4053-981a-69e2fa7cccca", "metadata": {}, "outputs": [], "source": [ "vectorizer = TfidfVectorizer()\n", "clf = SVC(kernel='linear', random_state=0)\n", "#clf = LogisticRegression(random_state=0, max_iter=10000)" ] }, { "cell_type": "markdown", "id": "09e70e89-e826-432a-9c22-b4097b5ac07f", "metadata": {}, "source": [ "### Preprocess the data" ] }, { "cell_type": "code", "execution_count": 5, "id": "aca02fbf-201b-47a6-b46c-ca9e0d4e9334", "metadata": {}, "outputs": [], "source": [ "tags = []\n", "patterns = []\n", "for intent in intents:\n", " for pattern in intent['patterns']:\n", " tags.append(intent['tag'])\n", " patterns.append(pattern)" ] }, { "cell_type": "markdown", "id": "e33f4599-2a6b-4b9b-805a-7875b1840eb4", "metadata": {}, "source": [ "### Training the model" ] }, { "cell_type": "code", "execution_count": 6, "id": "6d11f50c-7062-46e3-89d2-f6e252e5cf12", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
SVC(kernel='linear', random_state=0)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
SVC(kernel='linear', random_state=0)