Write a c program to tokenize a string by a list of possible tokens and will prioritize…
- User
Write a c program to tokenize a string by a list of possible tokens and will prioritize larger tokens
- Assistant
Write a c program to tokenize a string by a list of possible tokens and will prioritize larger tokens
Certainly! Below is a C program that tokenizes a given string based on a list of possible tokens provided by the user. The program prioritizes larger tokens when tokenizing the string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TOKEN_LENGTH 256 // Maximum length of a token
#define MAX_INPUT_LENGTH 1024 // Maximum length of the input string
#define ASCII_SIZE 256 // Size of the ASCII character set
// Trie node structure
typedef struct TrieNode {
struct TrieNode *children[ASCII_SIZE];
int is_end; // Indicates if the node represents the end of a token
} TrieNode;
// Function to create a new Trie node
TrieNode* createNode() {
TrieNode *node = (TrieNode*)malloc(sizeof(TrieNode));
if (node == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
node->is_end = 0;
for (int i = 0; i < ASCII_SIZE; i++) {
node->children[i] = NULL;
}
return node;
}
// Function to insert a token into the Trie
void insert(TrieNode *root, const char *word) {
TrieNode *node = root;
while (*word) {
unsigned char index = (unsigned char)*word;
if (node->children[index] == NULL) {
node->children[index] = createNode();
}
node = node->children[index];
word++;
}
node->is_end = 1;
}
// Function to search for the longest matching token starting at position 'pos'
int searchLongestToken(TrieNode *root, const char *str, int pos, int len, char *token) {
TrieNode *node = root;
int max_length = 0;
int current_length = 0;
int i;
for (i = pos; i < len; i++) {
unsigned char index = (unsigned char)str[i];
if (node->children[index] == NULL) {
break;
}
node = node->children[index];
current_length++;
if (node->is_end) {
max_length = current_length;
}
}
if (max_length > 0) {
strncpy(token, &str[pos], max_length);
token[max_length] = '\0';
}
return max_length;
}
// Function to free the Trie
void freeTrie(TrieNode *node) {
if (node == NULL) {
return;
}
for (int i = 0; i < ASCII_SIZE; i++) {
if (node->children[i] != NULL) {
freeTrie(node->children[i]);
}
}
free(node);
}
int main() {
TrieNode *root = createNode();
int num_tokens;
// Read the number of tokens
printf("Enter the number of tokens: ");
if (scanf("%d", &num_tokens) != 1 || num_tokens <= 0) {
printf("Invalid number of tokens.\n");
freeTrie(root);
return 1;
}
// Clear the newline character after reading num_tokens
getchar();
// Read the tokens and insert them into the Trie
printf("Enter the tokens (one per line):\n");
for (int i = 0; i < num_tokens; i++) {
char buffer[MAX_TOKEN_LENGTH];
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
printf("Error reading token.\n");
freeTrie(root);
return 1;
}
// Remove newline character if present
buffer[strcspn(buffer, "\n")] = '\0';
if (strlen(buffer) == 0) {
printf("Empty token is not allowed.\n");
freeTrie(root);
return 1;
}
insert(root, buffer);
}
// Read the input string to tokenize
printf("Enter the string to tokenize:\n");
char input_string[MAX_INPUT_LENGTH];
if (fgets(input_string, sizeof(input_string), stdin) == NULL) {
printf("Error reading input string.\n");
freeTrie(root);
return 1;
}
// Remove newline character if present
input_string[strcspn(input_string, "\n")] = '\0';
int len = strlen(input_string);
int pos = 0;
printf("\nTokenized output:\n");
while (pos < len) {
char token[MAX_TOKEN_LENGTH];
int token_length = searchLongestToken(root, input_string, pos, len, token);
if (token_length > 0) {
printf("%s ", token);
pos += token_length;
} else {
// No matching token found; print the character as is and move to the next position
printf("%c ", input_string[pos]);
pos++;
}
}
printf("\n");
// Free the Trie
freeTrie(root);
return 0;
}
How the Program Works:
Trie Implementation:
insert function adds tokens to the Trie, and the searchLongestToken function searches for the longest token starting from a given position.Tokenization Process:
Prioritizing Larger Tokens:
searchLongestToken function ensures that the longest possible token starting at the current position is selected. This prioritizes larger tokens over smaller ones.Example Usage:
Let's go through an example to see how the program works.
Sample Input:
Enter the number of tokens: 6
Enter the tokens (one per line):
this
is
a
test
th
his
Enter the string to tokenize:
thisisatest
Sample Output:
Tokenized output:
this is a test
Explanation:
Notes:
MAX_TOKEN_LENGTH and MAX_INPUT_LENGTH, respectively.Compiling and Running the Program:
To compile the program, save it to a file called tokenize.c and run the following command in your terminal:
gcc -o tokenize tokenize.c
Then run the executable:
./tokenize
Feedback and Modifications:
#define statements at the beginning of the program.Feel free to ask if you need further assistance or modifications to the program!
Not yet included in a published release.