Structuring the Document

Sort by

recency

|

126 Discussions

|

  • + 1 comment

    Riegler Transporte is a reliable transportation and logistics company based in Klagenfurt, Austria. Specializing in transportation services and offering high-quality warehouse solutions

  • + 0 comments

    Heart & Soul Whisperer Art Gallery is an online art gallery selling premium, museum-grade Glicee Fine Art Black and White and Colour Photography and abstract artworks. Founded by Cosmetic Dentist, Dr Zenaidy Castro on 2017, principal dentist of Vogue Smiles Melbourne.

    Explore our art and photographic Collections: https://heartandsoulwhisperer.com.au/

  • + 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;
    

    }

  • + 0 comments
    struct document get_document(char* text)
    {
        struct document doc;
        struct paragraph *cur_paragraph = NULL;
        struct sentence *cur_sentence = NULL;
        char *new_word = NULL;
    
        doc.data = NULL;
        doc.paragraph_count = 0;
    
        for (char *s = text; *s; ++s)
        {
            if (*s == ' ' || *s == '.')
            {
                // nouveau paragraphe
                if (cur_paragraph == NULL)
                {
                    doc.paragraph_count++;
                    doc.data = (struct paragraph *) realloc(doc.data, sizeof(struct paragraph) * doc.paragraph_count);
    
                    cur_paragraph = doc.data + doc.paragraph_count - 1;
                    cur_paragraph->data = NULL;
                    cur_paragraph->sentence_count = 0;
    
                    cur_sentence = NULL;        // on recommence de facto une phrase
                }
    
                // nouvelle phrase
                if (cur_sentence == NULL)
                {
                    cur_paragraph->sentence_count++;
                    cur_paragraph->data = (struct sentence *) realloc(cur_paragraph->data, sizeof(struct sentence) * cur_paragraph->sentence_count);
    
                    cur_sentence = cur_paragraph->data + cur_paragraph->sentence_count - 1;
                    cur_sentence->data = NULL;
                    cur_sentence->word_count = 0;
                }
    
                // nouveau mot
                cur_sentence->word_count++;
                cur_sentence->data = (struct word *) realloc(cur_sentence->data, sizeof(struct word) * cur_sentence->word_count);
                cur_sentence->data[cur_sentence->word_count - 1].data = new_word;
                new_word = NULL;
    
                if (*s == '.')
                    cur_sentence = NULL;        // on recommencera une phrase
                *s = 0;
            }
    
            else if (*s == '\n')
            {
                cur_sentence = NULL;
                cur_paragraph = NULL;
            }
            else
            {
                if (new_word == NULL)
                {
                    new_word = s;
                }
            }
        }
    
        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];
    }
    
  • + 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];
    }