-
Notifications
You must be signed in to change notification settings - Fork 0
/
Structuring_The_Document
149 lines (121 loc) · 3.92 KB
/
Structuring_The_Document
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5
struct word {
char* data;
};
struct sentence {
struct word* data;
int word_count;//denotes number of words in a sentence
};
struct paragraph {
struct sentence* data ;
int sentence_count;//denotes number of sentences in a paragraph
};
struct document {
struct paragraph* data;
int paragraph_count;//denotes number of paragraphs in a document
};
typedef struct
{
struct word curr_word;
struct sentence curr_sentence;
struct paragraph curr_para;
struct document curr_doc;
} CompountStructType;
void end_word(CompountStructType *p)
{
struct word* curr_word = &p->curr_word;
struct sentence* curr_sentence = &p->curr_sentence;
// Add the current word to the sentence, making space for it - if there is a word
if (curr_word->data)
{
curr_sentence->data = realloc(curr_sentence->data, (1 + curr_sentence->word_count) * sizeof(struct word));
curr_sentence->data[curr_sentence->word_count] = *curr_word;
curr_sentence->word_count++;
// Clear down current word
curr_word->data = NULL;
}
}
void end_sentence(CompountStructType* p)
{
struct sentence* curr_sentence = &p->curr_sentence;
struct paragraph* curr_para = &p->curr_para;
// Add the current sentence to the paragraph, making space for it - if there is a sentence
if (curr_sentence->data)
{
curr_para->data = realloc(curr_para->data, (1 + curr_para->sentence_count) * sizeof(struct sentence));
curr_para->data[curr_para->sentence_count] = *curr_sentence;
curr_para->sentence_count++;
// Clear down current sentence
curr_sentence->data = NULL;
curr_sentence->word_count = 0;
}
}
void end_paragraph(CompountStructType *p)
{
struct paragraph* curr_para = &p->curr_para;
struct document* curr_doc = &p->curr_doc;
// Add the current paragraph to the document, making space for it - if there is a paragraph
if (curr_para->data)
{
curr_doc->data = realloc(curr_doc->data, (1 + curr_doc->paragraph_count) * sizeof(struct paragraph));
curr_doc->data[curr_doc->paragraph_count] = *curr_para;
curr_doc->paragraph_count++;
// Clear down current paragraph
curr_para->data = NULL;
curr_para->sentence_count = 0;
}
}
struct document get_document(char* text) {
CompountStructType data = { 0 };
while (*text != '\0')
{
switch (*text)
{
case '\n':
// End of word, sentence and paragraph
*text = '\0'; // Close off current word
end_word(&data);
end_sentence(&data);
end_paragraph(&data);
break;
case '.':
// End of word and end of sentence.
*text = '\0'; // Close off current word
end_word(&data);
end_sentence(&data);
break;
case ' ':
// End of word
*text = '\0'; // Close off current word
end_word(&data);
break;
default:
if (!data.curr_word.data)
{
// Start current word here since we have text which is not a special character
data.curr_word.data = text;
}
break;
}
text++;
}
// Finish up any word, sentence and paragraph which was pending
end_word(&data);
end_sentence(&data);
end_paragraph(&data);
return data.curr_doc; // return byval
}
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];
}