Update for completed project #1

Merged
Trianta merged 6 commits from dev into master 2023-11-12 20:28:49 -06:00
2 changed files with 103 additions and 10 deletions
Showing only changes of commit d11ca9c23c - Show all commits

View File

@ -1,7 +1,82 @@
#include "filter.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
Filter::Filter(void) {
SMSMessage::SMSMessage(MessageType given_type, std::string given_message) {
this->actual_type = given_type;
this->message = given_message;
}
Filter::~Filter(void) {
// Takes in a file name from which it will generate a probability filter
void SMSMessageFilter::GenerateProbability(std::string file_name) {
// Open file
std::ifstream input_messages(file_name);
if (!input_messages.is_open()) {
std::cerr << "Error opening '" << file_name << "'" << std::endl;
std::exit(EXIT_FAILURE);
}
// Read in messages
MessageType temp_type;
std::string temp_message;
while (!input_messages.eof()) {
input_messages.get(&temp_message[0], 10, '\t');
if (temp_message == "spam") { temp_type = kSpam; }
else if (temp_message == "ham") { temp_type = kHam; }
else {
std::cerr << "[SMSMessageFilter - Warning]" <<
" Could not determine message type" << std::endl;
std::cerr << "\t Contains: <" << temp_message << ">" << std::endl;
}
input_messages >> temp_message;
}
}
void SMSMessageFilter::Filter(std::string file_name) {
// Open file
std::ifstream input_messages(file_name);
if (!input_messages.is_open()) {
std::cerr << "Error opening '" << file_name << "'" << std::endl;
std::exit(EXIT_FAILURE);
}
// Read in messages
MessageType temp_type;
std::string temp_message;
while (!input_messages.eof()) {
input_messages.get(&temp_message[0], 10, '\t');
if (temp_message == "spam") { temp_type = kSpam; }
else if (temp_message == "ham") { temp_type = kHam; }
else {
std::cerr << "[SMSMessageFilter - Warning]" <<
" Could not determine message type" << std::endl;
std::cerr << "\t Contains: <" << temp_message << ">" << std::endl;
}
input_messages >> temp_message;
filtered_messages.emplace_back(temp_type, temp_message);
}
}
void SMSMessageFilter::PrintReport(void) {
// Spam precision: (true positives) / (true positives + false positives)
std::cout << "[SMSMessageFilter - Report] Spam precision: ";
// Spam recall: (true positives) / (true positives + false negatives)
std::cout << "[SMSMessageFilter - Report] Spam recall: ";
// Ham precision: (true negatives) / (true negatives + false negatives)
std::cout << "[SMSMessageFilter - Report] Ham precision: ";
// Ham recall: (true negatives) / (true negatives + false positives)
std::cout << "[SMSMessageFilter - Report] Ham recall: ";
// Spam F-Score: 2* (spam precision * spam recall) / (spam precision + spam recall)
std::cout << "[SMSMessageFilter - Report] Spam F-Score: ";
// Ham F-Score: 2* (ham precision * ham recall) / (ham precision + ham recall)
std::cout << "[SMSMessageFilter - Report] Ham F-Score: ";
// Accuracy: (spam recall + ham recall) / 2
std::cout << "[SMSMessageFilter - Report] Accuracy: ";
}

View File

@ -1,17 +1,35 @@
#ifndef FILTER_HPP
#define FILTER_HPP
class Filter {
#include <map>
#include <string>
#include <vector>
enum MessageType {
kSpam = 0,
kHam = 1,
kUnknown = 2,
};
struct SMSMessage {
SMSMessage(MessageType given_type, std::string given_message);
MessageType actual_type;
std::string message;
MessageType filter_type;
};
class SMSMessageFilter {
public:
Filter(void);
Filter(Filter &&) = default;
Filter(const Filter &) = default;
Filter &operator=(Filter &&) = default;
Filter &operator=(const Filter &) = default;
~Filter(void);
SMSMessageFilter(void) = default;
~SMSMessageFilter(void) = default;
void GenerateProbability(std::string file_name);
void Filter(std::string file_name);
void PrintReport(void);
private:
double sentence_probability_ham = 0.5; // Spam is 1 - sentence_probability_ham
std::map<std::string, double> probability_dictionary;
std::vector<SMSMessage> filtered_messages;
};