Write a simple symbolic AI that can learn from text in C++
- User
Write a simple symbolic AI that can learn from text in C++
- Assistant
Write a simple symbolic AI that can learn from text in C++
Certainly! Below is an example of a simple symbolic AI implemented in C++ that can learn from text. This AI will:
This example uses basic string manipulation and does not rely on advanced NLP libraries, making it suitable for educational purposes.
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
struct Triple {
std::string subject;
std::string verb;
std::string object;
};
class KnowledgeBase {
public:
void addTriple(const Triple& t) {
triples.push_back(t);
}
void querySubject(const std::string& subject) const {
bool found = false;
for (const auto& t : triples) {
if (t.subject == subject) {
std::cout << t.subject << " " << t.verb << " " << t.object << std::endl;
found = true;
}
}
if (!found) {
std::cout << "No entries found for subject: " << subject << std::endl;
}
}
void queryVerb(const std::string& verb) const {
bool found = false;
for (const auto& t : triples) {
if (t.verb == verb) {
std::cout << t.subject << " " << t.verb << " " << t.object << std::endl;
found = true;
}
}
if (!found) {
std::cout << "No entries found for verb: " << verb << std::endl;
}
}
void queryObject(const std::string& object) const {
bool found = false;
for (const auto& t : triples) {
if (t.object == object) {
std::cout << t.subject << " " << t.verb << " " << t.object << std::endl;
found = true;
}
}
if (!found) {
std::cout << "No entries found for object: " << object << std::endl;
}
}
private:
std::vector<Triple> triples;
};
std::vector<std::string> tokenize(const std::string& sentence) {
std::istringstream iss(sentence);
std::string word;
std::vector<std::string> tokens;
while (iss >> word) {
// Remove punctuation at the end of words
while (!word.empty() && ispunct(word.back())) {
word.pop_back();
}
tokens.push_back(word);
}
return tokens;
}
Triple parseSentence(const std::string& sentence) {
Triple t;
auto tokens = tokenize(sentence);
if (tokens.size() >= 3) {
t.subject = tokens[0];
t.verb = tokens[1];
// Combine remaining tokens as the object
t.object = tokens[2];
for (size_t i = 3; i < tokens.size(); ++i) {
t.object += " " + tokens[i];
}
}
return t;
}
int main() {
KnowledgeBase kb;
std::string input;
std::cout << "Enter sentences (type 'exit' to stop learning):" << std::endl;
while (true) {
std::cout << "> ";
std::getline(std::cin, input);
if (input == "exit")
break;
Triple t = parseSentence(input);
if (!t.subject.empty() && !t.verb.empty() && !t.object.empty()) {
kb.addTriple(t);
std::cout << "Learned: " << t.subject << " " << t.verb << " " << t.object << std::endl;
} else {
std::cout << "Could not parse sentence." << std::endl;
}
}
std::cout << "\nKnowledge base queries (type 'exit' to quit):" << std::endl;
while (true) {
std::cout << "Query by (subject/verb/object): ";
std::string queryType;
std::cin >> queryType;
if (queryType == "exit")
break;
std::cout << "Enter " << queryType << ": ";
std::string queryValue;
std::cin.ignore(); // Ignore any leftover newline characters
std::getline(std::cin, queryValue);
if (queryType == "subject") {
kb.querySubject(queryValue);
} else if (queryType == "verb") {
kb.queryVerb(queryValue);
} else if (queryType == "object") {
kb.queryObject(queryValue);
} else {
std::cout << "Unknown query type. Please enter 'subject', 'verb', or 'object'." << std::endl;
}
}
return 0;
}
Triple Structure: A simple structure to hold a subject, verb, and object extracted from a sentence.
KnowledgeBase Class: Manages a collection of Triple objects.
addTriple(): Adds a new Triple to the knowledge base.querySubject(), queryVerb(), queryObject(): Search and display triples matching the given subject, verb, or object.Tokenize Function:
ParseSentence Function:
tokenize function to split the sentence.Main Function:
> Alice loves Bob.
> Bob knows Charlie.
> Charlie plays soccer.
> The cat chases the mouse.
> exit
Knowledge base queries (type 'exit' to quit):
Query by (subject/verb/object): subject
Enter subject: Bob
Bob knows Charlie
Query by (subject/verb/object): object
Enter object: soccer
Charlie plays soccer
Query by (subject/verb/object): exit
To compile and run the program:
symbolic_ai.cpp.g++ -o symbolic_ai symbolic_ai.cpp
./symbolic_ai
Ensure that you have a C++11-compatible compiler, as the code uses features introduced in C++11.
This simple symbolic AI provides a foundation for understanding how an AI system can learn from text input by users. By expanding upon this basic structure, you can create more sophisticated AI applications that perform complex language understanding and knowledge representation.
Not yet included in a published release.