C++ AI Chatbot with NLP – Build a Smart Chatbot Using C++ (Full Code + Step-by-Step Guide)

Demo :


Click Video πŸ‘‡πŸ‘‡πŸ‘‡













πŸ“‚ Folder Structure

C++_AI_Chatbot/

│── chatbot.cpp            # Main chatbot source code

│── nlp.h                  # Header file for NLP functions

│── nlp.cpp                # NLP function implementations

│── data.txt               # Training data (questions and answers)

│── assets/                # (Optional) Icons, images, or additional assets

│── README.md              # Project documentation

│── Makefile               # (Optional) To compile the project using `make`

└── bin/                   # (Optional) Compiled binary files


Code :

1️⃣ chatbot.cpp (Main Chatbot Code)

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>  // For system calls and text styling

using namespace std;

// Function prototype for NLP processing
string processNLP(const string& input, const vector<pair<string, string>>& knowledgeBase);

// Function to load training data
vector<pair<string, string>> loadTrainingData(string filename) {
    vector<pair<string, string>> data;
    ifstream file(filename);
    string question, answer;

    while (getline(file, question) && getline(file, answer)) {
        data.push_back({question, answer});
    }
    
    file.close();
    return data;
}

// Function to set console color
void setColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

// Function to create a stylish border
void drawBorder() {
    setColor(11); // Light Blue
    cout << "\n=====================================\n";
    setColor(14); // Yellow
    cout << "        πŸ€– AI Chatbot v2.0          \n";
    setColor(11);
    cout << "=====================================\n";
    setColor(15); // Reset to white
}

// Main chatbot function with a modern UI
void chatbot() {
    vector<pair<string, string>> knowledgeBase = loadTrainingData("data.txt");
    string userInput;

    system("cls");  // Clear screen
    drawBorder();
    setColor(10); // Green
    cout << "πŸ€– AI Chatbot: Hello! Ask me anything (type 'exit' to quit)!\n";
    setColor(15);

    while (true) {
        setColor(9); // Blue
        cout << "\nYou: ";
        setColor(15);
        getline(cin, userInput);
        
        if (userInput == "exit") {
            setColor(12); // Red
            cout << "πŸ€– AI Chatbot: Goodbye! Have a great day! πŸ‘‹\n";
            setColor(15);
            break;
        }

        // Process NLP and get response
        string response = processNLP(userInput, knowledgeBase);

        // Display chatbot response with animation effect
        setColor(10); // Green
        cout << "πŸ€– AI Chatbot: ";
        for (char c : response) {
            cout << c;
            Sleep(30); // Typing effect delay
        }
        cout << endl;
        setColor(15);
    }
}

// Simple NLP Function to find a response
string processNLP(const string& input, const vector<pair<string, string>>& knowledgeBase) {
    for (const auto& pair : knowledgeBase) {
        if (input == pair.first) {
            return pair.second;
        }
    }
    return "I'm still learning! Can you ask something else?";
}

// Main Function
int main() {
    chatbot();
    return 0;
}


2️⃣ nlp.h (Header File)

#ifndef NLP_H
#define NLP_H

#include <string>
#include <vector>

std::string processNLP(const std::string& input, const std::vector<std::pair<std::string, std::string>>& data);

#endif

3️⃣ nlp.cpp (NLP Implementation)

#include "nlp.h"
#include <algorithm>
#include <iostream>

using namespace std;

// Function to process NLP and find the best response
string processNLP(const string& input, const vector<pair<string, string>>& data) {
    string bestMatch = "Sorry, I don't understand that. Can you rephrase?";

    for (const auto& pair : data) {
        string question = pair.first;
        string answer = pair.second;

        if (input.find(question) != string::npos) {
            return answer;
        }
    }
    
    return bestMatch;
}


4️⃣ data.txt (Training Data)


Hello

Hi there! How can I help you? 😊

How are you?

I'm just a chatbot, but I'm always here to help! πŸ˜ƒ

What is your name?

I am Fuzzu AI Chatbot, your virtual assistant! πŸ€–

Tell me a joke

Why don’t skeletons fight each other? They don’t have the guts! πŸ˜‚

exit

Goodbye! Have a great day! πŸ‘‹

 

πŸ’» How to Compile and Run

  1. Open CMD in your project folder.
  2. Compile the code using: cmd = > g++ chatbot.cpp nlp.cpp -o chatbot

Run the chatbot: cmd => chatbot

Comments

Popular posts from this blog

πŸš€ Simple Login & Registration System in Python Tkinter πŸ“±

πŸš€ Create a Python Screen Recorder with Audio (Complete Code)

πŸ“‘ Fuzzu Packet Sniffer – Python GUI for Real-Time IP Monitoring | Tkinter + Scapy