Structuring the Document

  • + 0 comments

    Here’s a concise answer with the C code for parsing a document into paragraphs, sentences, and words, and handling queries to retrieve specific parts of the document: for more info to solve problems like this visit our site.

    include

    include

    include

    define MAX_PARAGRAPHS 100

    define MAX_SENTENCES 100

    define MAX_WORDS 100

    // Structure Definitions struct word { char* data; };

    struct sentence { struct word* data; int word_count; };

    struct paragraph { struct sentence* data; int sentence_count; };

    struct document { struct paragraph* data; int paragraph_count; };

    struct document Doc;

    // Initialize document void initialize_document(int n) { Doc.paragraph_count = n; Doc.data = (struct paragraph*)malloc(n * sizeof(struct paragraph)); }

    // Parse document text void parse_document(char* text) { char* para_text = strtok(text, "\n"); int para_index = 0;

    while (para_text != NULL) {
        struct paragraph* para = &Doc.data[para_index++];
        para->sentence_count = 0;
        para->data = (struct sentence*)malloc(MAX_SENTENCES * sizeof(struct sentence));
    
        char* sent_text = strtok(para_text, ".");
        int sent_index = 0;
    
        while (sent_text != NULL) {
            struct sentence* sen = &para->data[sent_index++];
            sen->word_count = 0;
            sen->data = (struct word*)malloc(MAX_WORDS * sizeof(struct word));
    
            char* word_text = strtok(sent_text, " ");
            int word_index = 0;
            while (word_text != NULL) {
                sen->data[word_index].data = (char*)malloc((strlen(word_text) + 1) * sizeof(char));
                strcpy(sen->data[word_index].data, word_text);
                sen->word_count++;
                word_text = strtok(NULL, " ");
                word_index++;
            }
            para->sentence_count++;
            sent_text = strtok(NULL, ".");
        }
    
        para_text = strtok(NULL, "\n");
    }
    

    }

    // Query Functions void get_paragraph(int p) { for (int i = 0; i < Doc.data[p - 1].sentence_count; i++) { for (int j = 0; j < Doc.data[p - 1].data[i].word_count; j++) { printf("%s ", Doc.data[p - 1].data[i].data[j].data); } printf(". "); } printf("\n"); }

    void get_sentence(int p, int s) { for (int i = 0; i < Doc.data[p - 1].data[s - 1].word_count; i++) { printf("%s ", Doc.data[p - 1].data[s - 1].data[i].data); } printf("\n"); }

    void get_word(int p, int s, int w) { printf("%s\n", Doc.data[p - 1].data[s - 1].data[w - 1].data); }

    // Main function to process input int main() { int n; scanf("%d\n", &n);

    char document_text[1000] = "";
    for (int i = 0; i < n; i++) {
        char temp[1000];
        fgets(temp, 1000, stdin);
        strcat(document_text, temp);
    }
    
    initialize_document(n);
    parse_document(document_text);
    
    int q;
    scanf("%d\n", &q);
    for (int i = 0; i < q; i++) {
        int type, p, s, w;
        scanf("%d", &type);
    
        if (type == 1) {
            scanf("%d", &p);
            get_paragraph(p);
        } else if (type == 2) {
            scanf("%d %d", &p, &s);
            get_sentence(p, s);
        } else if (type == 3) {
            scanf("%d %d %d", &p, &s, &w);
            get_word(p, s, w);
        }
    }
    
    return 0;
    

    }