Structuring the Document

A document is represented as a collection paragraph, a paragraph is represented as a collection of sentences, a sentence is represented as a collection of words and a word is represented as a collection of lower-case ([a-z]) and upper-case ([A-Z]) English characters. You will convert a raw text document into its component paragraphs, sentences and words. To test your results, queries will ask you to return a specific paragraph, sentence, or word as described below.

Alicia is studying the C programming language at the University of Dunkirk and she represents the words, sentences, paragraphs, and documents using pointers:

Example:

Input:

2
Learning C is fun.
Learning pointers is more fun.It is good to have pointers.
3
1 2
2 1 1
3 1 1 1

Output:

Learning pointers is more fun.It is good to have pointers.
Learning C is fun
Learning

Approach:

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5

struct word {
    chardata;
};

struct sentence {
    struct worddata;
    int word_count;
};

struct paragraph {
    struct sentencedata  ;
    int sentence_count;
};

struct document {
    struct paragraphdata;
    int paragraph_count;
};
void add_char(struct word_wordchar character)
{
    static int size;

    if (_word->data == NULL)
    {
        _word->data = (char*)malloc(0);
        size = 2;
    }

    _word->data = (char*)realloc(_word->datasize * sizeof(char));
    _word->data[size - 2] = character;
    _word->data[size - 1] = 0;

    size++;
}

void add_word(struct sentence_sentencestruct word_word)
{
    if (_sentence->data == NULL)
    {
        _sentence->data = (struct word*)malloc(0);
        _sentence->word_count = 0;
    }

    _sentence->word_count++;
    _sentence->data = (struct word*)realloc(_sentence->data
_sentence->word_count * sizeof(struct word));
    _sentence->data[_sentence->word_count - 1] = *_word;
    _word->data = NULL;
}

void add_sentence(struct paragraph_paragraphstruct sentence_sentence)
{
    if (_paragraph->data == NULL)
    {
        _paragraph->data = (struct sentence*)malloc(0);
        _paragraph->sentence_count = 0;
    }

    _paragraph->sentence_count++;
    _paragraph->data = (struct sentence*)realloc(_paragraph->data
_paragraph->sentence_count * sizeof(struct sentence));
    _paragraph->data[_paragraph->sentence_count - 1] = *_sentence;
    _sentence->data = NULL;
}

void add_paragraph(struct document_documentstruct paragraph_paragraph)
{
    if (_document->data == NULL)
    {
        _document->data = (struct paragraph*)malloc(0);
        _document->paragraph_count = 0;
    }

    _document->paragraph_count++;
    _document->data = (struct paragraph*)realloc(_document->data,
 _document->paragraph_count * sizeof(struct paragraph));
    _document->data[_document->paragraph_count - 1] = *_paragraph;
    _paragraph->data = NULL;
}
struct document get_document(chartext) {
 struct document _document;
    struct paragraph _paragraph;
    struct sentence _sentence;
    struct word _word;

    _document.data = NULL;
    _paragraph.data = NULL;
    _sentence.data = NULL;
    _word.data = NULL;

    for (unsigned int i = 0i <= strlen(text); i++)
    {
        switch (text[i])
        {
        case ' ':
            add_word(&_sentence, &_word);
            break;

        case '.':
            add_word(&_sentence, &_word);
            add_sentence(&_paragraph, &_sentence);
            break;

        case '\n':
        case '\0':
            add_paragraph(&_document, &_paragraph);
            break;

        default:
            add_char(&_wordtext[i]);
            break;
        }
    }

    return _document;

}

struct word kth_word_in_mth_sentence_of_nth_paragraph(struct document Doc
int kint mint n) {
 return Doc.data[n - 1].data[m - 1].data[k - 1];
}

struct sentence kth_sentence_in_mth_paragraph(struct document Doc,
 int kint m) { 
 return Doc.data[m - 1].data[k - 1];
}

struct paragraph kth_paragraph(struct document Docint k) {
return Doc.data[k - 1];
}


void print_word(struct word w) {
    printf("%s"w.data);
}

void print_sentence(struct sentence sen) {
    for(int i = 0i < sen.word_counti++) {
        print_word(sen.data[i]);
        if (i != sen.word_count - 1) {
            printf(" ");
        }
    }
}

void print_paragraph(struct paragraph para) {
    for(int i = 0i < para.sentence_counti++){
        print_sentence(para.data[i]);
        printf(".");
    }
}

void print_document(struct document doc) {
    for(int i = 0i < doc.paragraph_counti++) {
        print_paragraph(doc.data[i]);
        if (i != doc.paragraph_count - 1)
            printf("\n");
    }
}

charget_input_text() {    
    int paragraph_count;
    scanf("%d", &paragraph_count);

    char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
    memset(doc0sizeof(doc));
    getchar();
    for (int i = 0i < paragraph_counti++) {
        scanf("%[^\n]%*c"p[i]);
        strcat(docp[i]);
        if (i != paragraph_count - 1)
            strcat(doc"\n");
    }

    charreturnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
    strcpy(returnDocdoc);
    return returnDoc;
}

int main() 
{
    chartext = get_input_text();
    struct document Doc = get_document(text);

    int q;
    scanf("%d", &q);

    while (q--) {
        int type;
        scanf("%d", &type);

        if (type == 3){
            int kmn;
            scanf("%d %d %d", &k, &m, &n);
            struct word w = kth_word_in_mth_sentence_of_nth_paragraph(Doc,
 kmn);
            print_word(w);
        }

        else if (type == 2) {
            int km;
            scanf("%d %d", &k, &m);
            struct sentence senkth_sentence_in_mth_paragraph(Dockm);
            print_sentence(sen);
        }

        else{
            int k;
            scanf("%d", &k);
            struct paragraph para = kth_paragraph(Dock);
            print_paragraph(para);
        }
        printf("\n");
    }     
}


No comments:

Post a Comment