-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
284 lines (249 loc) · 9.97 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
#include "mainwindow.h"
#include "omonimdialog.h"
#include "ui_mainwindow.h"
#include "matchtablemodel.h"
#include <QtCore/QCoreApplication>
#include <QtCore>
#include <QFile>
#include <QFileDialog>
#include <QTextEdit>
#include <QMessageBox>
#include <QByteArray>
#include <QTextStream>
#include <QTableWidget>
#include <QMainWindow>
#include <QInputDialog>
#include <lspl/Namespace.h>
#include <lspl/patterns/PatternBuilder.h>
#include <lspl/patterns/Alternative.h>
#include <lspl/patterns/matchers/Matcher.h>
#include <lspl/patterns/Pattern.h>
#include <lspl/patterns/matchers/Context.h>
#include <lspl/text/readers/PlainTextReader.h>
#include <lspl/text/Text.h>
#include <lspl/text/attributes/SpeechPart.h>
#include <lspl/text/Match.h>
#include <lspl/base/Exception.h>
#include <lspl/transforms/ContextRetriever.h>
#include <lspl/transforms/TextTransform.h>
#include <lspl/transforms/TextTransformBuilder.h>
#include <lspl/morphology/Morphology.h>
#include <sys/stat.h>
//using lspl::text::attributes::AttributeKey;
/* The main window representation class constructor.
* Attach handler for controlls items.
* Initialize lspl objects.
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Attach slots for signals for interface buttons
connect(ui->pbAddTemplate, SIGNAL(clicked()), this, SLOT(addTemplate()));
connect(ui->pbApplyTemplate, SIGNAL(clicked()), this, SLOT(applyTemplates()));
connect(ui->loadText, SIGNAL(triggered()), this, SLOT(loadText()));
connect(ui->loadTemplate, SIGNAL(triggered()), this, SLOT(loadTemplate()));
connect(ui->showOmonims, SIGNAL(triggered()), this, SLOT(showOmonims()));
connect(ui->saveStatistic, SIGNAL(triggered()), this, SLOT(saveData()));
connect(ui->saveText, SIGNAL(triggered()), this, SLOT(saveText()));
connect(ui->textInfo, SIGNAL(triggered()), this, SLOT(textInfo()));
ui->tableMatch->setModel(&matchTableModel);
// LSPL initializing
// Create lspl namespace (storage for patterns)
lsplNS = new lspl::Namespace();
// Define pattern builder
lsplPatternBuilderTransform = new lspl::patterns::PatternBuilder(lsplNS, new lspl::transforms::TextTransformBuilder(lsplNS));
}
/* Create dialog with omonims list function */
void MainWindow::showOmonims() {
qDebug() << "show omonims slot\n";
OmonimDialog * dialog = new OmonimDialog();
// Get last version of text
getTextFromView();
// Set data for view
dialog->setData(lsplText->getWords());
// Run dialog window
dialog->exec();
}
// Add pattern by text definition to system function */
void MainWindow::addPattern( const QString &pattern ) {
QTextCodec * codec = QTextCodec::codecForName("CP1251");
lspl::patterns::PatternBuilder::BuildInfo info;
try {
// Try to build pattern
info = lsplPatternBuilderTransform->build(std::string(codec->fromUnicode(pattern).constData()));
}
catch (lspl::patterns::PatternBuildingException ex) {
// Catch exception (Pattern may be added to system)
QString error_content(pattern);
error_content.append(QString(":\n"));
error_content.append(QString(ex.what()));
qDebug() << error_content.toStdString().c_str() << "\n";
}
// Analyze pattern parsing
if (info.parseTail.length() != 0) {
qDebug() << "Error during parsing '" << codec->toUnicode(pattern.toStdString().c_str()) << "': '" << info.parseTail.c_str() << "' not parsed\n";
}
else
// Show pattern in list if success
ui->listTemplate->addItem(pattern); // pattern vs codec->toUnicode(pattern.toStdString().c_str())
}
/* Add pattern from interface form function */
void MainWindow::addTemplate() {
qDebug() << "add template slot";
bool isOk;
// Get text definition of pattern from dialog
QString pattern = QInputDialog::getText(this, "Input pattern", "Pattern:", QLineEdit::Normal, "", &isOk);
if (!isOk)
return;
// Add pattern to system
addPattern(pattern);
}
/* Load patterns from file function */
void MainWindow::loadTemplate() {
qDebug() << "load template slot\n";
// Get file name for loading
QString filename = QFileDialog::getOpenFileName(this, QString("Open template file"));
if (filename == "")
return;
QFile tempfile(filename);
if (!tempfile.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, QString("Error"), QString("Cannot open template file"));
return;
}
QTextStream ts(&tempfile);
ts.setCodec("CP1251");
/* Add any file line to template list */
while (!ts.atEnd())
addPattern(ts.readLine().toUtf8());
tempfile.close();
}
/* Get text from edit view and load it to inner representation */
void MainWindow::getTextFromView() {
lspl::text::readers::PlainTextReader reader;
QTextCodec *codec = QTextCodec::codecForName("CP1251");
// Get text from edit view and convert it to CP1251 encoding
QByteArray ba = codec->fromUnicode(ui->textEdit->toPlainText());
// Translate to innner presentation
lsplText = reader.readFromString(ba.constData());
}
/* Build table of matches for registered patterns and show it (on 'tableMatch' buuton slot) */
void MainWindow::applyTemplates() {\
// Get text from field
getTextFromView();
// Update data in table model
matchTableModel.setNewData(lsplNS, lsplText);
// Update table
ui->tableMatch->reset();
}
/* Load text file function (on 'load text' menu item slot) */
void MainWindow::loadText() {
qDebug() << "load text slot";
QString filename = QFileDialog::getOpenFileName(this, QString("Open text file"));
if (filename == "")
return;
QFile text(filename);
if (!text.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, QString("Error"), QString("Cannot open text file"));
return;
}
QTextStream ts(&text);
ts.setCodec("CP1251");
// Show text on edit view
ui->textEdit->setText(ts.readAll());
text.close();
}
/* Save statistic about pattern matching */
void MainWindow::saveData() {
qDebug() << "save data slot";
// Get save file name from dialog
QString filename = QFileDialog::getSaveFileName(this, QString("Save data file"));
if (filename == "")
return;
QFile text(filename);
if (!text.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, QString("Error"), QString("Cannot open text file ") + filename);
return;
}
QTextStream ts(&text);
int patternCount = lsplNS->getPatternCount();
lspl::text::MatchList matches;
int start = -1, end = -1, dif = 0;
// For each patterns in system save statistic
for (int i = 0; i < patternCount; i++) {
lspl::patterns::PatternRef pattern = lsplNS->getPatternByIndex(i);
ts << pattern->getName().c_str() << " = " << pattern->getSource().c_str() << ":" << "\n";
// For each match add it to the tree to parent pattern
if (!lsplText) // No text in system
continue;
matches = lsplText->getMatches(pattern);
ts << "all matches: " << matches.size() << "\n";
// Count no crossing matches
dif = 0;
for (int i = 0; i < (int)matches.size(); i++) {
// Using order of matches during test
lspl::text::MatchRef match = matches[i];
// Test for crossing matches
if (end < (int)match->getRangeStart() || (int)match->getRangeEnd() < start) { // No crossing
dif++;
start = match->getRangeStart();
end = match->getRangeEnd();
}
else { // Crossing
start = qMin(start, (int)match->getRangeStart());
end = qMax(end, (int)match->getRangeEnd());
}
}
ts << "No crossing matches: " << dif << "\n";
}
text.close();
}
/* Save text from edit view to file in CP1251 encoding */
void MainWindow::saveText() {
qDebug() << "save text slot";
QString filename = QFileDialog::getSaveFileName(this, QString("Save text file"));
if (filename == "")
return;
QFile text(filename);
if (!text.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, QString("Error"), QString("Cannot open text file ") + filename);
return;
}
getTextFromView();
text.write(lsplText->getContent().c_str(), lsplText->getContent().length());
text.close();
}
/* Save text from edit view to file in CP1251 encoding */
void MainWindow::savePatterns() {
qDebug() << "save patterns slot";
QString filename = QFileDialog::getSaveFileName(this, QString("Save patterns file"));
if (filename == "")
return;
QFile text(filename);
if (!text.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, QString("Error"), QString("Cannot open text file ") + filename);
return;
}
int pattern_count = lsplNS->getPatternCount();
for (int i = 0; i < pattern_count; i++) {
lspl::patterns::PatternRef pattern = lsplNS->getPatternByIndex(i);
text.write(pattern->getName().c_str(), pattern->getName().length());
text.write(" = ", 3);
text.write(pattern->getSource().c_str(), pattern->getSource().length());
text.write("\n", 1); // Maybe 2 bytes? (Unix vs Windows)... Ohh, these problems...
}
text.close();
}
/* Show info about text (Number of words in text) */
void MainWindow::textInfo() {
getTextFromView();
int wordNum = 0;
if (!!lsplText)
wordNum = lsplText->getWords().size();
QMessageBox::about(this, QString("Text information"), QString("Number words in text: ") + QString::number(wordNum));
}
/* Destructor for the main window */
MainWindow::~MainWindow() {
delete ui;
}