-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.cpp
873 lines (765 loc) · 25.7 KB
/
dependencies.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ncurses.h>
using namespace std;
struct Character
{
char data;
Character *next;
Character *previous;
Character(char data = '\0')
{
this->data = data;
this->next = nullptr;
this->previous = nullptr;
}
void setNext(Character *next)
{
this->next = next;
}
void setPrevious(Character *previous)
{
this->previous = previous;
}
Character *getNext() const
{
return this->next;
}
Character *getPrevious() const
{
return this->previous;
}
Character &operator=(const Character *other)
{
this->data = other->data;
this->next = nullptr;
this->previous = nullptr;
return *this;
}
Character &operator=(const char other)
{
this->data = other;
return *this;
}
};
struct Line // a list of characters
{
Character *firstChar, *lastChar; // head, tail of list of characters
Line *above; // next of each line node
Line *below; // previous of each line node
Line()
{
this->firstChar = nullptr;
this->lastChar = nullptr;
this->above = nullptr;
this->below = nullptr;
}
int getCharacterCount() const // returns the number of characters in the list
{
int count = 0;
for (Character *current = this->firstChar; current != nullptr; count++, current = current->next);
return count;
}
bool isEmptyLine() const
{
return this->firstChar == nullptr;
}
void clearLineContents()
{
Character *current = this->firstChar;
while (current != nullptr)
{
Character *temp = current;
current = current->next;
delete temp;
}
this->firstChar = nullptr;
this->lastChar = nullptr;
}
void addCharacterAtEnd(Character *newCharacter)
{
if (this->isEmptyLine())
{
this->firstChar = newCharacter;
this->lastChar = newCharacter;
}
else
{
newCharacter->setPrevious(this->lastChar);
this->lastChar->setNext(newCharacter);
this->lastChar = newCharacter;
}
}
void addCharacterInLocation(Character *newCharacter, int characterNumber)
{
if (this->isEmptyLine())
{
this->firstChar = newCharacter;
this->lastChar = newCharacter;
}
else
{
if (characterNumber == 0)
{
newCharacter->setNext(this->firstChar);
this->firstChar->setPrevious(newCharacter);
this->firstChar = newCharacter;
}
else if (characterNumber == this->getCharacterCount())
{
addCharacterAtEnd(newCharacter);
}
else
{
Character *currentChar = this->firstChar;
for (int i = 0; i < characterNumber - 1; i++, currentChar = currentChar->next);
newCharacter->setNext(currentChar->next);
newCharacter->setPrevious(currentChar);
currentChar->next->setPrevious(newCharacter);
currentChar->next = newCharacter;
}
}
}
void replaceCharactersInLine(Line *charactersToReplace, int wordLength, int positionTillWord)
{
// going to the point where the word starts
Character *currentChar = firstChar;
for (int i = 0; i < positionTillWord; i++)
currentChar = currentChar->next;
// replacing the characters
for (int i = 0; i < wordLength; i++, currentChar = currentChar->next)
currentChar->data = (*charactersToReplace)[i].data;
}
void deleteCharacter(int characterNumber)
{
if (this->isEmptyLine())
{
cout << "Line is empty";
return;
}
if (characterNumber < 0 || characterNumber > this->getCharacterCount())
{
cout << "Invalid character number";
return;
}
if (this->getCharacterCount() == 1)
{
delete this->firstChar;
this->firstChar = nullptr;
this->lastChar = nullptr;
}
else
{
if (characterNumber == 0)
{
Character *temp = this->firstChar;
this->firstChar = this->firstChar->next;
this->firstChar->previous = nullptr;
delete temp;
}
else if (characterNumber == this->getCharacterCount() - 1)
{
Character *temp = this->lastChar;
this->lastChar = this->lastChar->previous;
this->lastChar->next = nullptr;
delete temp;
}
else
{
Character *currentChar = this->firstChar;
for (int i = 0; i < characterNumber; i++, currentChar = currentChar->next);
currentChar->previous->next = currentChar->next;
currentChar->next->previous = currentChar->previous;
delete currentChar;
}
}
}
void addWordToLine(Line *word, int positionToAddTo)
{
// going to the point where the word starts
Character *currentChar = firstChar;
for (int i = 0; i < positionToAddTo; i++)
currentChar = currentChar->next;
// adding the characters
for (int i = 0; i < word->getCharacterCount(); i++)
{
if (currentChar == nullptr)
this->addCharacterAtEnd(new Character((*word)[i].data));
else
this->replaceCharactersInLine(word, word->getCharacterCount(), positionToAddTo);
currentChar = currentChar->next;
}
}
vector<char> toVector() const
{
vector<char> line;
Character* currentChar = firstChar;
while (currentChar != nullptr)
{
line.push_back(currentChar->data);
currentChar = currentChar->next;
}
return line;
}
Character &operator[](const int characterNumber)
{
Character *current = this->firstChar;
for (int i = 0; i < characterNumber; i++, current = current->next);
return *current;
}
bool operator==(const Line &otherLine) const
{
if (this->getCharacterCount() != otherLine.getCharacterCount())
return false;
Character *current = this->firstChar;
Character *otherCurrent = otherLine.firstChar;
for (int i = 0; i < this->getCharacterCount(); i++, current = current->next, otherCurrent = otherCurrent->next)
{
if (current->data != otherCurrent->data)
return false;
}
return true;
}
bool operator==(const vector<char> vectorLine) const
{
if (this->getCharacterCount() != vectorLine.size())
return false;
Character *current = this->firstChar;
for (int i = 0; i < this->getCharacterCount(); i++, current = current->next)
{
if (current->data != vectorLine[i])
return false;
}
return true;
}
};
void reverseWord(Line* word)
{
Character *current = word->firstChar;
Character *temp = nullptr;
// Swap next and prev for all nodes
while (current != nullptr) {
temp = current->previous;
current->previous = current->next;
current->next = temp;
current = current->previous;
}
// Change head
if (temp != nullptr)
word->firstChar = temp->previous;
}
int compare(const vector<char> &a, const vector<char> &b)
{
for (int i = 0; i < min(a.size(), b.size()); i++)
{
if (a[i] < b[i])
return -1;
if (a[i] > b[i])
return 1;
}
if (a.size() < b.size())
return -1;
if (a.size() > b.size())
return 1;
return 0;
}
bool binarySearch(const vector<vector<char> >& dictionary, const vector<char> &wordToFind)
{
int left = 0;
int right = dictionary.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int cmp = compare(dictionary[mid], wordToFind);
if (cmp == 0) {
return true;
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
class ListofLines
{
Line *firstLine, *lastLine; // head, tail of list of lines
public:
ListofLines()
{
this->firstLine = nullptr;
this->lastLine = nullptr;
}
void addLine()
{
Line *newLine = new Line();
if (this->isEmptyList())
{
this->firstLine = newLine;
this->lastLine = newLine;
}
else
{
newLine->above = this->lastLine;
this->lastLine->below = newLine;
this->lastLine = newLine;
}
}
void addLine(Line *newLine)
{
if (this->isEmptyList())
{
this->firstLine = newLine;
this->lastLine = newLine;
}
else
{
newLine->above = this->lastLine;
this->lastLine->below = newLine;
this->lastLine = newLine;
}
}
int getLineCount() const
{
int count = 0;
for (Line *current = this->firstLine; current != nullptr; count++, current = current->below);
return count;
}
bool isEmptyList() const
{
return this->firstLine == nullptr;
}
void addDataToLine(int lineNumber, int characterNumber, char data)
{
if (lineNumber < 0 || lineNumber >= this->getLineCount())
{
cout << "Invalid line number";
return;
}
Line *current = this->firstLine;
for (int i = 0; i < lineNumber; i++, current = current->below);
if (characterNumber < 0 || characterNumber > current->getCharacterCount())
{
cout << "Invalid character number";
return;
}
Character *newCharacter = new Character(data);
if (current->isEmptyLine())
{
current->firstChar = newCharacter;
current->lastChar = newCharacter;
}
else
{
if (characterNumber == 0)
{
newCharacter->next = current->firstChar;
current->firstChar->previous = newCharacter;
current->firstChar = newCharacter;
}
else if (characterNumber == current->getCharacterCount())
{
newCharacter->previous = current->lastChar;
current->lastChar->next = newCharacter;
current->lastChar = newCharacter;
}
else
{
Character *currentChar = current->firstChar;
for (int i = 0; i < characterNumber - 1; i++, currentChar = currentChar->next);
newCharacter->next = currentChar->next;
newCharacter->previous = currentChar;
currentChar->next->previous = newCharacter;
currentChar->next = newCharacter;
}
}
}
void replaceDataInLine(int lineNumber, int characterNumber, char data)
{
if (lineNumber < 0 || lineNumber >= this->getLineCount())
{
cout << "Invalid line number";
return;
}
Line *current = this->firstLine;
for (int i = 0; i < lineNumber; i++, current = current->below);
if (characterNumber < 0 || characterNumber >= current->getCharacterCount())
{
cout << "Invalid character number";
return;
}
Character *currentChar = current->firstChar;
for (int i = 0; i < characterNumber; i++, currentChar = currentChar->next);
currentChar->data = data;
}
void deleteLine(int lineNumber)
{
Line *current = this->firstLine;
for (int i = 0; i < lineNumber && current != nullptr; i++, current = current->below);
if (current != nullptr)
{
if (current->above == nullptr) // first line
this->firstLine = current->below;
else
current->above->below = current->below;
if (current->below == nullptr) // last line
this->lastLine = current->above;
else
current->below->above = current->above;
current->clearLineContents();
delete current;
}
}
void deleteCharacterFromLine(int lineNumber, int characterNumber)
{
if (this->isEmptyList())
{
cout << "List is empty";
return;
}
if (lineNumber < 0 || lineNumber >= this->getLineCount())
{
cout << "Invalid line number";
return;
}
Line *current = this->firstLine;
for (int i = 0; i < lineNumber; i++, current = current->below);
if (characterNumber < 0 || characterNumber > current->getCharacterCount())
{
cout << "Invalid character number";
return;
}
if (current->isEmptyLine())
{
cout << "Line is empty";
return;
}
if (current->getCharacterCount() == 1)
{
delete current->firstChar;
current->firstChar = nullptr;
current->lastChar = nullptr;
}
else
{
if (characterNumber == 0)
{
Character *temp = current->firstChar;
current->firstChar = current->firstChar->next;
current->firstChar->previous = nullptr;
delete temp;
}
else if (characterNumber == current->getCharacterCount())
{
Character *temp = current->lastChar;
current->lastChar = current->lastChar->previous;
current->lastChar->next = nullptr;
delete temp;
}
else
{
Character *currentChar = current->firstChar;
for (int i = 0; i < characterNumber; i++, currentChar = currentChar->next);
currentChar->previous->next = currentChar->next;
currentChar->next->previous = currentChar->previous;
delete currentChar;
}
}
}
void saveToFile() const
{
ofstream file("save.txt", ios::out | ios::trunc);
Line *current = this->firstLine;
while (current != nullptr)
{
Character *currentChar = current->firstChar;
while (currentChar != nullptr)
{
file << currentChar->data; // writing each character to the file
currentChar = currentChar->next;
}
if (current->below != nullptr)
file << endl;
current = current->below;
}
file.close();
}
void clearListContents()
{
Line *current = this->firstLine;
while (current != nullptr)
{
Character *currentChar = current->firstChar;
while (currentChar != nullptr)
{
Character *temp = currentChar;
currentChar = currentChar->next;
delete temp;
}
Line *temp = current;
current = current->below;
delete temp;
}
this->firstLine = nullptr;
this->lastLine = nullptr;
}
void readFromFile()
{
ifstream file("save.txt");
if (!file.is_open())
{
cout << "File not found";
return;
}
string line;
clearListContents();
while (getline(file, line))
{
this->addLine();
for (int i = 0; i < line.length(); i++)
this->addDataToLine(this->getLineCount() - 1, i, line[i]);
}
if (this->getLineCount() == 0)
this->addLine();
file.close();
}
void printList() const
{
int lineNumber, characterNumber;
Line *current = this->firstLine;
for (lineNumber = 0; current != nullptr; lineNumber++)
{
Character *currentChar = current->firstChar;
for (characterNumber = 0; currentChar != nullptr; characterNumber++)
{
addch(currentChar->data);
currentChar = currentChar->next;
}
current = current->below;
if (current != nullptr)
printw("\n");
else
break;
}
}
void printCorrectWordsList(int terminalHeight) const
{
int wordNumber, characterNumber;
Line *current = this->firstLine;
for (wordNumber = 0; current != nullptr; wordNumber++)
{
Character *currentChar = current->firstChar;
for (characterNumber = 0; currentChar != nullptr; characterNumber++)
{
addch(currentChar->data);
currentChar = currentChar->next;
}
current = current->below;
if (current != nullptr)
printw(" ");
else
break;
}
}
Line &operator[](const int lineNumber) const
{
if (lineNumber < 0 || lineNumber >= this->getLineCount())
{
cout << "Invalid line number";
return *(this->firstLine);
}
Line *current = this->firstLine;
for (int i = 0; i < lineNumber; i++, current = current->below);
return *current;
}
bool attemptLetterSubstitution(vector<vector<char> > dictionary, int wordLength, Line *word, ListofLines &correctedWords)
{
int wordsAdded = 0;
// creating a copy of the word
Line *newWord = new Line();
for (int i = 0; i < wordLength; i++)
newWord->addCharacterAtEnd(new Character((*word)[i].data));
// looping through each character in the word
for (int characterInWord = 0; characterInWord < wordLength; characterInWord++)
{
char originalCharacter = (*newWord)[characterInWord].data;
// looping through each character in the alphabet
for (int letter = 97; letter < 123; letter++)
{
if (letter == originalCharacter)
continue;
// replacing jth character in the word with k
(*newWord)[characterInWord] = letter; // using string-like indexing, overloaded operators for Line
// if the word is found in the dictionary
if (binarySearch(dictionary, newWord->toVector()))
{
for (int correctedWordsIndex = 0; correctedWordsIndex < wordsAdded; correctedWordsIndex++)
{
// if the word is already in the list of corrected words
if (correctedWords[correctedWordsIndex] == *newWord)
continue;
}
// creating a copy of the new word to store
Line *wordToStore = new Line();
for (int i = 0; i < wordLength; i++)
wordToStore->addCharacterAtEnd(new Character((*newWord)[i].data));
correctedWords.addLine(wordToStore);
wordsAdded++;
}
}
// restoring the original character
(*newWord)[characterInWord] = originalCharacter;
}
if (wordsAdded > 0)
return true;
else
return false;
}
bool attemptLetterOmmission(vector<vector<char> > dictionary, int wordLength, Line *word, ListofLines &correctedWords)
{
int wordsAdded = 0;
// creating a copy of the word
Line *newWord = new Line();
for (int i = 0; i < wordLength; i++)
newWord->addCharacterAtEnd(new Character((*word)[i].data));
// looping through each character in the word
for (int characterInWord = 0; characterInWord < wordLength; characterInWord++)
{
// removing jth character in the word
newWord->deleteCharacter(characterInWord);
// if the word is found in the dictionary
if (binarySearch(dictionary, newWord->toVector()))
{
for (int correctedWordsIndex = 0; correctedWordsIndex < wordsAdded;correctedWordsIndex++)
{
// if the word is already in the list of corrected words
if (correctedWords[correctedWordsIndex] == *newWord)
continue;
}
// creating a copy of the new word to store
Line *wordToStore = new Line();
for (int i = 0; i < newWord->getCharacterCount(); i++)
wordToStore->addCharacterAtEnd(new Character((*newWord)[i].data));
correctedWords.addLine(wordToStore);
wordsAdded++;
}
// restoring the original character
newWord->addCharacterInLocation(new Character((*word)[characterInWord].data), characterInWord);
}
if (wordsAdded > 0)
return true;
else
return false;
}
bool attemptLetterInsertion(vector<vector<char> > dictionary, int wordLength, Line *word, ListofLines &correctedWords)
{
int wordsAdded = 0;
// creating a copy of the word
Line *newWord = new Line();
for (int i = 0; i < wordLength; i++)
newWord->addCharacterAtEnd(new Character((*word)[i].data));
// looping through each character in the word
for (int characterInWord = 0; characterInWord < wordLength + 1; characterInWord++)
{
// inserting jth character in the word
newWord->addCharacterInLocation(new Character(' '), characterInWord);
// looping through each character in the alphabet
for (int letter = 97; letter < 123; letter++)
{
// inserting jth character in the word
(*newWord)[characterInWord] = letter; // using string-like indexing, overloaded operators for Line
if (binarySearch(dictionary, newWord->toVector()))
{
for (int correctedWordsIndex = 0; correctedWordsIndex < wordsAdded; correctedWordsIndex++)
{
// if the word is already in the list of corrected words
if (correctedWords[correctedWordsIndex] == *newWord)
continue;
}
// creating a copy of the new word to store
Line *wordToStore = new Line();
for (int i = 0; i < wordLength + 1; i++)
wordToStore->addCharacterAtEnd(new Character((*newWord)[i].data));
correctedWords.addLine(wordToStore);
wordsAdded++;
}
}
// restoring the original character
newWord->deleteCharacter(characterInWord);
}
if (wordsAdded > 0)
return true;
else
return false;
}
bool attemptLetterReversal(vector<vector<char> > dictionary, int wordLength, Line *word, ListofLines &correctedWords)
{
int wordsAdded = 0;
// creating a copy of the word
Line *newWord = new Line();
for (int i = 0; i < wordLength; i++)
newWord->addCharacterAtEnd(new Character((*word)[i].data));
// looping through each character in the word
for (int characterInWord = 0; characterInWord < wordLength - 1; characterInWord++)
{
reverseWord(newWord);
// if the word is found in the dictionary
if (binarySearch(dictionary, newWord->toVector()))
{
bool isAlreadyStored = false;
int currentCount = correctedWords.getLineCount();
for (int correctedWordsIndex = 0; correctedWordsIndex < currentCount; correctedWordsIndex++)
{
// if the word is already in the list of corrected words
if (correctedWords[correctedWordsIndex] == *newWord)
isAlreadyStored = true;
}
if (!isAlreadyStored)
{ // creating a copy of the new word to store
Line *wordToStore = new Line();
for (int i = 0; i < wordLength; i++)
wordToStore->addCharacterAtEnd(new Character((*newWord)[i].data));
correctedWords.addLine(wordToStore);
wordsAdded++;
}
}
// reverting
reverseWord(newWord);
}
if (wordsAdded > 0)
return true;
else
return false;
}
};
void toLowerCase(vector<vector<char> >& dictionary)
{
for (int i = 0; i < dictionary.size(); i++)
{
if (!dictionary[i].empty())
{
if (dictionary[i][0] >= 'A' && dictionary[i][0] <= 'Z')
dictionary[i][0] = dictionary[i][0] + ' ';
}
}
}
vector<vector<char> > readDictionary()
{
vector<vector<char> > dictionaryWords;
ifstream file("dictionary.txt");
string word;
while (file >> word)
{
vector<char> wordVector;
for (int i = 0; i < word.length(); i++)
wordVector.push_back(word[i]);
dictionaryWords.push_back(wordVector);
}
toLowerCase(dictionaryWords);
return dictionaryWords;
}
bool isCorrectlySpelt(vector<vector<char> > dictionary, Line word)
{
return binarySearch(dictionary, word.toVector());
}