#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAX_WORD_LENGTH 20
#define MAX_SUBSTITUTIONS 5

// Define a struct to hold a word and its aphasia-like substitutions.
typedef struct {
    char original_word[MAX_WORD_LENGTH];
    char substitutions[MAX_SUBSTITUTIONS][MAX_WORD_LENGTH];
    int num_substitutions;
} WordMapping;

// A simple database of word mappings.
WordMapping aphasia_db[] = {
    {"cat", {"dog", "car", "hat", "that thing"}, 4},
    {"book", {"table", "paper", "box", "the reading thing"}, 4},
    {"walk", {"run", "drive", "stumble", "that thing with my legs"}, 4},
    {"apple", {"banana", "orange", "grape", "the red fruit"}, 4}
};

// Function to find a word mapping in our database.
WordMapping* find_word_mapping(const char* word) {
    int db_size = sizeof(aphasia_db) / sizeof(aphasia_db[0]);
    for (int i = 0; i < db_size; i++) {
        if (strcmp(aphasia_db[i].original_word, word) == 0) {
            return &aphasia_db[i];
        }
    }
    return NULL;
}

void simulate_fluent_aphasia(char* sentence) {
    char* token;
    char temp_sentence[500];
    strcpy(temp_sentence, sentence);

    printf("Simulating Fluent Aphasia:\n");
    printf("Original: \"%s\"\n", sentence);
    printf("Result: \"");

    token = strtok(temp_sentence, " ");
    while (token != NULL) {
        WordMapping* mapping = find_word_mapping(token);
        if (mapping != NULL) {
            // Randomly select and print a substitution.
            int index = rand() % mapping->num_substitutions;
            printf("%s ", mapping->substitutions[index]);
        } else {
            // Print the original word if no mapping is found.
            printf("%s ", token);
        }
        token = strtok(NULL, " ");
    }
    printf("\"\n\n");
}

void simulate_non_fluent_aphasia(char* sentence) {
    char* token;
    char temp_sentence[500];
    strcpy(temp_sentence, sentence);

    printf("Simulating Non-Fluent Aphasia:\n");
    printf("Original: \"%s\"\n", sentence);
    printf("Result: \"");

    token = strtok(temp_sentence, " ");
    int word_count = 0;
    while (token != NULL) {
        if (word_count == 0 || strcmp(token, ".") == 0 || strcmp(token, ",") == 0) {
            // Keep the first word and punctuation.
            printf("%s ", token);
        } else {
            // Drop other words to simulate halting speech.
            printf("... ");
            break; 
        }
        token = strtok(NULL, " ");
        word_count++;
    }
    printf("\"\n\n");
}

int main() {
    srand(time(NULL)); // Seed the random number generator.

    char sentence1[] = "I want to read a book.";
    char sentence2[] = "The cat is in the garden.";

    simulate_fluent_aphasia(sentence1);
    simulate_fluent_aphasia(sentence2);

    simulate_non_fluent_aphasia(sentence1);
    simulate_non_fluent_aphasia(sentence2);

    return 0;
}