feat: add C++ port of isspam with updated tokenizer and benchmark integration

Add a new C++ implementation of the isspam text analysis tool in retoor_c/isspam.cpp, mirroring the C version's functionality with thread-based file analysis. Update the Makefile with a build_cpp target and add the new binary to .gitignore. Modify bench.py to include timing for the C++ executable. Fix the C tokenizer's delimiter string from punctuation-only to include all standard whitespace characters (\f\v\r\n\t).
This commit is contained in:
2025-03-20 14:27:02 +00:00
parent 4b44ad85ec
commit ca42ad8abc
5 changed files with 140 additions and 8 deletions
+3 -3
View File
@@ -98,7 +98,7 @@ void* analyze_file(void* arg) {
}
char *saveptr;
char* token = strtok_r(text, " .?!;:\n", &saveptr);
char* token = strtok_r(text, " \f\v\r\n\t", &saveptr);
while (token != NULL) {
word_count++;
@@ -117,7 +117,7 @@ void* analyze_file(void* arg) {
forbidden_count++;
}
token = strtok_r(NULL, " .?!;:\n", &saveptr);
token = strtok_r(NULL, " \f\v\r\n\t", &saveptr);
}
result->total_word_count = word_count;
@@ -180,4 +180,4 @@ int main(int argc, char *argv[]) {
printf("Word count per sentence: %.6f\n", word_count_per_sentence);
printf("Total files read: %d\n", (int)(argc - 1));
return 0;
}
}
+129
View File
@@ -0,0 +1,129 @@
// Author: retoor@molodetz.nl
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <thread>
#include <unordered_set>
#include <algorithm>
#include <sstream>
#define FORBIDDEN_WORDS_COUNT 40
const std::unordered_set<std::string> forbidden_words = {
"recovery", "techie", "http", "https", "digital", "hack", "::", "//", "com",
"@", "crypto", "bitcoin", "wallet", "hacker", "welcome", "whatsapp", "email", "cryptocurrency",
"stolen", "freeze", "quick", "crucial", "tracing", "scammers", "expers", "hire", "century",
"transaction", "essential", "managing", "contact", "contacting", "understanding", "assets", "funds",
};
struct AnalysisResult {
std::string filename;
long long total_word_count = 0;
long long total_capitalized_count = 0;
long long total_sentence_count = 0;
long long total_number_count = 0;
long long total_forbidden_count = 0;
};
std::string read_file(const std::string& filename) {
std::ifstream file(filename);
if (!file) {
std::cerr << "File doesn't exist: " << filename << std::endl;
return "";
}
std::ostringstream content;
content << file.rdbuf(); // Read the entire file into a string
return content.str();
}
void analyze_file(AnalysisResult& result) {
std::string text = read_file(result.filename);
if (!text.empty()) {
long long word_count = 0;
long long capitalized_count = 0;
long long sentence_count = 0;
long long number_count = 0;
long long forbidden_count = 0;
for (char c : text) {
if (c == '.') {
sentence_count++;
}
}
std::istringstream stream(text);
std::string token;
while (stream >> token) {
word_count++;
if (std::isupper(token[0])) {
capitalized_count++;
}
if (std::any_of(token.begin(), token.end(), ::isdigit)) {
number_count++;
}
if (forbidden_words.find(token) != forbidden_words.end()) {
forbidden_count++;
}
}
result.total_word_count = word_count;
result.total_capitalized_count = capitalized_count;
result.total_sentence_count = sentence_count;
result.total_number_count = number_count;
result.total_forbidden_count = forbidden_count;
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <file1> <file2> ... <fileN>" << std::endl;
return 1;
}
std::vector<std::thread> threads;
std::vector<AnalysisResult> results(argc - 1);
for (int i = 1; i < argc; i++) {
results[i - 1].filename = argv[i];
threads.emplace_back(analyze_file, std::ref(results[i - 1]));
}
for (auto& thread : threads) {
thread.join();
}
long long total_word_count = 0;
long long total_capitalized_count = 0;
long long total_sentence_count = 0;
long long total_number_count = 0;
long long total_forbidden_count = 0;
for (const auto& result : results) {
total_word_count += result.total_word_count;
total_capitalized_count += result.total_capitalized_count;
total_sentence_count += result.total_sentence_count;
total_number_count += result.total_number_count;
total_forbidden_count += result.total_forbidden_count;
}
double capitalized_percentage = (total_word_count > 0) ? (static_cast<double>(total_capitalized_count) / total_word_count * 100.0) : 0;
double forbidden_percentage = (total_word_count > 0) ? (static_cast<double>(total_forbidden_count) / total_word_count * 100.0) : 0;
double word_count_per_sentence = (total_sentence_count > 0) ? (static_cast<double>(total_word_count) / total_sentence_count) : 0;
std::cout << "\nTotal Words: " << total_word_count << std::endl;
std::cout << "Total Capitalized words: " << total_capitalized_count << std::endl;
std::cout << "Total Sentences: " << total_sentence_count << std::endl;
std::cout << "Total Numbers: " << total_number_count << std::endl;
std::cout << "Total Forbidden words: " << total_forbidden_count << std::endl;
std::cout << "Capitalized percentage: " << capitalized_percentage << "%" << std::endl;
std::cout << "Forbidden percentage: " << forbidden_percentage << "%" << std::endl;
std::cout << "Word count per sentence: " << word_count_per_sentence << std::endl;
std::cout << "Total files read: " << (argc - 1) << std::endl;
return 0;
}