Upload naive_bayes.cpp with huggingface_hub
Browse files- naive_bayes.cpp +88 -0
naive_bayes.cpp
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <iostream>
|
| 2 |
+
#include <vector>
|
| 3 |
+
#include <string>
|
| 4 |
+
#include <map>
|
| 5 |
+
#include <cmath>
|
| 6 |
+
|
| 7 |
+
/**
|
| 8 |
+
* @brief A simple Naive Bayes Classifier component.
|
| 9 |
+
* Inspired by Chapter 2 of the "Building AI" course (Elements of AI).
|
| 10 |
+
*/
|
| 11 |
+
|
| 12 |
+
class NaiveBayes {
|
| 13 |
+
public:
|
| 14 |
+
void train(const std::vector<std::string>& texts, const std::vector<int>& labels) {
|
| 15 |
+
for (size_t i = 0; i < texts.size(); ++i) {
|
| 16 |
+
int label = labels[i];
|
| 17 |
+
class_counts[label]++;
|
| 18 |
+
total_samples++;
|
| 19 |
+
|
| 20 |
+
// Simple word tokenization (splitting by space)
|
| 21 |
+
std::string word;
|
| 22 |
+
for (char c : texts[i]) {
|
| 23 |
+
if (c == ' ') {
|
| 24 |
+
word_counts[label][word]++;
|
| 25 |
+
word;
|
| 26 |
+
} else {
|
| 27 |
+
word += c;
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
if (!word.empty()) word_counts[label][word]++;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
int predict(const std::string& text) {
|
| 35 |
+
double best_prob = -1e18;
|
| 36 |
+
int best_label = -1;
|
| 37 |
+
|
| 38 |
+
for (auto const& [label, count] : class_counts) {
|
| 39 |
+
double log_prob = std::log((double)count / total_samples);
|
| 40 |
+
|
| 41 |
+
std::string word;
|
| 42 |
+
for (char c : text) {
|
| 43 |
+
if (c == ' ') {
|
| 44 |
+
log_prob += calculate_word_log_prob(label, word);
|
| 45 |
+
word;
|
| 46 |
+
} else {
|
| 47 |
+
word += c;
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
if (!word.empty()) log_prob += calculate_word_log_prob(label, word);
|
| 51 |
+
|
| 52 |
+
if (log_prob > best_prob) {
|
| 53 |
+
best_prob = log_prob;
|
| 54 |
+
best_label = label;
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
return best_label;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
private:
|
| 61 |
+
std::map<int, int> class_counts;
|
| 62 |
+
std::map<int, std::map<std::string, int>> word_counts;
|
| 63 |
+
int total_samples = 0;
|
| 64 |
+
|
| 65 |
+
double calculate_word_log_prob(int label, const std::string& word) {
|
| 66 |
+
// Laplace smoothing
|
| 67 |
+
int count = word_counts[label][word];
|
| 68 |
+
int total_words_in_class = 0;
|
| 69 |
+
for (auto const& [w, c] : word_counts[label]) total_words_in_class += c;
|
| 70 |
+
|
| 71 |
+
return std::log((double)(count + 1) / (total_words_in_class + 1000)); // Assuming vocab size 1000
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
int main() {
|
| 76 |
+
std::cout << "--- Naive Bayes AI Component ---" << std::endl;
|
| 77 |
+
|
| 78 |
+
NaiveBayes nb;
|
| 79 |
+
nb.train({"good great awesome", "bad terrible awful"}, {1, 0});
|
| 80 |
+
|
| 81 |
+
std::string test = "great awesome";
|
| 82 |
+
int prediction = nb.predict(test);
|
| 83 |
+
|
| 84 |
+
std::cout << "Text: \"" << test << "\"" << std::endl;
|
| 85 |
+
std::cout << "Prediction: " << (prediction == 1 ? "Positive" : "Negative") << std::endl;
|
| 86 |
+
|
| 87 |
+
return 0;
|
| 88 |
+
}
|