This repository was archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow.cpp
More file actions
525 lines (445 loc) · 18.1 KB
/
window.cpp
File metadata and controls
525 lines (445 loc) · 18.1 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
#include <QtCore>
#include <QtWidgets>
#include <random>
#include "window.h"
#include "soundchanges.h"
#include "highlighter.h"
#include "affixerdialog.h"
Window::Window()
{
QWidget *mainwidget = new QWidget;
setCentralWidget(mainwidget);
m_layout = new QHBoxLayout;
m_leftlayout = new QVBoxLayout;
m_ruleslayout = new QVBoxLayout;
m_wordslayout = new QVBoxLayout;
m_midlayout = new QVBoxLayout;
m_resultslayout = new QVBoxLayout;
m_syllableseperatorlayout = new QHBoxLayout;
mainwidget->setLayout(m_layout);
QFont font("Courier", 10);
font.setFixedPitch(true);
m_categorieslabel = new QLabel("Categories:");
m_leftlayout->addWidget(m_categorieslabel);
m_categories = new QPlainTextEdit;
m_categories->setFont(font);
m_leftlayout->addWidget(m_categories, 2);
m_rewriteslabel = new QLabel("Rewrite rules:");
m_leftlayout->addWidget(m_rewriteslabel);
m_rewrites = new QPlainTextEdit;
m_leftlayout->addWidget(m_rewrites, 1);
m_layout->addLayout(m_leftlayout);
m_ruleslabel = new QLabel("Sound changes:");
m_ruleslayout->addWidget(m_ruleslabel);
m_rules = new QPlainTextEdit;
m_rules->setFont(font);
m_highlighter = new Highlighter(m_rules->document());
m_ruleslayout->addWidget(m_rules);
m_layout->addLayout(m_ruleslayout);
m_wordslabel = new QLabel("Input lexicon:");
m_wordslayout->addWidget(m_wordslabel);
m_words = new QPlainTextEdit;
m_wordslayout->addWidget(m_words);
m_layout->addLayout(m_wordslayout);
m_applyfillerlabel = new QLabel("");
m_midlayout->addWidget(m_applyfillerlabel);
m_apply = new QPushButton("Apply");
m_midlayout->addWidget(m_apply);
m_showChangedWords = new QCheckBox("Show changed words");
m_showChangedWords->setChecked(true);
m_midlayout->addWidget(m_showChangedWords);
m_reportChanges = new QCheckBox("Report which rules apply");
m_midlayout->addWidget(m_reportChanges);
m_doBackwards = new QCheckBox("Rewrite on output");
m_midlayout->addWidget(m_doBackwards);
const QChar arrow(0x2192);
m_formatgroup = new QGroupBox("Output format");
m_plainformat = new QRadioButton("output > gloss");
m_arrowformat = new QRadioButton(QString("input %1 output").arg(arrow));
m_squareinputformat = new QRadioButton("output [input]");
m_squareglossformat = new QRadioButton("output [gloss]");
m_arrowglossformat = new QRadioButton(QString("input %1 output [gloss]").arg(arrow));
QVBoxLayout *fbox = new QVBoxLayout;
fbox->addWidget(m_plainformat);
fbox->addWidget(m_arrowformat);
fbox->addWidget(m_squareinputformat);
fbox->addWidget(m_squareglossformat);
fbox->addWidget(m_arrowglossformat);
m_plainformat->setChecked(true);
m_formatgroup->setLayout(fbox);
m_midlayout->addWidget(m_formatgroup);
m_reversegroup = new QGroupBox("Reversing changes");
m_reversechanges = new QCheckBox("Reverse changes");
m_filters = new QPlainTextEdit;
m_filterslabel = new QLabel("Filters:");
m_filtercurrent = new QPushButton("Filter current");
QVBoxLayout *rbox = new QVBoxLayout;
rbox->addWidget(m_reversechanges);
rbox->addWidget(m_filterslabel);
rbox->addWidget(m_filters);
rbox->addWidget(m_filtercurrent);
m_reversegroup->setLayout(rbox);
m_midlayout->addWidget(m_reversegroup);
m_midlayout->addStretch();
m_progress = new QProgressBar;
m_midlayout->addWidget(m_progress);
m_syllabify = new QLineEdit;
m_midlayout->addWidget(m_syllabify);
m_syllableseperatorlabel = new QLabel("Syllable seperator: ");
m_syllableseperatorlayout->addWidget(m_syllableseperatorlabel);
m_syllableseperator = new QLineEdit;
m_syllableseperator->setMaxLength(1);
// make width equal to 1em so it's clear the textbox only accepts 1 char
// we multiply by 2 because we also need some space on the side
m_syllableseperator->setFixedWidth(m_syllableseperator->fontMetrics().width('M') * 2);
m_syllableseperator->setText("-");
m_syllableseperator->setAlignment(Qt::AlignCenter);
m_syllableseperatorlayout->addWidget(m_syllableseperator);
m_syllableseperatorlayout->addStretch();
m_syllableseperatorlayout->setSpacing(0);
m_midlayout->addLayout(m_syllableseperatorlayout);
m_layout->addLayout(m_midlayout);
m_resultslabel = new QLabel("Output lexicon:");
m_resultslayout->addWidget(m_resultslabel);
m_results = new QTextEdit;
m_results->setReadOnly(true);
m_resultslayout->addWidget(m_results);
m_layout->addLayout(m_resultslayout);
m_categorieslist = new QMap<QChar, QList<QChar>>();
connect(m_categories, &QPlainTextEdit::textChanged, this, &Window::UpdateCategories);
connect(m_apply, &QPushButton::clicked, this, &Window::DoSoundChanges);
connect(m_filtercurrent, &QPushButton::clicked, this, &Window::FilterCurrent);
fileMenu = menuBar()->addMenu("File");
fileMenu->addAction("Open sound changes", this, &Window::OpenEsc, QKeySequence(QKeySequence::Open));
fileMenu->addAction("Open wordlist", this, &Window::OpenLex);
fileMenu->addAction("Save sound changes", this, &Window::SaveEsc, QKeySequence(QKeySequence::Save));
fileMenu->addAction("Save sound changes as", this, &Window::SaveEscAs, QKeySequence(QKeySequence::SaveAs));
fileMenu->addAction("Save wordlist as", this, &Window::SaveLex);
toolsMenu = menuBar()->addMenu("Tools");
toolsMenu->addAction("Affixer", this, &Window::LaunchAffixer, QKeySequence(Qt::CTRL + Qt::Key_J));
helpMenu = menuBar()->addMenu("Help");
helpMenu->addAction("About", this, &Window::LaunchAboutBox);
helpMenu->addAction("About Qt", this, &Window::LaunchAboutQt);
if (QCoreApplication::arguments().length() > 1)
{
RealOpenEsc(QCoreApplication::arguments().at(1));
}
}
void Window::DoSoundChanges()
{
bool reverse = m_reversechanges->isChecked();
QStringList changes = ApplyRewrite(m_rules->toPlainText()).split('\n', QString::SkipEmptyParts);
if (reverse) std::reverse(changes.begin(), changes.end());
m_progress->setMaximum(qMax(1, changes.length())); // we use qMax to avoid showing a busy indicator when no changes are specified
m_progress->setMinimum(0);
m_progress->setValue(0);
QStringList result;
QString syllabifyregexp(SoundChanges::PreProcessRegexp(m_syllabify->text(), *m_categorieslist));
QString report;
for (QString word : m_words->toPlainText().split('\n'))
{
QString gloss = "";
bool hasGloss = false;
if (word.split('>').length() > 1)
{
hasGloss = true;
QStringList split = word.split('>');
gloss = split.at(1);
word = split.at(0);
}
QString changed = "";
for (QString subword : word.split(' ', QString::SkipEmptyParts))
{
QStringList subchanged = ApplyRewrite(subword).split(' ', QString::SkipEmptyParts);
for (QString change : changes)
{
bool alwaysApply = false;
bool sometimesApply = false;
bool reverseThisWord = reverse;
for (QString &_subchanged : subchanged)
{
QStringList splitchange;
if ((change.at(0) == QChar('_')) || QRegularExpression(" _").match(change).hasMatch())
// We need the second expression to account for cases like 'f _ax*b/c'
splitchange = change .split(' ', QString::SkipEmptyParts);
else
splitchange = change.replace(QRegularExpression(R"(\*.*)"), "").split(' ', QString::SkipEmptyParts);
if (splitchange.length() == 0) continue;
QString _change;
int prob = 100;
if (splitchange.length() > 1)
{
_change = splitchange.at(splitchange.length() - 1);
for (int i = 0; i < splitchange.length() - 1; i++)
{
switch (splitchange.at(i).at(0).toLatin1())
{
case 'x':
_subchanged = SoundChanges::Syllabify(syllabifyregexp, _subchanged, m_syllableseperator->text().at(0));
break;
case '?':
{
bool ok;
int _prob = QString(splitchange.at(i).mid(1)).toInt(&ok);
if (ok)
{
prob = _prob;
}
break;
}
case 'f':
if (reverseThisWord)
goto CONTINUE;
break;
case 'b':
if (!reverseThisWord)
goto CONTINUE;
reverseThisWord = false; // So we can use normal rules with no special handling
break;
case 'a':
alwaysApply = true;
break;
case 's':
sometimesApply = true;
break;
}
}
}
else _change = splitchange.at(0);
// Block is so no compiler error occurs: "Initialization of 'before' is skipped by 'goto CONTINUE'
{
QString before = _subchanged;
_subchanged = SoundChanges::RemoveDuplicates(SoundChanges::ApplyChange(_subchanged, _change, *m_categorieslist, prob, reverseThisWord, alwaysApply, sometimesApply).join(' '));
_subchanged.remove(m_syllableseperator->text().at(0));
if (_subchanged != before)
report.append(QString("<b>%1</b> changed <b>%2</b> to <b>%3</b><br/>").arg(_change, before, _subchanged));
}
CONTINUE:;
}
subchanged = SoundChanges::Reanalyse(subchanged);
m_progress->setValue(m_progress->value() + 1);
}
m_progress->setValue(0);
QString subchangedJoined = SoundChanges::Filter(subchanged, m_filters->toPlainText().split('\n', QString::SkipEmptyParts), *m_categorieslist).join(' ');
if (m_doBackwards->isChecked()) subchangedJoined = ApplyRewrite(subchangedJoined, true);
if (m_showChangedWords->isChecked() && subchangedJoined != subword) subchangedJoined = QString("<b>").append(subchangedJoined).append("</b>");
if (changed.length() == 0) changed = subchangedJoined;
else changed += ' ' + subchangedJoined;
}
changed = FormatOutput(word.trimmed(), changed.trimmed(), gloss.trimmed(), hasGloss);
result.append(changed);
}
m_results->setHtml(result.join("<br/>"));
if (m_reportChanges->isChecked())
{
QMessageBox *msgBox = new QMessageBox(this);
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setText(report);
msgBox->setWindowModality(Qt::NonModal);
msgBox->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
msgBox->show();
}
}
void Window::FilterCurrent()
{
QList<QString> result;
for (QString word : m_results->toPlainText().split('\n'))
{
result.append(SoundChanges::Filter(word.split(' ', QString::SkipEmptyParts), m_filters->toPlainText().split('\n', QString::SkipEmptyParts), *m_categorieslist).join(' '));
}
m_results->setHtml(result.join("<br/>"));
}
void Window::UpdateCategories()
{
bool first = true;
for (QString line : ApplyRewrite(m_categories->toPlainText()).split('\n', QString::SkipEmptyParts))
{
if (!QRegularExpression("^.=.+$").match(line).hasMatch()) continue;
QStringList parts = line.split("=");
if (first) { m_categorieslist = new QMap<QChar, QList<QChar>>; first = false; }
QList<QChar> phonemes;
for (QChar c : parts.at(1))
{
if (m_categorieslist->contains(c)) phonemes.append(m_categorieslist->value(c));
else phonemes.append(c);
}
m_categorieslist->insert(parts.at(0).at(0), phonemes);
}
QString regexp("");
first = true;
for (QChar key : m_categorieslist->keys())
{
if (!first) regexp.append("|");
if (first) first = false;
regexp.append(key);
}
m_highlighter->MakeHighlightingRules(regexp);
m_highlighter->rehighlight();
}
QString Window::ApplyRewrite(QString str, bool backwards)
{
QString rewritten = str;
QString s = m_rewrites->toPlainText();
for (QString line : m_rewrites->toPlainText().split('\n', QString::SkipEmptyParts))
{
QStringList parts = line.split('>');
if (parts.length() != 2) continue;
if (backwards) rewritten.replace(parts.at(1), parts.at(0));
else rewritten.replace(parts.at(0), parts.at(1));
}
return rewritten;
}
QString Window::FormatOutput(QString in, QString out, QString gloss, bool hasGloss)
{
const QChar arrow(0x2192);
if (m_plainformat->isChecked())
{
if (hasGloss) return QString("%1 > %2").arg(out, gloss);
else return out;
}
else if (m_arrowformat->isChecked())
{
return QString("%1 %2 %3").arg(in, arrow, out);
}
else if (m_squareinputformat->isChecked())
{
return QString("%1 [%2]").arg(out, in);
}
else if (m_squareglossformat->isChecked())
{
if (hasGloss) return QString("%1 [%2]").arg(out, gloss);
else return out;
}
else if (m_arrowglossformat->isChecked())
{
if (hasGloss) return QString("%1 %2 %3 [%4]").arg(in, arrow, out, gloss);
else return QString("%1 %2 %3").arg(in, arrow, out);
}
return out;
}
void Window::LaunchAffixer()
{
AffixerDialog *affixer = new AffixerDialog;
affixer->show();
connect(affixer, &AffixerDialog::addText, this, &Window::AddFromAffixer);
}
void Window::AddFromAffixer(QStringList words, AffixerDialog::PlaceToAdd placeToAdd)
{
QString textToAdd = words.join('\n');
switch (placeToAdd)
{
case AffixerDialog::PlaceToAdd::AddToStart:
m_words->setPlainText(textToAdd.append('\n').append(m_words->toPlainText()));
break;
case AffixerDialog::PlaceToAdd::AddToEnd:
m_words->setPlainText(m_words->toPlainText().append('\n').append(textToAdd));
break;
case AffixerDialog::PlaceToAdd::AddAtCursor:
m_words->insertPlainText(QString('\n').append(textToAdd).append('\n'));
break;
case AffixerDialog::PlaceToAdd::Overwrite:
m_words->setPlainText(textToAdd);
break;
}
}
void Window::OpenEsc()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open .esc File", QString(), "exSCA Files (*.esc);;All files (*.*)");
RealOpenEsc(fileName);
}
void Window::RealOpenEsc(QString fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, "Could Not Open File", "The file could not be opened");
return;
}
QTextStream in(&file);
in.setCodec("UTF-8");
QStringList cats, rules, rews;
while (!in.atEnd())
{
QString line = in.readLine();
if (line.contains('=')) cats.append(line);
else if (line.contains('>') && !line.contains('/')) rews.append(line);
else rules.append(line);
}
m_categories->setPlainText(cats.join('\n'));
m_rules->setPlainText(rules.join('\n'));
m_rewrites->setPlainText(rews.join('\n'));
file.close();
SetCurrentFile(fileName);
}
void Window::OpenLex()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open .lex File", QString(), "exSCA word files (*.lex);;All files (*.*)");
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, "Could Not Open File", "The file could not be opened");
return;
}
QTextStream in(&file);
in.setCodec("UTF-8");
QStringList words;
while (!in.atEnd()) words.append(in.readLine());
m_words->setPlainText(words.join('\n'));
file.close();
}
void Window::SaveEsc()
{
if (currentFile == "") SaveEscAs();
else RealSaveEsc(currentFile);
}
void Window::SaveEscAs()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save As .esc File", QString(), "exSCA Files (*.esc);;All files (*.*)");
RealSaveEsc(fileName);
}
void Window::RealSaveEsc(QString fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::warning(this, "Could Not Open File", "The file could not be opened");
return;
}
QTextStream out(&file);
out.setCodec("UTF-8");
out << m_categories->toPlainText().toUtf8() << endl;
out << m_rewrites ->toPlainText().toUtf8() << endl;
out << m_rules ->toPlainText().toUtf8() << endl;
file.close();
SetCurrentFile(fileName);
}
void Window::SaveLex()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save As .lex File", QString(), "exSCA word files (*.lex);;All files (*.*)");
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::warning(this, "Could Not Open File", "The file could not be opened");
return;
}
QTextStream out(&file);
out.setCodec("UTF-8");
for (QString word : m_words->toPlainText().split('\n'))
{
out << word.toUtf8() << endl;
}
file.close();
}
void Window::LaunchAboutBox()
{
QMessageBox::about(this, "About exSCA", "<b>exSCA</b><br/>Version 2.1.2<br/>Copyright © Brad Neimann 2017");
}
void Window::LaunchAboutQt()
{
QMessageBox::aboutQt(this, "About Qt");
}
void Window::SetCurrentFile(QString fileName)
{
currentFile = fileName;
setWindowTitle("exSCA - " + fileName);
}