A few optimizations.

This commit is contained in:
retoor 2024-12-01 19:40:01 +01:00
parent 3ae1979f26
commit 6be2d372f2
2 changed files with 16 additions and 25 deletions

BIN
isspam

Binary file not shown.

View File

@ -174,22 +174,14 @@ bool is_fully_capitalized_word(char *word) {
return true; return true;
} }
sl *get_capitalized_words(char *content) { sl *get_capitalized_words(sl *all_words) {
sl *capitalized_words = sln(); sl *capitalized_words = sln();
sl *sentences = get_sentences(content);
for (uint j = 0; j < sentences->count; j++) {
char *sentence = sentences->strings[j];
sl *all_words = get_words(sentence);
// Always skip the first word since sentences start with
for (uint i = 0; i < all_words->count; i++) { for (uint i = 0; i < all_words->count; i++) {
if (is_fully_capitalized_word(all_words->strings[i])) { if (is_fully_capitalized_word(all_words->strings[i])) {
rstring_list_add(capitalized_words, all_words->strings[i]); rstring_list_add(capitalized_words, all_words->strings[i]);
} }
} }
slf(all_words);
}
slf(sentences);
return capitalized_words; return capitalized_words;
} }
@ -251,16 +243,13 @@ bool containswordi(sl *words, char *word) {
return false; return false;
} }
sl *get_forbidden_words(char *content) { sl *get_forbidden_words(sl *words) {
sl *words = get_words(content);
sl *found = sln(); sl *found = sln();
for (int j = 0; forbidden_words[j] != NULL; j++) { for (int j = 0; forbidden_words[j] != NULL; j++) {
if (containswordi(words, forbidden_words[j])) { if (containswordi(words, forbidden_words[j])) {
rstring_list_add(found, forbidden_words[j]); rstring_list_add(found, forbidden_words[j]);
} }
} }
slf(words);
return found; return found;
} }
unsigned int total = 0; unsigned int total = 0;
@ -278,8 +267,18 @@ void analyze(FILE *f) {
free(clean_data); free(clean_data);
sl *words = get_words(data);
// All words
printf("Words: %llu\n", words->count);
if(show_words)
sld(words);
sbuf = slds(words);
stra(all, sbuf);
free(sbuf);
// All capitalized words // All capitalized words
sl *capitalized_words = get_capitalized_words(data); sl *capitalized_words = get_capitalized_words(words);
ulonglong capitalized_words_count = capitalized_words->count; ulonglong capitalized_words_count = capitalized_words->count;
printf("Capitalized words: %llu\n", capitalized_words_count); printf("Capitalized words: %llu\n", capitalized_words_count);
if(show_capitalized) if(show_capitalized)
@ -298,15 +297,7 @@ void analyze(FILE *f) {
stra(all, sbuf); stra(all, sbuf);
free(sbuf); free(sbuf);
sl *words = get_words(data);
// All words
printf("Words: %llu\n", words->count);
if(show_words)
sld(words);
sbuf = slds(words);
stra(all, sbuf);
free(sbuf);
// Numbers // Numbers
sl *numbers = get_numbers(data); sl *numbers = get_numbers(data);
@ -318,7 +309,7 @@ void analyze(FILE *f) {
free(sbuf); free(sbuf);
// Forbidden words // Forbidden words
sl *fw = get_forbidden_words(data); sl *fw = get_forbidden_words(words);
printf("Forbidden words: %llu\n", fw->count); printf("Forbidden words: %llu\n", fw->count);
if(show_forbidden_words) if(show_forbidden_words)
sld(fw); sld(fw);