{"id":3972,"date":"2023-11-04T23:13:58","date_gmt":"2023-11-04T23:13:58","guid":{"rendered":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/"},"modified":"2023-11-05T05:48:25","modified_gmt":"2023-11-05T05:48:25","slug":"how-to-build-a-chatbot-with-python","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/","title":{"rendered":"How to Build a Chatbot with Python"},"content":{"rendered":"

Chatbots are becoming increasingly popular in the tech industry. With the advent of Natural Language Processing (NLP), a chatbot can intelligently understand and interpret the language used in a conversation with a human. Python is one of the best programming languages that can be used to create a chatbot because of its simplicity and readability.<\/p>\n

In this tutorial, we will build a simple chatbot using Python. We will train our chatbot on a small dataset and use a pre-built NLP library called NLTK (Natural Language Toolkit) to process the text input and generate responses. We will divide the tutorial into the following sections:<\/p>\n

    \n
  1. Setting up the Environment<\/li>\n
  2. Installing Required Libraries<\/li>\n
  3. Defining Functions for Preprocessing and Training Data<\/li>\n
  4. Writing the Code for our Chatbot<\/li>\n<\/ol>\n

    Setting up the Environment<\/h2>\n

    Before we start building the chatbot, we need to set up our environment. We will be using Python 3.6+ for this tutorial. If you do not have Python installed on your system already, you can download the latest version from the official website.<\/p>\n

    Create a new project directory where the chatbot files will reside. Open a command prompt (Windows) or a terminal (macOS\/Linux), navigate to the project directory, and create a virtual environment. We will use the command python -m venv venv<\/code> to create a virtual environment called venv<\/code>. Once the virtual environment has been created, activate it using the following command:<\/p>\n

    # On Windows\nvenvScriptsactivate.bat\n\n# On macOS\/Linux\nsource venv\/bin\/activate\n<\/code><\/pre>\n

    Installing Required Libraries<\/h2>\n

    Our chatbot will depend on several libraries, including NLTK, numpy, and tensorflow. Install these libraries using the following command:<\/p>\n

    pip install nltk numpy tensorflow\n<\/code><\/pre>\n

    Defining Functions for Preprocessing and Training Data<\/h2>\n

    In this step, we will define some helper functions to preprocess our data and train our model.<\/p>\n

    import nltk\nfrom nltk.stem import WordNetLemmatizer\nimport numpy as np\n\nlemmatizer = WordNetLemmatizer()\n\ndef preprocess(sentence):\n    \"\"\"\n    Tokenize and lemmatize the sentence.\n    \"\"\"\n    words = nltk.word_tokenize(sentence)\n    words = [lemmatizer.lemmatize(word.lower()) for word in words]\n    return words\n\ndef bag_of_words(sentence, words):\n    \"\"\"\n    Create a bag of words vector for the sentence.\n    \"\"\"\n    sentence_words = preprocess(sentence)\n    bag = np.zeros(len(words), dtype=np.float32)\n    for idx, word in enumerate(words):\n        if word in sentence_words:\n            bag[idx] = 1.0\n    return bag\n<\/code><\/pre>\n

    The preprocess(sentence)<\/code> function tokenizes and lemmatizes a sentence. Tokenization involves splitting the sentence into individual words. Lemmatization involves converting words to their base forms, e.g., “running” becomes “run” and “mice” becomes “mouse”. This process reduces the number of unique words in our dataset and helps to remove any discrepancies in the same words with different tenses, plurals, etc.<\/p>\n

    The bag_of_words(sentence, words)<\/code> function takes as input a sentence and a list of words. It creates a vector of length len(words)<\/code>, where each element is set to 1.0<\/code> if the corresponding word is present in the sentence and 0.0<\/code> otherwise. This function converts a sentence into a numerical input that can be used by our model.<\/p>\n

    def create_training_data(data):\n    \"\"\"\n    Create training data from the dataset.\n    \"\"\"\n    corpus = []\n    classes = []\n    words = set()\n    for intent in data[\"intents\"]:\n        for pattern in intent[\"patterns\"]:\n            words.update(preprocess(pattern))\n            corpus.append((pattern, intent[\"tag\"]))\n        classes.append(intent[\"tag\"])\n\n    # Create a list of unique words\n    words = sorted(list(words))\n\n    x_train = []\n    y_train = []\n    for (pattern, tag) in corpus:\n        # Create a bag of words vector for each pattern\n        bag = bag_of_words(pattern, words)\n        x_train.append(bag)\n\n        # Create a one-hot encoded vector for each tag\n        label = classes.index(tag)\n        label_vec = np.zeros(len(classes), dtype=np.float32)\n        label_vec[label] = 1.0\n        y_train.append(label_vec)\n\n    return x_train, y_train, words, classes\n<\/code><\/pre>\n

    The create_training_data(data)<\/code> function takes as input a dataset containing tags and associated patterns. It preprocesses the patterns, converting them into a list of unique words and creating a bag of words vector for each pattern. It also creates a one-hot encoded vector for each tag. The function returns the training data (input and output), a list of unique words, and a list of classes (i.e., tags).<\/p>\n

    Writing the Code for our Chatbot<\/h2>\n

    We can now start writing the code for our chatbot. Here is the complete chatbot.py<\/code> code:<\/p>\n

    import random\nimport json\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.optimizers import SGD\n\nfrom preprocessing import create_training_data\n\nwith open(\"intents.json\", \"r\") as f:\n    data = json.load(f)\n\nx_train, y_train, words, classes = create_training_data(data)\n\n# Create a neural network\nmodel = Sequential()\nmodel.add(Dense(128, input_shape=(len(words),), activation=\"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(64, activation=\"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(len(classes), activation=\"softmax\"))\n\n# Compile the model\nsgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=[\"accuracy\"])\n\n# Train the model\nmodel.fit(np.array(x_train), np.array(y_train), epochs=200, batch_size=5, verbose=1)\n\n# Save the model\nmodel.save(\"chatbot_model.h5\")\nprint(f\"Model saved to chatbot_model.h5\")\n\ndef predict(sentence):\n    \"\"\"\n    Predict the tag associated with a sentence.\n    \"\"\"\n    bag = bag_of_words(sentence, words)\n    res = model.predict(np.array([bag]))[0]\n    idx = np.argmax(res)\n    tag = classes[idx]\n    if res[idx] < 0.7:\n        return \"I'm sorry, I don't understand.\"\n    else:\n        for intent in data[\"intents\"]:\n            if intent[\"tag\"] == tag:\n                return random.choice(intent[\"responses\"])\n<\/code><\/pre>\n

    The first step is to import the required libraries and load the training data using the create_training_data(data)<\/code> function from the previous section. We then create and compile a neural network using the Keras API from tensorflow. The network consists of an input layer of size len(words)<\/code>, two hidden layers with 128 and 64 neurons respectively, a dropout layer with a rate of 0.5 to prevent overfitting, and an output layer with size len(classes)<\/code>.<\/p>\n

    We then train the model using the fit()<\/code> method, passing in our training data and setting epochs=200<\/code> and batch_size=5<\/code>. We save the model to a file called chatbot_model.h5<\/code>.<\/p>\n

    Finally, we define a predict(sentence)<\/code> function that takes as input a sentence and predicts the tag associated with the sentence using our trained model. If the confidence of the prediction is less than 0.7, the function returns an “I’m sorry, I don’t understand” message. Otherwise, it returns a random response from the list of responses associated with the predicted tag.<\/p>\n

    Conclusion<\/h2>\n

    In this tutorial, we have built a simple chatbot using Python and the NLTK library. We used a neural network to classify the input message and generate an appropriate response. This chatbot can be further improved by adding more training data and fine-tuning the hyperparameters of the neural network. Chatbots have the potential to revolutionize customer service and support in many industries and Python is an excellent tool for building them.<\/p>\n","protected":false},"excerpt":{"rendered":"

    Chatbots are becoming increasingly popular in the tech industry. With the advent of Natural Language Processing (NLP), a chatbot can intelligently understand and interpret the language used in a conversation with a human. Python is one of the best programming languages that can be used to create a chatbot because Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[373,39,631,20,76,119,632,337,75],"yoast_head":"\nHow to Build a Chatbot with Python - Pantherax Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Build a Chatbot with Python\" \/>\n<meta property=\"og:description\" content=\"Chatbots are becoming increasingly popular in the tech industry. With the advent of Natural Language Processing (NLP), a chatbot can intelligently understand and interpret the language used in a conversation with a human. Python is one of the best programming languages that can be used to create a chatbot because Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:13:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:48:25+00:00\" \/>\n<meta name=\"author\" content=\"Panther\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Panther\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t \"@context\": \"https:\/\/schema.org\",\n\t \"@graph\": [\n\t {\n\t \"@type\": \"Article\",\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"How to Build a Chatbot with Python\",\n\t \"datePublished\": \"2023-11-04T23:13:58+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:25+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/\"\n\t },\n\t \"wordCount\": 696,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"AI development\\\"\",\n\t \"\\\"Artificial Intelligence\\\"\",\n\t \"\\\"build chatbot\\\"\",\n\t \"\\\"chatbot development\\\"\",\n\t \"\\\"chatbot\\\"\",\n\t \"\\\"programming\\\"\",\n\t \"\\\"Python libraries\\\"\",\n\t \"\\\"Python programming\\\"\",\n\t \"\\\"Python\\\"\"\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"WebPage\",\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/\",\n\t \"url\": \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/\",\n\t \"name\": \"How to Build a Chatbot with Python - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:13:58+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:25+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/#breadcrumb\"\n\t },\n\t \"inLanguage\": \"en-US\",\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"ReadAction\",\n\t \"target\": [\n\t \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/#breadcrumb\",\n\t \"itemListElement\": [\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 1,\n\t \"name\": \"Home\",\n\t \"item\": \"http:\/\/localhost:10003\/\"\n\t },\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 2,\n\t \"name\": \"How to Build a Chatbot with Python\"\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"WebSite\",\n\t \"@id\": \"http:\/\/localhost:10003\/#website\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"description\": \"\",\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"SearchAction\",\n\t \"target\": {\n\t \"@type\": \"EntryPoint\",\n\t \"urlTemplate\": \"http:\/\/localhost:10003\/?s={search_term_string}\"\n\t },\n\t \"query-input\": \"required name=search_term_string\"\n\t }\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"Organization\",\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"logo\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\",\n\t \"url\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"contentUrl\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"width\": 1024,\n\t \"height\": 1024,\n\t \"caption\": \"Pantherax Blogs\"\n\t },\n\t \"image\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\"\n\t }\n\t },\n\t {\n\t \"@type\": \"Person\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\",\n\t \"name\": \"Panther\",\n\t \"image\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/image\/\",\n\t \"url\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"contentUrl\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"caption\": \"Panther\"\n\t },\n\t \"sameAs\": [\n\t \"http:\/\/localhost:10003\"\n\t ],\n\t \"url\": \"http:\/\/localhost:10003\/author\/pepethefrog\/\"\n\t }\n\t ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Build a Chatbot with Python - Pantherax Blogs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Build a Chatbot with Python","og_description":"Chatbots are becoming increasingly popular in the tech industry. With the advent of Natural Language Processing (NLP), a chatbot can intelligently understand and interpret the language used in a conversation with a human. Python is one of the best programming languages that can be used to create a chatbot because Continue Reading","og_url":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:13:58+00:00","article_modified_time":"2023-11-05T05:48:25+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"How to Build a Chatbot with Python","datePublished":"2023-11-04T23:13:58+00:00","dateModified":"2023-11-05T05:48:25+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/"},"wordCount":696,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"AI development\"","\"Artificial Intelligence\"","\"build chatbot\"","\"chatbot development\"","\"chatbot\"","\"programming\"","\"Python libraries\"","\"Python programming\"","\"Python\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/","url":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/","name":"How to Build a Chatbot with Python - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:13:58+00:00","dateModified":"2023-11-05T05:48:25+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/how-to-build-a-chatbot-with-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"How to Build a Chatbot with Python"}]},{"@type":"WebSite","@id":"http:\/\/localhost:10003\/#website","url":"http:\/\/localhost:10003\/","name":"Pantherax Blogs","description":"","publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/localhost:10003\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/localhost:10003\/#organization","name":"Pantherax Blogs","url":"http:\/\/localhost:10003\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/","url":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","contentUrl":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","width":1024,"height":1024,"caption":"Pantherax Blogs"},"image":{"@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7","name":"Panther","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","caption":"Panther"},"sameAs":["http:\/\/localhost:10003"],"url":"http:\/\/localhost:10003\/author\/pepethefrog\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3972"}],"collection":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/comments?post=3972"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3972\/revisions"}],"predecessor-version":[{"id":4548,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3972\/revisions\/4548"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=3972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=3972"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=3972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}