-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumTools.c
More file actions
204 lines (170 loc) · 5.32 KB
/
Copy pathsumTools.c
File metadata and controls
204 lines (170 loc) · 5.32 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdlib.h>
#include <stdio.h>
#include "sumTools.h"
#include <string.h>
#include "trees.h"
#include "linkedList.h"
#include "strs.h"
// this used to insert ngrams of words
void insertEach(cell **root, char *nframe, int gram) {
if (root == NULL || nframe == NULL || gram <= 0) {
return;
}
char frame[256];
int len = strlen(nframe);
if (len >= sizeof(frame)) {
len = sizeof(frame) - 1;
}
memcpy(frame, nframe, len);
frame[len] = '\0';
if (len > 0 && frame[len - 1] == ' ') {
frame[len - 1] = '\0';
}
char *word = malloc(256);
word[0] = '\0';
for(int i = 0 ; i < len ; i++){
if(frame[i] != ' ' && frame[i] != '\0'){
word[i] = frame[i];
}else{
insert(root, word);
strcat(word, " ");
}
}
}
// this doesn't took words individual but it took n-grams of the words
//it's helpful to gard the main idea of the paragraph and to make the intersection and the difference more accurate
void popStructNgram(node ** listHead , char * fileLocation , int gram){
node *paragraph = NULL;
cell *paragraphTree = NULL;
FILE *file = fopen(fileLocation, "r");
char line[10000];
char copy[10000];
int numberOfParagraphs = 0 ;
if (file == NULL) {
printf("Failed to open file");
return;
}
while(fgets(line, sizeof(line), file) != NULL) {
if (isBlankLine(line)) {
continue;
}
numberOfParagraphs++;
paragraph = NULL;
paragraphTree = NULL;
strcpy(copy, line);
char *word = strtok(copy, " \t\r\n,.?!;:\"()");
char accumulator[256] = "";
int count = 0;
while (word != NULL) {
toLowerCase(word);
count++;
size_t acc_len = strlen(accumulator);
size_t word_len = strlen(word);
if (acc_len + word_len + 2 >= sizeof(accumulator)) {
break;
}
strcat(accumulator, word);
strcat(accumulator, " ");
if(count == gram){
insertEach(¶graphTree, accumulator , gram);
int i = 0;
while(accumulator[i] != ' ' && accumulator[i] != '\0'){
i++;
}
remove_substring(accumulator, 0, i+1);
count -- ;
}
word = strtok(NULL, " \t\r\n,.?!;:\"()");
}
if(paragraphTree != NULL && paragraphTree->word != NULL){
insertNewParagraph(listHead , paragraph , paragraphTree);
}
}
if(numberOfParagraphs == 0){
node * vide = malloc(sizeof(node));
cell * emptyParagraph = malloc(sizeof(cell));
emptyParagraph->word = malloc(sizeof(char) * 20);
vide->pragraph = emptyParagraph ;
strcpy(vide->pragraph->word, "empty file");
vide->next = NULL ;
insertNewParagraph(listHead, vide , vide->pragraph);
}
fclose(file);
}
//populate the tree with sentences not words
void populSentences(char *fileLocation, node **listHead) {
node *paragraph = NULL;
cell *paragraphTree = NULL;
FILE *file = fopen(fileLocation, "r");
char line[10000];
char copy[10000];
int numberOfParagraphs = 0 ;
if (file == NULL) {
printf("Failed to open file");
return;
}
while(fgets(line, sizeof(line), file) != NULL) {
if (isBlankLine(line)) {
continue;
}
numberOfParagraphs++;
paragraph = NULL;
paragraphTree = NULL;
strcpy(copy, line);
char *word = strtok(copy, "\t\r\n,.?!;:");
while (word != NULL) {
toLowerCase(word);
insert(¶graphTree, word);
word = strtok(NULL, "\t\r\n,.?!;:");
}
if(paragraphTree != NULL && paragraphTree->word != NULL){
insertNewParagraph(listHead , paragraph , paragraphTree);
}
}
if(numberOfParagraphs == 0){
node * vide = malloc(sizeof(node));
cell * emptyParagraph = malloc(sizeof(cell));
emptyParagraph->word = malloc(sizeof(char) * 20);
vide->pragraph = emptyParagraph ;
strcpy(vide->pragraph->word, "empty file");
vide->next = NULL ;
insertNewParagraph(listHead, vide , vide->pragraph);
}
fclose(file);
}
// sums up the frequencies of the words in the sentences
void scoringSentence(cell **sentenecs, cell *words) {
if (sentenecs == NULL || *sentenecs == NULL || words == NULL) {
return;
}
scoringSentence(&(*sentenecs)->leftC, words);
if ((*sentenecs)->word != NULL) {
char tmp[256];
strcpy(tmp, (*sentenecs)->word);
char *word = strtok(tmp, " ");
while (word != NULL) {
cell *hit = getCell(words, word);
if (hit != NULL) {
(*sentenecs)->freq += hit->freq ;
}
word = strtok(NULL, " ");
}
}
scoringSentence(&(*sentenecs)->rightC, words);
}
//extract the most repeated token in the tree
cell * mostRepeated(cell * root){
if(root == NULL){
return NULL ;
}
cell * leftMost = mostRepeated(root->leftC);
cell * rightMost = mostRepeated(root->rightC);
cell * maxCell = root;
if (leftMost != NULL && leftMost->freq > maxCell->freq) {
maxCell = leftMost;
}
if (rightMost != NULL && rightMost->freq > maxCell->freq) {
maxCell = rightMost;
}
return maxCell;
}