-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_frequency.c
More file actions
130 lines (101 loc) · 3.76 KB
/
Copy pathword_frequency.c
File metadata and controls
130 lines (101 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @file word_frequency.c
* @brief Real-world example: Word frequency counter using NanoMap
*
* Demonstrates v1.0.0 features:
* - Hash map with randomized seeding (Anti-DoS)
* - Iterator usage
* - Secure flag for sensitive data
*/
#define NANODS_IMPLEMENTATION
#include "../nanods.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 64
void to_lowercase(char* str) {
for (int i = 0; str[i]; i++) {
str[i] = tolower((unsigned char)str[i]);
}
}
void count_word_frequencies(const char* text, NanoMap* freq_map) {
char word[MAX_WORD_LEN];
int word_idx = 0;
for (size_t i = 0; i <= strlen(text); i++) {
char c = text[i];
if (isalnum((unsigned char)c)) {
if (word_idx < MAX_WORD_LEN - 1) {
word[word_idx++] = c;
}
} else if (word_idx > 0) {
word[word_idx] = '\0';
to_lowercase(word);
int* count_ptr = (int*)nm_get(freq_map, word);
int new_count = count_ptr ? (*count_ptr + 1) : 1;
int* new_count_ptr = (int*)malloc(sizeof(int));
*new_count_ptr = new_count;
nm_set(freq_map, word, new_count_ptr);
word_idx = 0;
}
}
}
void find_most_common(NanoMap* freq_map, char* most_common_word, int* max_count) {
NanoMapIterator iter;
nm_iter_init(&iter, freq_map);
const char* key;
void* value;
*max_count = 0;
strcpy(most_common_word, "");
while (nm_iter_next(&iter, &key, &value) == NANODS_OK) {
int count = *(int*)value;
if (count > *max_count) {
*max_count = count;
strcpy(most_common_word, key);
}
}
}
int main(void) {
printf("==============================================\n");
printf(" Word Frequency Counter (NanoDS v%s)\n", NANODS_VERSION);
printf("==============================================\n\n");
/* Initialize hash seed for Anti-DoS protection */
nanods_seed_init(0);
printf("Hash seed initialized: 0x%08X\n\n", nanods_get_seed());
const char* text =
"The quick brown fox jumps over the lazy dog. "
"The dog was sleeping under the tree. "
"The fox was quick and clever. "
"A quick brown fox is a clever fox. ";
printf("Text:\n\"%s\"\n\n", text);
/* Count word frequencies with secure flag (for demonstration) */
NanoMap freq_map;
nm_init_ex(&freq_map, NANODS_FLAG_SECURE);
count_word_frequencies(text, &freq_map);
/* Display results */
printf("Word Frequencies:\n");
printf("─────────────────────────────────\n");
NanoMapIterator iter;
nm_iter_init(&iter, &freq_map);
const char* key;
void* value;
while (nm_iter_next(&iter, &key, &value) == NANODS_OK) {
int count = *(int*)value;
printf("%-15s : %d\n", key, count);
}
printf("─────────────────────────────────\n");
printf("Total unique words: %zu\n", nm_size(&freq_map));
printf("Hash seed: 0x%08X (Anti-DoS protected)\n\n", freq_map.seed);
/* Find most common word */
char most_common[MAX_WORD_LEN];
int max_count;
find_most_common(&freq_map, most_common, &max_count);
printf("🏆 Most common word: '%s' (%d times)\n\n", most_common, max_count);
/* Cleanup */
nm_iter_init(&iter, &freq_map);
while (nm_iter_next(&iter, &key, &value) == NANODS_OK) {
free(value);
}
nm_free(&freq_map); /* Secure free automatically wipes memory */
printf("✅ Done! (Memory securely wiped)\n");
return 0;
}