-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtp.c
More file actions
931 lines (775 loc) · 24.7 KB
/
Copy pathtp.c
File metadata and controls
931 lines (775 loc) · 24.7 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "trees.h"
#include "linkedList.h"
#include "sumTools.h"
#include "strs.h"
//this treats the blank line as a seperator between paragrahps
//Im using it in the extractiion operation to since my sets are built this way
void populateTheStructure9dima(char *fileLocation, node **listHead) {
node *paragraph = NULL;
cell *paragraphTree = NULL;
FILE *file = fopen(fileLocation, "r");
char line[256];
char copy[256];
if (file == NULL) {
printf("Error: failed to open file.\n");
return;
}
while (fgets(line, sizeof(line), file) != NULL) {
if (isBlankLine(line)) {
continue;
}
paragraph = NULL;
paragraphTree = NULL;
do{
strcpy(copy, line);
char *word = strtok(copy, " \t\r\n,.?!;:\"()");
while (word != NULL) {
toLowerCase(word);
insert(¶graphTree, word);
word = strtok(NULL, " \t\r\n,.?!;:\"()");
}
fgets(line, sizeof(line), file);
} while (!isBlankLine(line) && !feof(file)) ;
if(paragraphTree != NULL){
insertNewParagraph(listHead , paragraph , paragraphTree);
}
}
fclose(file);
}
//this treat each \n as the end of paragraph
void populateTheStructure(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("Error: failed to open file.\n");
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);
}
//check the existence of a word in a tree
int exist(cell* root , char * word , int (*comparision)(const char * a ,const char * b) , char * similarword ){
if(root == NULL ){
return 0 ;
}else{
if(comparision == NULL){
comparision = strcmp ; //this means we will do literal coomparision not similarity based
}
if(comparision(word , root->word ) == 0 ) {
if(similarword != NULL){
strcpy(similarword , root->word );
}
return 1 ;
}else if(strcmp(word , root->word) > 0){
return exist(root->rightC , word , comparision , similarword);
}else{
return exist(root->leftC , word , comparision , similarword);
}
}
}
void insertAll(cell * tree1 , cell ** tree2){
if(tree1 == NULL){
return ;
}else{
insertAll(tree1->leftC , tree2 );
insert(tree2, tree1->word);
insertAll(tree1->rightC , tree2 ) ;
}
return ;
}
void setsUnion(cell * tree1 , cell * tree2 ,cell ** tree3 , int (*comparision)(const char * a ,const char * b) ){ //i made this to contain the return pointer and save the initial states of the two sets
if(tree1 != NULL){
insertAll(tree1 , tree3);
}
if(tree2 != NULL){
insertAll(tree2 , tree3);
}
}
// i build this to take build the intersection from the both sets taking the longest word in the case of similarity based comparision
void setsIntersection(cell * tree1 , cell * tree2 ,cell **tree3 , int (*comparision)(const char * a ,const char * b) ){
char * word = NULL ;
word = malloc(sizeof(char) * 20);
if(tree1 == NULL){
return ;
}else{
setsIntersection(tree1->leftC , tree2 , tree3, comparision);
if(exist(tree2 , tree1->word , comparision, word) ){
if(strlen(word) > strlen(tree1->word)){
insert(tree3 , word);
}else{
insert(tree3,tree1->word);
}
}
setsIntersection(tree1->rightC , tree2 , tree3, comparision);
}
}
// i build this to take build the intersection from the first sets not the longest word as in the first sets interection
void setsIntersection2(cell * tree1 , cell * tree2 ,cell **tree3 , int (*comparision)(const char * a ,const char * b) ){
if(tree1 == NULL){
return ;
}else{
setsIntersection2(tree1->leftC , tree2 , tree3, comparision);
if(exist(tree2 , tree1->word , comparision, NULL) ){
insert(tree3,tree1->word);
}
setsIntersection2(tree1->rightC , tree2 , tree3, comparision);
}
}
void difference(cell * tree1 , cell * tree2 ,cell **tree3 , int (*comparision)(const char * a ,const char * b)){
char * word = NULL ;
word = malloc(sizeof(char) * 20);
if(tree1 == NULL){
return ;
}else{
difference(tree1->leftC , tree2 , tree3 , comparision);
if(!exist(tree2 , tree1->word , comparision, word )){
insert(tree3,tree1->word);
}
difference(tree1->rightC , tree2 , tree3, comparision);
}
}
// i leave this function to gard the int type to do the recusive call condition even when the outer function is of void type
// all of this so i can use it directly in the menu procedure directly
int includeInner(cell * tree1 , cell * tree2 , cell **tree3 , int (*comparision)(const char * a ,const char * b)){
if(tree1 == NULL){
return 1 ;
strcpy((*tree3)->word ,"included" ) ;
}else if (tree1->leftC == NULL && tree1->rightC == NULL){
if(exist(tree2 , tree1->word , comparision , NULL )){
strcpy((*tree3)->word ,"included" );
return 1 ;
}else{
strcpy((*tree3)->word ,"not included");
return 0 ;
}
}else{
if(includeInner(tree1->leftC , tree2, tree3 , comparision ) && includeInner(tree1->rightC , tree2 , tree3 , comparision) && exist(tree2 , tree1->word , comparision , NULL )) {
strcpy((*tree3)->word ,"included" );
return 1 ;
}else{
strcpy((*tree3)->word ,"not included");
return 0 ;
}
}
}
void include(cell * tree1 , cell * tree2 , cell **tree3 , int (*comparision)(const char * a ,const char * b)){
includeInner(tree1 , tree2 , tree3 , comparision);
}
void printMainStructure(node * head){ //print the list of trees
while(head != NULL){
printTree(head->pragraph);
head = next(head);
}
}
// this count the number of files entered by the user
int filesNumber(char * fileNamesString){
int count = 0 ;
for(int i = 0 ; fileNamesString[i] != '\0' ; i++){
if(fileNamesString[i] == ' ' || fileNamesString[i] == '\t' || fileNamesString[i] == '\n'){
count++;
}
}
return count + 1 ;
}
void askforfiles(node *** filesHeader , int mode ){
int n = 0 ;
char fileNamesString[256] ;
int i = 0 ;
if(mode==1){
printf("Enter file paths separated by spaces (absolute or relative):\n");
}
fgets(fileNamesString, 256, stdin);
char * copy = malloc(sizeof(char) * 256);
strcpy(copy , fileNamesString);
char * fileName = malloc(sizeof(char)*50);
char ** list = malloc(sizeof(char * ) * filesNumber(fileNamesString));
fileName = strtok(copy , " \t\n");
while(fileName != NULL){
list[i] = fileName;
fileName = strtok(NULL , " \t\n");
i++;
}
(*filesHeader) = malloc((i + 1) * sizeof(node *)) ; //list of the headers of the linked list of each file
for (int j = 0; j <= i; j++) {
(*filesHeader)[j] = NULL;
}
for(int k = 0 ; k < i ; k++){
populateTheStructure(list[k] , &(*filesHeader)[k]);
}
}
node * getParagraph(node * file , int index ){
int i = 0 ;
while(file != NULL && i < index){
file = file->next ;
i++;
}
return file ;
}
//MENU PROCEDURE
//i build one big procedue because the process of intersection and union and difference is almost the same with a few details that i handle with conditions inside the procedure
void operationSelected(cell ** result , node ** filesHeader , void (*operation)(cell * tree1 , cell * tree2 , cell ** tree3 , int (*comparision)(const char * a ,const char * b)) , int mode ){
int k = 0 ;
int selectedParagraphs = 0 ;
int (*comparisionFun)(const char * a ,const char * b);
char operationName[20];
if(operation == setsIntersection){
strcpy(operationName , "intersection");
}else if(operation == difference){
strcpy(operationName , "difference");
}else if(operation == setsUnion){
strcpy(operationName , "union");
}else if(operation == include){
strcpy(operationName , "inclusion test");
}
int selection ;
selection = 0 ;
while(selection < 1 || selection > 3){
if(operation == include){
selection = 2;
break;
}
printf("Choose how to select paragraphs:\n");
printf("1) Select by order in each file\n");
printf("2) Select by file:number pairs\n");
printf("3) Select all paragraphs in order\n");
scanf("%d" , &selection);
while(getchar() != '\n');
if(selection < 1 || selection > 3){
printf("Invalid selection. Try again.\n");
}
}
if(mode == 1 ){
printf("Choose comparison method:\n");
printf("1) Literal comparison\n");
printf("2) Similarity-based comparison\n");
int comparisionMethod ;
comparisionMethod= 0 ;
while(comparisionMethod < 1 || comparisionMethod > 2){
scanf("%d" , &comparisionMethod);
while(getchar() != '\n');
if(comparisionMethod > 2 || comparisionMethod < 1){
printf("Invalid option. Try again.\n");
}
}
if(comparisionMethod == 1){
comparisionFun = strcmp ; //this means we will do literal coomparision not similarity based
}else if(comparisionMethod == 2){
comparisionFun = similarWords ;
}
}else{
comparisionFun = strcmp; // here we are using the strcmp to hide unecessary details from the user when extracting a set from the other
}
switch (selection)
{
case 1: {
int vide = 0 ;
printf("which paragraphs from which files you want to do the %s on ? \n", operationName);
printf("say y or Y to select a paragraph toinclude in the operation \n");
while(filesHeader[k] != NULL){
node * current = filesHeader[k];
int j = 0 ;
printf("the file %d \n" , k);
while(current != NULL){
printf("Paragraph %d\n" , j);
char answer ;
scanf("%c" , &answer);
while(getchar() != '\n');
if(answer == 'y' || answer == 'Y'){
selectedParagraphs++;
if(selectedParagraphs==1&& k==0){
if(strcmp(current->pragraph->word , "empty file")==0 && operation == setsIntersection){
printf("Intersection result is empty.\n");
*result =filesHeader[k]->pragraph;
break;
}else{
*result = malloc(sizeof(cell));
*result = (current->pragraph) ;
printf("First paragraph selected.\n");
}
}else{
printf("Paragraph selected.\n");
cell * curr = *result ;
*result = NULL ;
if(strcmp(current->pragraph->word , "empty file")==0 && operation == setsIntersection){
printf("Intersection result is empty.\n");
*result = current->pragraph ;
break;
}else{
if(curr != NULL){
operation(curr , current->pragraph , result , comparisionFun);
}
}
}
}
j++ ;
current = current->next ;
}
k++;
if(vide == 1){
break;
}
}
break;
}
case 2: {
printf("Enter paragraphs as file:paragraph (e.g., 0:1), separated by spaces:\n");
char input[256];
fgets(input, 256, stdin);
input[strcspn(input , "\n")] = '\0';
int i = 0;
char copy[256];
int inc = 0 ;
strcpy(copy , input);
char * paragraph = strtok(copy , " ");
int pNumber = 0 ;
int fNumber = 0 ;
int f = 0 ;
cell * first = NULL ;
cell * second = NULL ;
while(paragraph !=NULL){
fNumber = fileNumber(paragraph);
pNumber = paragraphNumber(paragraph);
if(operation != include){
printf("\nParagraph %d of file %d\n" , pNumber , fNumber);
if(getParagraph(filesHeader[fNumber],pNumber) != NULL){
if(getParagraph(filesHeader[fNumber],pNumber)->pragraph != NULL){
printTree(getParagraph(filesHeader[fNumber] , pNumber)->pragraph);
if(*result == NULL ){
*result = malloc(sizeof(cell));
*result = getParagraph(filesHeader[fNumber] , pNumber)->pragraph ;
if(strcmp((*result)->word , "empty file")==0 && operation == setsIntersection){
printf("Intersection result is empty.\n");
break;
}
}
if(f != 0){
cell * curr = *result ;
*result = NULL ;
if(strcmp( getParagraph(filesHeader[fNumber] , pNumber)->pragraph->word , "empty file")==0 && operation == setsIntersection){
printf("Intersection result is empty.\n");
*result = getParagraph(filesHeader[fNumber] , pNumber)->pragraph ;
break;
}else{
if(curr != NULL){
operation(curr , getParagraph(filesHeader[fNumber] , pNumber)->pragraph , result , comparisionFun);
}
}
}
}else{
printf("Paragraph %d in file %d does not exist.\n" , pNumber ,fNumber );
}
}else{
printf("Paragraph %d in file %d does not exist.\n" , pNumber , fNumber );
if(operation == setsIntersection){
printf("Intersection result is empty.\n");
*result = NULL ;
}
break;
}
}else{ // case of inclusion operation
if(getParagraph(filesHeader[fNumber],pNumber) != NULL){
if(inc > 2){
printf("You can select at most 2 paragraphs for inclusion test.\n");
}else{
if(first == NULL){
first = getParagraph(filesHeader[fNumber] , pNumber)->pragraph ;
inc++;
}else{
inc ++;
second = getParagraph(filesHeader[fNumber] , pNumber)->pragraph ;
}
}
operation(first , second , result , comparisionFun);
}else{
printf("Paragraph %d in file %d does not exist.\n" , pNumber , fNumber );
}
}
paragraph = strtok(NULL , " ");
f ++ ;
}
break ;
}
case 3: {
while(filesHeader[k] != NULL){
int j = 0 ;
node * current = filesHeader[k];
while(current != NULL){
selectedParagraphs++;
if(selectedParagraphs==1&& k==0){
*result = malloc(sizeof(cell));
*result = (filesHeader[k]->pragraph) ;
if(strcmp((*result)->word , "empty file")==0 && operation == setsIntersection){
printf("Intersection result is empty.\n");
break;
}
}else{
cell * curr = *result ;
*result = NULL ;
if(strcmp(current->pragraph->word , "empty file")==0 && operation == setsIntersection){
printf("the intersection result is empty\n");
*result = current->pragraph ;
break;
}else{
if(curr != NULL){
operation(curr , current->pragraph , result , comparisionFun);
}
}
}
j++ ;
current = current->next ;
}
k++;
}
break;
}
default:
break;
}
if(mode == 1){
system("clear");
printf("Final %s result:\n", operationName);
if(operation != include){
if(*result == NULL){
printf("Result is empty.\n");
}else{
printTree(*result);
}
}else{
printf("%s " , (*result)->word);
}
printf("\n");
}
}
//this is the same as set intersection but its build to extract from sets ordered alphabitacally to reduce the search time
void checkSets(cell * tree , node * set , cell ** result) {
if(tree == NULL){
return;
}else{
checkSets(tree->leftC , set , result);
char first = tree->word[0];
if(first >= 'A' && first <= 'Z'){
first = (char)(first + ('a' - 'A'));
}
if(first >= 'a' && first <= 'z'){
int number = first - 'a';
node *paragraph = getParagraph(set , number);//our data sets are build in alphabitic order so we can search directlythe corresponding paragraph for the first letter of the word we want to extract from the set
if(paragraph != NULL && paragraph->pragraph != NULL){
if(exist(paragraph->pragraph , tree->word , NULL , NULL)){
insert(result , tree->word);
}
}
}
checkSets(tree->rightC , set , result);
}
}
//this procedure is used in the menu procedure to extract a set of words from the other based on the user selection of the set and the files to extract from and the operation to do on them
void extractingOperation(node *** filesHeader , char * operands){
char *target = malloc(sizeof(char) * 60) ;
strcpy(target , "dataSets/") ;
int i = 0 ;
while(operands[i] != '\0') {
target[i+9] = operands[i];
i++;
}
target[i + 9] = '\0';
cell * unionResultFor = NULL ;
askforfiles(filesHeader , 1 );
if(*filesHeader == NULL){
printf("No files selected.\n");
return ;
}else{
printf("Select paragraphs to extract %s from:\n" , operands);
operationSelected(&unionResultFor, *filesHeader, setsUnion , 2);
node * verbs = NULL ;
populateTheStructure(target , &verbs);
cell * resulting = NULL ;
if(unionResultFor == NULL){
printf("No paragraphs selected.\n");
return ;
}else if(verbs == NULL || verbs->pragraph == NULL ){
printf("The %s set is empty.\n" , operands);
return ;
}else{
if(strcmp(operands , "verbs.txt")==0 || strcmp(operands , "stopWords.txt")==0 ){
checkSets( unionResultFor , verbs , &resulting );
}else{
setsIntersection2(verbs->pragraph , unionResultFor , &resulting , similarWords );
}
}
printf("Extracted %s:\n" , operands);
printTree(resulting);
printf("What next?\n");
}
}
int main(){
int m = 0 ;
do{
node ** filesHeader = NULL ;
printf("=== Document Processing Program ===\n");
printf("Choose an option:\n");
printf("1) Union on selected files\n");
printf("2) Intersection on selected files\n");
printf("3) Difference on selected files\n");
printf("4) Check if a paragraph is included in another\n");
printf("5) Extract special words\n");
printf("6) Summarize file\n");
printf("7) Check plagiarism between two files\n");
printf("8) Exit\n");
m=10 ;
while(m > 8 || m < 1){
printf("Your choice: ");
scanf("%d" , &m);
while(getchar() != '\n');
if(m > 8 || m < 1){
printf("Invalid choice. Try again.\n");
}
}
system("clear");
switch(m){
case 1:
// Union operation
cell * unionResult = NULL ;
askforfiles(&filesHeader , 1 );
if(filesHeader == NULL){
printf("No files selected.\n");
return 0 ;
}else{
operationSelected(&unionResult, filesHeader, setsUnion , 1 );
printf("What next?\n");
}
break;
case 2:
// Intersection operation
cell * intersectionResult = NULL ;
askforfiles(&filesHeader , 1 );
operationSelected(&intersectionResult, filesHeader, setsIntersection , 1);
break;
case 3:
// Difference operation
cell * differenceResult = NULL ;
askforfiles(&filesHeader , 1 );
operationSelected(&differenceResult, filesHeader, difference , 1);
break;
case 4:
// test the inclusion
cell * inclusionRes = NULL ;
inclusionRes = malloc(sizeof(cell));
inclusionRes->word = malloc(sizeof(char) * 20);
askforfiles(&filesHeader , 1 );
operationSelected(&inclusionRes, filesHeader , include , 1);
break;
case 5:
//extract verbs operation
printf("Extract verbs/stop words or a custom set.\n");
printf("1) Extract verbs\n");
printf("2) Extract stop words\n");
printf("3) Extract a custom set\n");
int extractSelection ;
extractSelection = 0 ;
while(extractSelection < 1 || extractSelection > 3){
printf("Your selection: \n");
scanf("%d" , &extractSelection);
while(getchar() != '\n');
if (extractSelection < 1 || extractSelection > 3)
{
printf("Invalid selection. Try again.\n");
}
}
switch(extractSelection){
case 1:
extractingOperation(&filesHeader , "verbs.txt");
break;
case 2:
extractingOperation(&filesHeader , "stopWords.txt");
break;
case 3:
printf("Define a new set in the terminal or from a file in dataSets.\n");
printf("1) Define in terminal\n");
printf("2) Use a file from dataSets\n");
int setDefSelection ;
setDefSelection = 0 ;
while(setDefSelection < 1 || setDefSelection > 2){
printf("Your selection: \n");
scanf("%d" , &setDefSelection);
if (setDefSelection < 1 || setDefSelection > 2)
{
printf("Invalid selection. Try again.\n");
}
}
switch (setDefSelection)
{
case 1://asking for the set items in the terminal write it in a file and extract from it
{
FILE * file = fopen( "dataSets/sets.txt" , "w");
if(file == NULL){
printf("Error: file creation failed.\n");
return 0 ;
}
char item[20] ;
printf("Enter set items one per line; type 'done' to finish.\n");
while(1){
fgets(item , 20 , stdin);
item[strcspn(item , "\n")] = '\0';
if(strcmp(item , "done") == 0){
break;
}
fprintf(file , "%s\n" , item);
}
fclose(file);
extractingOperation(&filesHeader , "sets.txt");
}
break;
case 2:
char setFileName[256];
printf("Enter file name (ending with .txt): ");
fgets( setFileName , 256, stdin);
setFileName[strcspn(setFileName , "\n")] = '\0';
extractingOperation(&filesHeader , setFileName);
break ;
default:
break;
}
break;
default:
printf("Invalid selection.\n");
}
break;
case 6:
printf("Enter path of file to summarize:\n");
int n = 0 ;
char fileNamesString[256] ;
fgets(fileNamesString, 256, stdin);
fileNamesString[strcspn(fileNamesString , "\n")] = '\0';
node * fileBysentences = NULL ;
populSentences(fileNamesString , &fileBysentences);
node * byWords = NULL ;
populateTheStructure(fileNamesString , &byWords);
node * stopWords = NULL ;
node * verbs = NULL ;
populateTheStructure("dataSets/stopWords.txt" , &stopWords);
populateTheStructure("dataSets/verbs.txt" , &verbs);
node * sentHeader = fileBysentences;
node * wordsHeader = byWords;
int index = 0;
while (sentHeader != NULL && wordsHeader != NULL) {
cell * extractedStops = NULL;
cell * extractedVerbs = NULL;
cell * filt= NULL;
cell * filt2 = NULL;
//extract the stop words and verbs
checkSets(paragraph(wordsHeader), stopWords , &extractedStops);
checkSets(paragraph(wordsHeader), verbs , &extractedVerbs);
//remove them
difference(paragraph(wordsHeader), extractedVerbs , &filt, NULL);
difference(filt , extractedStops , &filt2 , NULL);
//give each sentence a score based of the repition of its words in the paragraph
scoringSentence(&sentHeader->pragraph , filt2);
cell * best = mostRepeated(sentHeader->pragraph);
if (best != NULL && best->word != NULL) {
printf("Paragraph %d idea: %s\n" , index , best->word);
} else {
printf("Paragraph %d has no main idea \n" , index);
}
freeTree(extractedVerbs);
freeTree(extractedStops);
freeTree(filt);
freeTree(filt2);
sentHeader = sentHeader->next;
wordsHeader = wordsHeader->next;
index++;
}
freeList(stopWords);
freeList(verbs);
break;
case 7:
// check plagiarism between two files
printf("Enter paths of two files to compare, separated by space:\n");
char input[256];
fgets(input, 256, stdin);
input[strcspn(input , "\n")] = '\0';
char copy[256];
strcpy(copy , input);
char * file1 = strtok(copy , " ");
char * file2 = strtok(NULL , " ");
while(file1 == NULL || file2 == NULL){
printf("Invalid input. Please provide two file paths.\n");
fgets(input, 256, stdin);
input[strcspn(input , "\n")] = '\0';
strcpy(copy , input);
file1 = strtok(copy , " ");
file2 = strtok(NULL , " ");
}
node * file1ngrams = NULL ;
node * file2ngrams = NULL ;
popStructNgram(&file1ngrams , file1 , 2);
popStructNgram(&file2ngrams , file2 , 2);
cell * shared = NULL ;
cell * allFile1 = NULL ;
cell * allFile2 = NULL ;
while(file1ngrams != NULL){
insertAll(file1ngrams->pragraph , &allFile1);
file1ngrams = file1ngrams->next ;
}
while(file2ngrams != NULL){
insertAll(file2ngrams->pragraph , &allFile2);
file2ngrams = file2ngrams->next ;
}
setsIntersection(allFile1 , allFile2 , &shared , NULL);
cell * all = NULL;
setsUnion(allFile1 , allFile2 , &all , NULL);
int sharedCount = countNodes(shared);
int allCount = countNodes(all);
float similarity = (allCount == 0) ? 0 : ((float)sharedCount / allCount) * 100;
printf("Shared bi-grams: %d\n", sharedCount);
printf("Total bi-grams: %d\n", allCount);
printf("Similarity: %.2f%%\n", similarity);
freeTree(shared);
freeTree(all);
freeTree(allFile1);
freeTree(allFile2);
break;
case 8:
printf("Exiting program... Goodbye! \n");
break;
default:
printf("Invalid option.\n");
}
if(filesHeader != NULL){
freeFile(filesHeader);
}
}while(m != 8);
return 1 ;
}