Structuring the Document

  • + 0 comments

    struct document get_document(char* text) { struct document doc; doc.data = NULL; doc.paragraph_count = 0;

    struct paragraph para;
    para.data = NULL;
    para.sentence_count = 0;
    
    struct sentence sen;
    sen.data = NULL;
    sen.word_count = 0;
    
    struct word w;
    w.data = NULL;
    
    for (int i = 0; i <= strlen(text); i++) {
        char ch = text[i];
    
        if (ch == ' ') {
            if (w.data != NULL) {
                sen.word_count++;
                sen.data = realloc(sen.data, sen.word_count * sizeof(struct word));
                sen.data[sen.word_count - 1] = w;
                w.data = NULL;
            }
        } else if (ch == '.') {
            if (w.data != NULL) {
                sen.word_count++;
                sen.data = realloc(sen.data, sen.word_count * sizeof(struct word));
                sen.data[sen.word_count - 1] = w;
                w.data = NULL;
            }
    
            para.sentence_count++;
            para.data = realloc(para.data, para.sentence_count * sizeof(struct sentence));
            para.data[para.sentence_count - 1] = sen;
            sen.data = NULL;
            sen.word_count = 0;
        } else if (ch == '\n' || ch == '\0') {
            if (para.sentence_count > 0) {
                doc.paragraph_count++;
                doc.data = realloc(doc.data, doc.paragraph_count * sizeof(struct paragraph));
                doc.data[doc.paragraph_count - 1] = para;
                para.data = NULL;
                para.sentence_count = 0;
            }
        } else {
            int len = (w.data == NULL) ? 0 : strlen(w.data);
            w.data = realloc(w.data, len + 2);
            w.data[len] = ch;
            w.data[len + 1] = '\0';
        }
    }
    
    return doc;
    

    }

    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]; }