Structuring the Document

  • + 0 comments
    #define PARAGRAPH_COUNT     d.paragraph_count
    #define SENTENCE_COUNT      d.data[PARAGRAPH_COUNT].sentence_count
    #define WORD_COUNT          d.data[PARAGRAPH_COUNT].data[SENTENCE_COUNT].word_count
    
    #define PARAGRAPH_DATA      d.data[PARAGRAPH_COUNT].data
    #define SENTENCE_DATA       PARAGRAPH_DATA[SENTENCE_COUNT].data
    #define WORD_DATA           SENTENCE_DATA[WORD_COUNT].data
    
    struct document get_document(char* text) {
    
        struct document d = {0};
        d.data = calloc(MAX_PARAGRAPHS , sizeof(struct paragraph));
        d.data[0].data = calloc(((MAX_CHARACTERS/4)/2) , sizeof(struct sentence));
        d.data[0].data[0].data = calloc((MAX_CHARACTERS/4) , sizeof(struct word));
        
        char * ptr = text;
        int s_index = 0 , current_index = 0;
        
        while (*ptr != '\0') 
        {
            if( *ptr == ' ')
            {
                *ptr = '\0';
                if(strlen(text+s_index) > 0)
                {
                    WORD_DATA = calloc(strlen(text+s_index) , sizeof(char));
                    WORD_DATA = (text+s_index);
                    WORD_COUNT++;
                }
                s_index = current_index+1;
            }  
    
            else if (*ptr == '.') {
                *ptr = '\0';
                if(strlen(text+s_index) > 0)
                {
                    WORD_DATA = calloc(strlen(text+s_index) , sizeof(char));
                    WORD_DATA = (text+s_index);
                    WORD_COUNT++;
                    
                    SENTENCE_COUNT++;
                    SENTENCE_DATA = calloc(((MAX_CHARACTERS/4)/2) , sizeof(struct word));                
                }
                s_index = current_index+1;    
            }
            else if (*ptr == '\n') {
                *ptr = '\0';
    
                PARAGRAPH_COUNT++;
                PARAGRAPH_DATA = calloc(MAX_PARAGRAPHS , sizeof(struct sentence));
                SENTENCE_DATA = calloc(((MAX_CHARACTERS/4)/2) , sizeof(struct word));
                s_index = current_index+1;            
            }
            
            ptr++;
            current_index++;
        }
    
        PARAGRAPH_COUNT++;
        return d;
    }
    
    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];
    }
    
    struct sentence kth_sentence_in_mth_paragraph(struct document Doc, int k, int m) { 
        return Doc.data[m-1].data[k-1];
    }
    
    struct paragraph kth_paragraph(struct document Doc, int k) {
        return Doc.data[k-1];
    }