-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchapter.cpp
More file actions
245 lines (210 loc) · 7.83 KB
/
chapter.cpp
File metadata and controls
245 lines (210 loc) · 7.83 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
#include "chapter.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QResource>
#include <QIODevice>
Chapter::Chapter()
{
//This should never be called, and therefore does nothing.
}
Chapter::Chapter(QString name)
{
//Creates a chapter with the dictated name.
//No Note was provided, therefore an empty Note will be created for its mainNote.
chapterName = name;
mainNote = new Note();
}
Chapter::Chapter(QString name, Note* note)
{
//Creates a chapter with the dictated name.
//Designates the given note to be the main Note.
chapterName = name;
mainNote = note;
}
void Chapter::setMainNote(Note* note)
{
mainNote = note;
}
Note* Chapter::getMainNote()
{
return mainNote;
}
QString Chapter::getChapterName(){return chapterName;}
void Chapter::findDifferences(Note* note)
{
//eMNS stands for Edited Main Note Sentences
//eNNS stands for Edited New Note Sentences
QVector<QVector<QString>> eMNS = getMainNote()->getEditedSentences();
QVector<QVector<QString>> eNNS = note->getEditedSentences();
//oMNS stands for Original Main Note Sentences
//oNNS stands for Original New Note Sentences
//These variable names were too long, so I made them into acronyms. - Luis
QVector<QString> oMNS = getMainNote()->getOriginalSentences();
QVector<QString> oNNS = note->getOriginalSentences();
int currentSimilarityPercent = 0;
int largestSimilarityPercent = 0;
int largestSimilarityIndex;
int previousSimilarIndex = eMNS.size()-1;
//Compare all sentences to each other.
for(int newIndex = 0; newIndex < eNNS.size(); newIndex++)
{
QVector<QString> newSentence = eNNS.at(newIndex);
for(int originalIndex = 0; originalIndex < eMNS.size(); originalIndex++)
{
QVector<QString> mainSentence = eMNS.at(originalIndex);
//Account for all cases when comparing these sentences.
currentSimilarityPercent = this->compareSentences(newSentence, mainSentence);
if(currentSimilarityPercent >= largestSimilarityPercent)
{
largestSimilarityPercent = currentSimilarityPercent;
largestSimilarityIndex = originalIndex;
}
}
//Analyze similarity then proceed.
if(largestSimilarityPercent <= 100)
{
if(largestSimilarityPercent >= 90){
//These sentences are far too similar, and should not be considered for changes.
}else if(largestSimilarityPercent < 90 && largestSimilarityPercent >= 30){
//These sentences are in a good range of similarity, and should be considered for changes.
QString originalSentence = oMNS.at(largestSimilarityIndex);
QString newSentence = oNNS.at(newIndex);
//Add these two sentences to a Change object and add that change object to the changeLog.
Change* change = new Change(originalSentence, newSentence, largestSimilarityIndex);
note->addChange(change);
}else if(largestSimilarityPercent < 30){
//These sentences are completely new, or too dissimilar from other sentences to be compared.
//We must append them to the appropriate location within the original text.
//In order to do this, we want to place it right after the sentence most similar to the sentence
//prior to this unique sentence.
QString newSentence = oNNS.at(newIndex);
Change* change = new Change("This is a new entry.", newSentence, previousSimilarIndex+1);
note->addChange(change);
}
}else{
qDebug()<<"Some error ocurred. Similarity was above 100%";
}
//Set a previous similarity index in case the next sentence is an insertion. We can insert it after this index.
previousSimilarIndex = largestSimilarityIndex;
largestSimilarityPercent = 0;
}
}
int Chapter::compareSentences(QVector<QString> sentence1, QVector<QString> sentence2)
{
int percentSimilarity;
int similarWords = 0;
int totalWords;
//Compare words across both sentences to one another.
for(int i = 0; i < sentence1.size(); i++)
{
for(int j = 0; j < sentence2.size(); j++)
{
QString word1 = sentence1.at(i);
QString word2 = sentence2.at(j);
word1 = word1.toLower();
word2 = word2.toLower();
if(word1 == word2 || this->isSynonym(word1, word2) || this->isSynonym(word2, word1))
{
similarWords++;
}
}
}
//Total words should actually equal the word count of the longest sentence.
if(sentence1.size() > sentence2.size())
{
totalWords = sentence1.size();
}else{
totalWords = sentence2.size();
}
percentSimilarity = (similarWords*100/totalWords);
return percentSimilarity;
}
/*
* Compares two words and determines if they are synonyms in the thesaurus reference file.
* A word is not a synonym to itself.
* */
bool Chapter::isSynonym(QString word1, QString word2)
{
QString thesaurus = "";
QFile thesaurusFile(":/resources/thesaurus.txt");
QTextStream in(&thesaurusFile);
thesaurusFile.open(QIODevice::ReadOnly);
thesaurus = in.readAll();
thesaurusFile.close();
bool found = false;
QStringList thesaurusList;
QString synonyms;
QString line; QString word;
QRegExp rx("(\\,|\\:)");
//checks that the word is in the thesaurus
int word1Location = thesaurus.indexOf(word1 + "|");
if(word1Location == -1)
{
return false;
}
//splits the thesaurus file line by line
while(!thesaurusFile.atEnd())
{
line = in.readLine();
thesaurusList.push_back(line);
}
//finds the line where word1 is located
for (int i = 0; i < thesaurusList.length(); i++)
{
word = thesaurusList.at(i).mid(0, word1.length() + 1);
if (word.compare(word1 + "|"))
{
synonyms = thesaurusList.at(i);
}
}
//separates individual synonyms by commas and colons and trims whitespace from beginning and end of each word
QStringList synList = synonyms.split(rx, QString::SkipEmptyParts);
for (int i = 0; i < synList.length(); i++)
{
synList.replaceInStrings(synList.at(i), synList.at(i).trimmed());
}
for (int i = 0; i < synList.length(); i++)
{
//qDebug()<< synList.at(i) << endl;
if (synList.at(i).compare(word2) == 0)
{
found = true;
}
}
if (found)
{
//qDebug()<<word1 <<" and " << word2 << " are synonyms.";
}
else
{
//qDebug()<<word1 <<" and " << word2 << " are NOT synonyms.";
}
return found;
}
void Chapter::mergeNotes(Change* change)
{
Note* mainNote = this->getMainNote();
QVector<QString> originalSentences = mainNote->getOriginalSentences();
QString noPrevSentence = "This is a new entry.";
qDebug()<<mainNote->getOriginalSentences();
//Replace the sentence or make an insertion in a QVector of sentences.
if(change->getOriginalSentence() == noPrevSentence)
{
//This is an insertion.
originalSentences.insert(change->getIndexOfChange(), change->getProposedSentence());
}else{
//This is a replacement.
originalSentences.remove(change->getIndexOfChange());
originalSentences.insert(change->getIndexOfChange(), change->getProposedSentence());
//qDebug()<<change->getProposedSentence();
}
QString amendedText = "";
for(int i = 0; i < originalSentences.size(); i++)
{
amendedText += originalSentences.at(i) + " ";
}
// qDebug()<<mainNote->getText();
mainNote->editNote(amendedText);
mainNote->setOriginalSentence(originalSentences);
}