Markov Chain Text Generator with CSV Input and Random Seeding

The program must only run on a Linux-Based machine and can only accept libraries of the C standard.

This program aims to generate text based on a Markov chain model using input from a CSV file. The CSV contains word sequences with their frequencies. The program should:

Read the CSV file and build a matrix structure.
Allow the user to specify a starting word or randomly select one based on punctuation.
Generate a specified number of words using probabilistic selection based on the frequencies in the CSV.
Use clock-based randomization for word selection.

However, the program enters an indeterminate loop, preventing any debugging or error analysis. There are potential issues with memory management, CSV parsing, or the probabilistic word selection algorithm. The code’s complexity makes it challenging to create a minimized version for debugging.
We’re seeking assistance from someone with a keen eye to identify potential issues in the code, particularly around memory management, file parsing, or the core algorithm implementation.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>

// Structure to represent a word and its properties
typedef struct {
    char word[30];
    float frequency;
    int occurrence;
    int x;
    int y;
} WordNode;

// Structure to represent the matrix of words
typedef struct {
    WordNode** list;
    int* x;
    int y;
} WordMatrix;

// Structure for the trie-like dictionary
struct AlphabetNode {
    struct AlphabetNode* subAlphabet[31];
    WordNode *word;
};

typedef struct AlphabetNode Dictionary;

// Function prototypes
Dictionary* deallocateDict(Dictionary* dict);
int getASCIIIndex(char character);
bool compareStrings(char* string1, char* string2);
int operativeSearch(char string[], int index, Dictionary* dict, int y, WordMatrix* m);
void printGeneratedText(char startWord[30], WordMatrix m, Dictionary* dictionary, int y, int wordsToGenerate, int generatedWordCount, FILE* outputFile);

// Main function
int main() {
    Dictionary* dictionary = (Dictionary*)calloc(1, sizeof(Dictionary));
    dictionary->word = (WordNode*)malloc(sizeof(WordNode));
    
    FILE *inputFile = fopen("input.csv", "r");
    if (inputFile == NULL) {
        fprintf(stderr, "Error opening input file.n");
        return 1;
    }
    FILE *outputFile = fopen("output.txt", "w");
    WordMatrix matrix;
    matrix.y = 0;
    
    matrix.list = NULL;
    matrix.x = NULL;
    char chosenWord[30] = "";
    int wordsToGenerate;

    wordsToGenerate = 30;

    int* punctuationIndices = NULL;
    int capitalCount = 0;
    punctuationIndices = malloc(sizeof(int));

    // Read the CSV file and build the matrix
    while (true) {
        char character = fgetc(inputFile);
        if (character == EOF) {
            break;
        }

        fseek(inputFile, -1, SEEK_CUR);

        matrix.y++;
        matrix.list = realloc(matrix.list, matrix.y * sizeof(WordNode*));
        matrix.x = realloc(matrix.x, matrix.y * sizeof(int));
        matrix.list[matrix.y - 1] = NULL;
        matrix.x[matrix.y - 1] = 0;

        while (character != 'n' && character != EOF) {
            matrix.x[matrix.y - 1]++;
            matrix.list[matrix.y - 1] = realloc(matrix.list[matrix.y - 1], matrix.x[matrix.y - 1] * sizeof(WordNode));

            fseek(inputFile, -1, SEEK_CUR);
            fscanf(inputFile,"n");
            
            if (fscanf(inputFile, "%30[^,],", matrix.list[matrix.y - 1][matrix.x[matrix.y - 1] - 1].word) == 1) {
                if ((matrix.x[matrix.y - 1] - 1) == 0) {
                    operativeSearch(matrix.list[matrix.y - 1][matrix.x[matrix.y - 1] - 1].word, 0, dictionary, matrix.y - 1, &matrix);
                    if (matrix.list[matrix.y - 1][0].word[0] == '.' ||
                        matrix.list[matrix.y - 1][0].word[0] == '!' ||
                        matrix.list[matrix.y - 1][0].word[0] == '?'){

                        punctuationIndices[capitalCount] = matrix.y - 1;
                        printf("start indices: %dn", punctuationIndices[capitalCount]);
                        capitalCount++;
                        punctuationIndices = realloc(punctuationIndices, capitalCount * sizeof(int));
                    }
                }
            }
            
            if (fscanf(inputFile, "%f,", &matrix.list[matrix.y - 1][matrix.x[matrix.y - 1] - 1].frequency) == 1) {
            }

            character = fgetc(inputFile);
        }
    }

    // Generate text
    if(chosenWord[0] != ''){
        int y = operativeSearch(chosenWord, 0, dictionary, 0, &matrix);
        if(y == -1){
            printf("Invalid starting word");
        }
        else{
            printGeneratedText(chosenWord, matrix, dictionary, y, wordsToGenerate, 0, outputFile);
        } 
    }
    else{
        int len = capitalCount;
        srand(clock());
        int randomIndex = rand() % len;
        int punctuationIndex = punctuationIndices[randomIndex];

        for(int i = 1; i <= matrix.x[punctuationIndex]; i++){              
            int y = operativeSearch(matrix.list[punctuationIndex][i].word, 0, dictionary, punctuationIndex, &matrix);
            printGeneratedText(matrix.list[punctuationIndex][i].word, matrix, dictionary, y, wordsToGenerate, 0, outputFile);
            break;
        }
    }

    // Memory deallocation
    for (int i = 0; i <= 2; i++) {
        free(matrix.list[i]);
    }
    free(matrix.list);
    free(matrix.x);

    for (int i = 0; i < 30; i++) {
        dictionary->subAlphabet[i] = deallocateDict(dictionary->subAlphabet[i]);
    }
    free(dictionary->word);
    free(dictionary);

    return 0;
}

// Function to deallocate the dictionary
Dictionary* deallocateDict(Dictionary* dict) {
    if (dict == NULL) {
        return NULL;
    }

    for (int i = 0; i < 30; i++) {
        dict->subAlphabet[i] = deallocateDict(dict->subAlphabet[i]);
    }
    free(dict->word);
    free(dict);
    return NULL;
}

CSV input (input.csv):

What,do,1.0000
do,the,1.0000
the,weather,1.0000
weather,forecasts,0.3333,forecast,0.3333,!,0.3333
forecasts,say,1.0000
say,?,1.0000
?,Today's,0.5000,What,0.5000
Today's,weather,1.0000
forecast,uncertain,0.5000,is 0.5000
uncertain,weather,1.0000
!,Tomorrow's,1.0000
Tomorrow's,forecast,1.0000
is,uncertain,1.0000

Random Output file (output.txt):

Tomorrow's forecast is uncertain weather ! Tomorrow's forecast is uncertain weather ! Tomorrow's forecast is uncertain

Can you help analyze this code and suggest potential areas of improvement or identify the source of the infinite loop?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật