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
#include <iostream>#include <fstream>#include <vector>#include <string>#include <windows.h> // For system calls and text stylingusing namespace std;// Function prototype for NLP processingstring processNLP(const string& input, const vector<pair<string, string>>& knowledgeBase);// Function to load training datavector<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 colorvoid setColor(int color) {SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);}// Function to create a stylish bordervoid drawBorder() {setColor(11); // Light Bluecout << "\n=====================================\n";setColor(14); // Yellowcout << " π€ AI Chatbot v2.0 \n";setColor(11);cout << "=====================================\n";setColor(15); // Reset to white}// Main chatbot function with a modern UIvoid chatbot() {vector<pair<string, string>> knowledgeBase = loadTrainingData("data.txt");string userInput;system("cls"); // Clear screendrawBorder();setColor(10); // Greencout << "π€ AI Chatbot: Hello! Ask me anything (type 'exit' to quit)!\n";setColor(15);while (true) {setColor(9); // Bluecout << "\nYou: ";setColor(15);getline(cin, userInput);if (userInput == "exit") {setColor(12); // Redcout << "π€ AI Chatbot: Goodbye! Have a great day! π\n";setColor(15);break;}// Process NLP and get responsestring response = processNLP(userInput, knowledgeBase);// Display chatbot response with animation effectsetColor(10); // Greencout << "π€ AI Chatbot: ";for (char c : response) {cout << c;Sleep(30); // Typing effect delay}cout << endl;setColor(15);}}// Simple NLP Function to find a responsestring 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 Functionint main() {chatbot();return 0;}
#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
#include "nlp.h"#include <algorithm>#include <iostream>using namespace std;// Function to process NLP and find the best responsestring 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
- Open CMD in your project folder.
- Compile the code using: cmd = > g++ chatbot.cpp nlp.cpp -o chatbot
Comments
Post a Comment