-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
executable file
·359 lines (341 loc) · 11.4 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_wizard.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include "tokenizer.h"
#include "verbdetector.h"
#include "compvector.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->lstInputFiles, SIGNAL(currentRowChanged(int)), this, SLOT(inputFileIndexChanged(int)));
browserEmptyHtml = ui->txtBrsInputReader->toHtml();
//
QFile LangDataFile(NOUN_AFFIX_FILE_ADDRESS);
LangDataFile.open(QFile::ReadOnly);
QTextStream Stream(&LangDataFile);
Stream.setAutoDetectUnicode(true);
lang_an = Stream.readLine().trimmed();
lang_at = Stream.readLine().trimmed();
lang_ati = Stream.readLine().trimmed();
lang_ha = Stream.readLine().trimmed();
lang_haii = Stream.readLine().trimmed();
lang_ii = Stream.readLine().trimmed();
lang_ini = Stream.readLine().trimmed();
lang_in = Stream.readLine().trimmed();
lang_yii = Stream.readLine().trimmed();
LangDataFile.close();
QFile NounsFile(NOUNS_FILE_ADDRESS);
NounsFile.open(QFile::ReadOnly);
QTextStream Stream2(&NounsFile);
Stream2.setAutoDetectUnicode(true);
while(!Stream2.atEnd()){
Lang_Nouns.insert(Stream2.readLine().trimmed());
}
NounsFile.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_action_2_triggered()
{
}
void MainWindow::on_actionAddFilesToInput_triggered()
{
//Show a dialog box and read from file
QFileDialog Dialog;
QStringList List = Dialog.getOpenFileNames(this,"Open");
if(List.length() > 0){
foreach(QString Path, List){
if(inputFileNames.contains(Path)){
//Error
QMessageBox Box;
Box.setText("File already added!");
Box.exec();
}else if(!QFile::exists(Path)){
QMessageBox Box;
Box.setText("File not found!");
Box.exec();
}else{
QFile File(Path);
File.open(QFile::ReadOnly);
QTextStream Stream(&File);
Stream.setAutoDetectUnicode(true);
inputFileContents.append(Stream.readAll());
inputFileNames.append(Path);
File.close();
updateList();
}
}
}
}
void MainWindow::inputFileIndexChanged(int _index)
{
if(_index >= 0 ){
ui->btnInputDeleteFile->setEnabled(true);
qDebug("Index Changed.");
ui->txtBrsInputReader->setText(inputFileContents.at(_index));
ui->txtBrsInputReader->setEnabled(true);
}
}
void MainWindow::updateList()
{
int currentSelected = ui->lstInputFiles->currentIndex().row();
ui->lstInputFiles->clear();
foreach(QString FileName, inputFileNames){
ui->lstInputFiles->addItem(FileName);
}
if(currentSelected >= 0 && currentSelected < ui->lstInputFiles->count()){
ui->lstInputFiles->setCurrentRow(currentSelected);
ui->btnInputDeleteFile->setEnabled(true);
}else{
ui->btnInputDeleteFile->setEnabled(false);
ui->txtBrsInputReader->setHtml(browserEmptyHtml);
ui->txtBrsInputReader->setEnabled(false);
}
}
void MainWindow::on_actionDeleteFileFromInput_triggered()
{
int currentSelected = ui->lstInputFiles->currentRow();
if(currentSelected >= 0 && currentSelected < ui->lstInputFiles->count()){
inputFileNames.removeAt(currentSelected);
inputFileContents.removeAt(currentSelected);
updateList();
}
}
void MainWindow::on_actionProcess_triggered()
{
//Load Files
QSet<QString>* PrepositionsSet = loadIntoHash(PREPOSITION_FILE_ADDRESS);
QSet<QString>* CommonWordsSet = loadIntoHash(COMMON_WORDS_FILE_ADDRESS);
QSet<QString> EffectiveWords;
//Statistics
int VerbCount = 0;
int CommonWordCount = 0;
int PrepositionCount = 0;
int WordCount = 0;
int LetterCount = 0;
int SentenceCount = 0;
int EffectiveWordCount = 0;
VerbDetector vDetector;
QFile WWW("fff.fff");
WWW.open(QFile::WriteOnly);
foreach(QString Doc, inputFileContents){
Tokenizer tokenizer(Doc);
while(tokenizer.hasMoreTokens()){
int Steps = vDetector.IsVerb(tokenizer);
if(Steps > 0){
VerbCount++;
for(int i = 0 ; i < Steps ; i++){
LetterCount += tokenizer.nextToken().length();
}
}else{
QString Token = tokenizer.nextToken();
LetterCount += Token.length();
Token = NormalizeToken(Token);
if(Token.length() == 0){
continue;
}
if(PrepositionsSet->contains(Token)){
PrepositionCount++;
}else if(CommonWordsSet->contains(Token)){
CommonWordCount++;
}else{
if(!EffectiveWords.contains(Token)){
WWW.write((Token).toUtf8());
WWW.write("\n");
EffectiveWordCount++;
EffectiveWords.insert(Token);
}
}
}
WordCount++;
}
}
WWW.close();
ui->lblStatVerbCount->setText(QString::number(VerbCount));
ui->lblStatPrepCount->setText(QString::number(PrepositionCount));
ui->lblStatCommonWordCount->setText(QString::number(CommonWordCount));
ui->lblStatEffectiveWordCount->setText(QString::number(EffectiveWordCount));
ui->lblStatWordCount->setText(QString::number(WordCount));
ui->lblStatLetterCount->setText(QString::number(LetterCount));
ui->lblStatTotalUselessWordCount->setText(QString::number(VerbCount + PrepositionCount + CommonWordCount));
delete PrepositionsSet;
delete CommonWordsSet;
QList<QString> FinalWords = EffectiveWords.toList();
//Read and Encode Sentences
QHash<int, QString> Sentencetable;
QList<CompVector> Sentences = ReadAndEncodeSentences(FinalWords, Sentencetable);
ui->lblStatSentenceCount->setText(QString::number(Sentences.length()));
QList<QList<CompVector> > Paragraphs = ConstructParagraphs(Sentences);
qDebug("Num Paragraphs: %d", Paragraphs.count());
//Decode Sentences
FinalResult.clear();
foreach(QList<CompVector> Paragraph, Paragraphs){
foreach(CompVector Sentence, Paragraph){
QString DecodedSentence = *Sentencetable.find(Sentence.GetID());
FinalResult.append(" " + DecodedSentence);
}
FinalResult.append("\n\r\n\r\n\r\n\r");
}
ui->txtBrsOutput->setText(FinalResult);
}
QSet<QString>* MainWindow::loadIntoHash(QString _file)
{
QSet<QString>* Set = new QSet<QString>();
qDebug(qPrintable(_file));
QFile File(_file);
File.open(QFile::ReadOnly);
QTextStream Stream(&File);
Stream.setAutoDetectUnicode(true);
while(!Stream.atEnd()){
Set->insert(Stream.readLine().trimmed());
}
return Set;
}
QList<CompVector> MainWindow::ReadAndEncodeSentences(QList<QString> EffectiveWords, QHash<int, QString> &SentenceTable)
{
int ID = 0;
QList<CompVector> Result;
foreach(QString Content, inputFileContents){
Tokenizer tokenizer(Content, ".!?؟\n\r");
while(tokenizer.hasMoreTokens()){
QString NextSentence = tokenizer.nextToken();
CompVector SentenceVector(ID);
SentenceTable.insert(ID, NextSentence);
ID++;
Tokenizer LocalTokenizer(NextSentence);
while(LocalTokenizer.hasMoreTokens()){
QString Word = LocalTokenizer.nextToken();
int Location = EffectiveWords.indexOf(Word);
if(Location != -1){
SentenceVector.AddTerm(Location);
}
}
Result.append(SentenceVector);
}
}
return Result;
}
QList<QList<CompVector> > MainWindow::ConstructParagraphs(QList<CompVector> _sentences)
{
// qDebug("%f", CompVector::Sim(_sentences.at(0), _sentences.at(0)));
double TRESHOLD = 0.15;
double BestSimSofar = 0;
int BestParagraphSoFar = -1;
int ParagraphNo = -1;
QList<QList<CompVector> > Result;
foreach(CompVector Vector, _sentences){
if(Result.length() == 0){
//First Time
QList<CompVector> List;
List.append(Vector);
Result.append(List);
}else{
ParagraphNo = -1;
BestParagraphSoFar = -1;
BestSimSofar = 0;
foreach(QList<CompVector> Paragraph, Result){
ParagraphNo++;
foreach(CompVector Sentence, Paragraph){
double sim = CompVector::Sim(Vector, Sentence);
qDebug("%f", sim);
if(sim > TRESHOLD && sim > BestSimSofar){
BestSimSofar = sim;
BestParagraphSoFar = ParagraphNo;
}
}
}
if(BestParagraphSoFar >= 0){
Result[BestParagraphSoFar].append(Vector);
}else{
QList<CompVector> List;
List.append(Vector);
Result.append(List);
}
}
}
return Result;
}
void MainWindow::on_actionSaveOutput_triggered()
{
QFileDialog Dialog;
QString Path = Dialog.getSaveFileName(this);
if(Path.length() > 0){
QFile File(Path);
if(!File.open(QFile::WriteOnly)){
QMessageBox Box;
Box.setText("Unable to save!");
Box.exec();
return;
}
File.write(FinalResult.toUtf8());
File.close();
}
}
QString MainWindow::NormalizeToken(QString _token){
// _token.remove(')');
// _token.remove('(');
// _token.remove('\'');
// _token.remove('\"');
// _token.remove('[');
// _token.remove(']');
if(IsValidWord(_token)){
return _token;
}
if(ValidIfRemoved(_token, lang_an)){
_token.chop(lang_an.length());
}else if(ValidIfRemoved(_token, lang_at)){
_token.chop(lang_at.length());
}else if(ValidIfRemoved(_token, lang_ati)){
_token.chop(lang_ati.length());
}else if(ValidIfRemoved(_token, lang_ha)){
_token.chop(lang_ha.length());
}else if(ValidIfRemoved(_token, lang_haii)){
_token.chop(lang_haii.length());
}else if(ValidIfRemoved(_token, lang_in)){
_token.chop(lang_in.length());
}else if(ValidIfRemoved(_token, lang_ini)){
_token.chop(lang_ini.length());
}else if(ValidIfRemoved(_token, lang_yii)){
_token.chop(lang_yii.length());
}else if(ValidIfRemoved(_token, lang_ii)){
_token.chop(lang_ii.length());
}
return _token.trimmed();
}
bool MainWindow::IsValidWord(QString _word)
{
// QMessageBox B;
// B.setText(_word + (Lang_Nouns.contains(_word) ? " Yes" : " No"));
// B.exec();
if(Lang_Nouns.contains(_word))
return true;
return false;
}
bool MainWindow::ValidIfRemoved(QString _token, QString _affix)
{
if(_token.endsWith(_affix)){
if(IsValidWord(_token.left(_token.length() - _affix.length()).trimmed())){
return true;
}
return false;
}
return false;
}