Structuring the Document

  • + 0 comments

    not the prettiest code i've ever written

    void append_word(struct word *tmp_word, int *letter_count, char ch){
        tmp_word->data = realloc(tmp_word->data, 
            sizeof(char)*(*letter_count+1));
        tmp_word->data[*letter_count] = ch;
        *letter_count += 1;
    }
    
    void reset_word(struct word *tmp_word, int *letter_count){
        tmp_word->data = malloc(sizeof(char));
        *letter_count = 0;
    }
    
    void append_sentence(struct sentence *tmp_sentence, struct word *tmp_word){
        tmp_sentence->data = realloc(tmp_sentence->data, 
            sizeof(struct word)*(tmp_sentence->word_count+1));
        tmp_sentence->data[tmp_sentence->word_count] = *tmp_word;
        tmp_sentence->word_count++;
    }
    
    void reset_sentence(struct sentence *tmp_sentence){
        tmp_sentence->data = malloc(sizeof(struct word));
        tmp_sentence->word_count = 0;
    }
    
    void append_paragraph(struct paragraph *tmp_paragraph, struct sentence *tmp_sentence){
        tmp_paragraph->data = realloc(tmp_paragraph->data, 
            sizeof(struct sentence)*(tmp_paragraph->sentence_count+1));
        tmp_paragraph->data[tmp_paragraph->sentence_count] = *tmp_sentence;
        tmp_paragraph->sentence_count++;
    }
    
    void reset_paragraph(struct paragraph *tmp_paragraph){
        tmp_paragraph->data = malloc(sizeof(struct sentence));
        tmp_paragraph->sentence_count = 0;
    }
    
    void append_document(struct document *tmp_document, struct paragraph *tmp_paragraph){
        tmp_document->data = realloc(tmp_document->data, 
            sizeof(struct paragraph)*(tmp_document->paragraph_count+1));
        tmp_document->data[tmp_document->paragraph_count] = *tmp_paragraph;
        tmp_document->paragraph_count++;
    }
    
    struct document get_document(char* text) {
        //get length of text
        int text_length = strlen(text);
        
        struct word *tmp_word;
        int letters;
        tmp_word = malloc(sizeof(struct word));
        reset_word(tmp_word, &letters);
        
        struct sentence *tmp_sentence;
        tmp_sentence = malloc(sizeof(struct sentence));
        reset_sentence(tmp_sentence);
        
        struct paragraph *tmp_paragraph;
        tmp_paragraph = malloc(sizeof(struct paragraph));
        reset_paragraph(tmp_paragraph);
        
        struct document *tmp_document;
        tmp_document = malloc(sizeof(struct document));
        tmp_document->paragraph_count = 0;
        tmp_document->data = malloc(sizeof(struct sentence)); //paragraph[0]
        
        
        //iterate over the text
        for(int i=0; i<text_length; i++){ //condense by i<strlen(text)
            char ch = text[i];
            if((ch >= 'a' && ch <= 'z') || 
                (ch >= 'A' && ch <= 'Z')){
                //found letter
                
                //put into word
                append_word(tmp_word, &letters, ch);
            } else {
                //found end of word   
                if(letters>0){     
                    //terminate word string
                    tmp_word->data[letters] = '\0'; 
                    //put word into current sentence     
                    append_sentence(tmp_sentence, tmp_word);           
                    //clean tmp word
                    reset_word(tmp_word, &letters);
                }
    
                //check for end of sentence or paragraph
                if (ch == ' '){ //is word
                
                } else if (ch == '\n'){ 
                    //found end of paragraph
                    //put into document
                    append_document(tmp_document, tmp_paragraph);
                    reset_paragraph(tmp_paragraph);
                }
                else { 
                    //found end of sentence
                    //put into current paragraph
                    append_paragraph(tmp_paragraph, tmp_sentence);
                    reset_sentence(tmp_sentence);
                }
            }
        }
        //end of document
        append_document(tmp_document, tmp_paragraph);
        
        return *tmp_document;
    }
    
    struct paragraph kth_paragraph(struct document Doc, int k) {
        return Doc.data[k-1];
    }
    
    struct sentence kth_sentence_in_mth_paragraph(struct document Doc, int k, int m) { 
        return Doc.data[m-1].data[k-1];
    }
    
    struct word kth_word_in_mth_sentence_of_nth_paragraph(struct document Doc, int k, int m, int n) {
        return Doc.data[n-1].data[m-1].data[k-1];
    }