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
|
/*
Copyright (c) 2006-2009, Tom Thielicke IT Solutions
SPDX-License-Identifier: GPL-2.0-only
*/
/****************************************************************
**
** Implementation of the TrainingSql class
** File name: trainingsql.cpp
**
****************************************************************/
#include <QDateTime>
#include <QRegularExpression>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlQuery>
#include <QStringList>
#include <QVariant>
#include "def/defines.h"
#include "trainingsql.h"
// Konstruktor
TrainingSql::TrainingSql(QString replace, QString regexp, QString layout)
: queryCounter(0)
, replaceSetting(replace)
, regexpSetting(regexp)
{
setSynchron(t10::synchron_db_while_training);
if (replaceSetting == "NULL") {
replaceSetting = getKeyboardLayoutReplaceRoutine(layout);
}
if (regexpSetting == "NULL") {
regexpSetting = getKeyboardLayoutRegexpRoutine(layout);
}
}
TrainingSql::~TrainingSql() { setSynchron(true); }
int TrainingSql::getLessonUnit(int lesson, int type)
{
QSqlQuery query;
QString tableName;
QString sqlString;
switch (type) {
case 0:
tableName = "lesson";
break;
case 1:
tableName = "open";
break;
case 2:
tableName = "own";
break;
}
sqlString = "SELECT " + tableName + "_list." + tableName + "_id, "
+ tableName + "_list." + tableName + "_unit FROM " + tableName
+ "_list WHERE " + tableName + "_list." + tableName
+ "_id = " + QString::number(lesson) + ";";
// Standard query to get the first lesson
if (!query.exec(sqlString)) {
return 0;
}
if (!query.first()) {
return 0;
}
auto lessonUnit = query.value(1).toInt();
return lessonUnit;
}
void TrainingSql::setSynchron(bool synchron)
{
QSqlQuery query;
QString queryString;
if (synchron) {
queryString = "PRAGMA synchronous=FULL;";
} else {
queryString = "PRAGMA synchronous=OFF;";
}
query.exec(queryString);
}
bool TrainingSql::updateUsertable(QChar unicodechar, QString columnname)
{
QSqlQuery query;
// int sqlNum = 0;
// First a simple select statement to check whether the unicode exists
// already in table
/*if (!query.exec("SELECT COUNT(uerrorunicode) FROM usererrors WHERE "
"uerrorunicode = " + QString::number(unicodechar.unicode()) + ";"))
{ return false;
}*/
// Now check the number of rows returned
// Below commented is the full code to test wheter the current database
// supports the feature "size()".
// At the Moment a SQLite Driver Version 3.3.4 is in use - it doesn't
// support the feature "size()". So we have to go to the end of the table
// to count the number of rows returned as listet below after the commented
// code.
// This is slow - so change it if using an other database driver which
// supports the feature!
/*QSqlDatabase defaultDB = QSqlDatabase::database();
//defaultDB.driver()->hasFeature(QSqlDriver::Transactions);
if (defaultDB.driver()->hasFeature(QSqlDriver::QuerySize)) {
sqlNum = query.size();
} else {
query.last();
sqlNum = query.at() + 1;
}*/
// Slow function (look at the comment above)
// sqlNum = query.at() + 1;
/*query.first();
if (query.value(0).toInt() == 0) {
// There is no data record with the current unicode
// -> insert new data record
if (!query.exec("INSERT INTO usererrors VALUES("
+ QString::number(unicodechar.unicode()) + ",0,0,0);")) {
return false;
}
}*/
// Now we keep save there is already a data record with the current unicode
// -> update (increment) current data record
QString charToString = QString::number(unicodechar.unicode());
// query.exec("INSERT INTO usererrors VALUES(" + temp + ",0,0,0);");
query.exec("BEGIN;");
query.exec(
"INSERT OR IGNORE INTO user_chars VALUES(" + charToString + ",0,0,0);");
query.exec("UPDATE user_chars SET " + columnname + "=" + columnname
+ "+1 WHERE user_char_unicode = " + charToString + ";");
return query.exec("COMMIT;");
}
QString TrainingSql::createLesson(int lesson, int type, int unit,
bool intelligence, bool useEszett, bool useUmlaut)
{
QSqlQuery query;
QString sqlString = "";
QString tableName = "";
QString textOutput = "";
QString textOutputSub = "";
switch (type) {
case 0:
tableName = "lesson_content";
break;
case 1:
tableName = "open_content";
break;
case 2:
tableName = "own_content";
break;
}
sqlString = "SELECT " + tableName + ".content_id, " + tableName
+ ".content_text FROM " + tableName + " WHERE " + tableName
+ ".content_lesson = " + QString::number(lesson) + " ORDER BY "
+ tableName + ".content_id;";
// Standard query to get the first lesson
if (!query.exec(sqlString)) {
return "";
}
if (intelligence) {
if (!query.first()) {
return "";
}
if (query.isNull(0)) {
return "";
}
lessonsDoneList.prepend(query.value(0).toInt());
textOutput = query.value(1).toString();
textOutput
= replaceRoutine(textOutput, useEszett, useUmlaut, lesson, type);
} else {
int counterToNewLine = 0;
// Without intelligence
while (query.next()) {
textOutputSub = query.value(1).toString();
counterToNewLine += textOutputSub.length();
if (unit == 0
|| (unit == 1
&& counterToNewLine > t10::num_token_until_new_line)) {
textOutputSub.append(t10::token_new_line);
counterToNewLine = 0;
} else {
textOutputSub.append(" ");
counterToNewLine++;
}
textOutput.append(replaceRoutine(
textOutputSub, useEszett, useUmlaut, lesson, type));
}
}
// return new lesson
return textOutput;
}
QString TrainingSql::updateLesson(
int lesson, int type, bool intelligence, bool useEszett, bool useUmlaut)
{
QSqlQuery query;
QString errorUnicodeMaxFirst = "";
QString errorUnicodeMaxSecond = "";
QString errorUnicodeMaxThird = "";
QString errorUnicodeMaxFourth = "";
QString sqlString = "";
bool errorsExist = false;
QString tableName = "";
QString analysisName = "";
QString textOutput = "";
QString textOutputSub = "";
queryCounter++;
switch (type) {
case 0:
tableName = "lesson_content";
analysisName = "lesson_analysis";
break;
case 1:
tableName = "open_content";
analysisName = "open_analysis";
break;
case 2:
tableName = "own_content";
analysisName = "own_analysis";
break;
}
if (intelligence) {
// Check if user errors exist and take the unicode with the
// highest error ratio
if (query.exec(
"SELECT user_char_unicode, (user_char_target_errornum * "
"100) / user_char_occur_num AS user_char_weighted "
"FROM user_chars "
"WHERE user_char_unicode > 31 AND user_char_unicode < 255 "
"ORDER BY user_char_weighted DESC;")) {
if (query.first()) {
errorsExist = true;
errorUnicodeMaxFirst = query.value(0).toString();
if (query.next()) {
errorUnicodeMaxSecond = query.value(0).toString();
if (query.next()) {
errorUnicodeMaxThird = query.value(0).toString();
if (query.next()) {
errorUnicodeMaxFourth = query.value(0).toString();
}
}
}
}
} else {
// SQL exec error
return "";
}
sqlString = "SELECT " + tableName + ".content_id, " + tableName
+ ".content_text FROM " + tableName;
if (errorsExist && (queryCounter % t10::num_intelligent_queries) != 0) {
// User error exists
// -> create lesson SQL string sorted by errorUnicodeMax
sqlString.append(" LEFT JOIN " + analysisName + " ON " + tableName
+ ".content_id = " + analysisName + ".analysis_content AND ");
if ((lesson % 100) != t10::last_lession || type != 0) {
sqlString.append(
tableName + ".content_lesson = " + QString::number(lesson));
} else {
// Last training lesson over all previous lessons
sqlString.append("(" + tableName + ".content_lesson % 100) >= "
+ QString::number(t10::border_lesson_is_sentence) + " AND ("
+ tableName + ".content_lesson % 100) <= "
+ QString::number(t10::last_lession));
}
sqlString.append(" AND (" + tableName
+ ".content_id % 1000) != 1 "
"GROUP BY "
+ tableName + ".content_id");
if (errorUnicodeMaxFirst != "") {
sqlString.append(" ORDER BY analysis_char_"
+ errorUnicodeMaxFirst + " DESC");
if (errorUnicodeMaxSecond != "") {
sqlString.append(
", analysis_char_" + errorUnicodeMaxSecond + " DESC");
}
if (errorUnicodeMaxThird != "") {
sqlString.append(
", analysis_char_" + errorUnicodeMaxThird + " DESC");
}
if (errorUnicodeMaxFourth != "") {
sqlString.append(
", analysis_char_" + errorUnicodeMaxFourth + " DESC");
}
}
sqlString.append(", Random();");
} else {
// No user error exists
// -> create standard lesson SQL string sorted by id
sqlString.append(" WHERE");
sqlString.append(" (" + tableName + ".content_id % 1000) != 0");
if ((lesson % 100) != t10::last_lession || type != 0) {
sqlString.append(" AND " + tableName
+ ".content_lesson = " + QString::number(lesson));
} else {
// Last training lesson over all previous lessons
sqlString.append(" AND (" + tableName
+ ".content_lesson % 100) >= "
+ QString::number(t10::border_lesson_is_sentence) + " AND ("
+ tableName + ".content_lesson % 100) <= "
+ QString::number(t10::last_lession));
}
sqlString.append(" ORDER BY Random();");
}
// qDebug() << sqlString;
// Execute SQL string created above
if (query.exec(sqlString)) {
// Find a lesson not occurs last time
while (query.next()) {
if (!lessonsDoneList.contains(query.value(0).toInt())) {
// New unused lesson found
// -> append to lessonsDoneList
lessonsDoneList.prepend(query.value(0).toInt());
// Check if lessonsDoneList exceeds its maximum
if (lessonsDoneList.size() > t10::num_text_until_repeat) {
// lessonsDoneList exceeded -> remove last item
lessonsDoneList.removeLast();
}
textOutput = query.value(1).toString();
// return new lesson
return replaceRoutine(
textOutput, useEszett, useUmlaut, lesson, type);
}
}
} else {
// SQL exec error
return "";
}
// No unused lesson found
// -> delete all items expect the first of lessonsDoneList
while (lessonsDoneList.size() > 1) {
lessonsDoneList.removeLast();
}
return updateLesson(lesson, type, intelligence, useEszett, useUmlaut);
}
// Without intelligence
sqlString = "SELECT " + tableName + ".content_id, " + tableName
+ ".content_text FROM " + tableName + " WHERE " + tableName
+ ".content_lesson = " + QString::number(lesson) + " ORDER BY "
+ tableName + ".content_id;";
// Standard query to get the first lesson
if (!query.exec(sqlString)) {
return "";
}
while (query.next()) {
textOutputSub = query.value(1).toString();
textOutputSub.append(t10::token_new_line);
textOutput.append(
replaceRoutine(textOutputSub, useEszett, useUmlaut, lesson, type));
}
return textOutput;
}
QVariant TrainingSql::saveLesson(int lesson, int timelen, int tokenlen,
int charnum, int errornum, QDateTime timestamp, int type, QString name)
{
QSqlQuery query;
QVariant lastRowId;
QString lessonName;
lessonName = name;
if (!query.exec("INSERT INTO user_lesson_list VALUES(NULL,"
+ QString::number(lesson) + "," + QString::number(timelen) + ","
+ QString::number(tokenlen) + "," + QString::number(charnum) + ","
+ QString::number(errornum) + ", '"
+ timestamp.toString("yyyyMMddhhmmss") + "', "
+ QString::number(type) + ", '" + lessonName + "');")) {
return QVariant();
}
lastRowId = query.lastInsertId();
return lastRowId;
}
// TODO: decide what to do with unused variable useEszett
QString TrainingSql::replaceRoutine(QString content,
[[maybe_unused]] bool useEszett, bool useUmlaut, int lesson, int type)
{
// Tab char (an arrow)
content.replace(QChar(0x9), t10::token_tab, Qt::CaseSensitive);
// Eszett
/*if (!useEszett) {
content.replace(QChar(0x00df), "ss", Qt::CaseSensitive);
}
// Umlaut
if (!useUmlaut) {
content.replace(QChar(0x00df), "ss", Qt::CaseSensitive);
content.replace(QChar(0x00e4), "ae", Qt::CaseSensitive);
content.replace(QChar(0x00f6), "oe", Qt::CaseSensitive);
content.replace(QChar(0x00fc), "ue", Qt::CaseSensitive);
content.replace(QChar(0x00c4), "Ae", Qt::CaseSensitive);
content.replace(QChar(0x00d6), "Oe", Qt::CaseSensitive);
content.replace(QChar(0x00dc), "Ue", Qt::CaseSensitive);
content.replace(QChar(0x20ac), "Eur", Qt::CaseSensitive);
content.replace(QChar(0x00a7), "Par.", Qt::CaseSensitive);
if ((lesson % 100) > t10::last_lession && type == 0) {
content.replace(QChar(0x002c), ".", Qt::CaseSensitive);
}
}*/
if (!useUmlaut) {
if ((lesson % 100) > t10::last_lession && type == 0) {
content.replace(QChar(0x002c), ".", Qt::CaseSensitive);
}
}
if (replaceSetting != "") {
QStringList replaceList = replaceSetting.split(",", Qt::SkipEmptyParts);
QStringList searchReplace;
for (int i = 0; i < replaceList.size(); ++i) {
searchReplace = replaceList.at(i).split("=", Qt::SkipEmptyParts);
if (searchReplace.size() >= 2) {
content.replace(searchReplace.at(0), searchReplace.at(1),
Qt::CaseSensitive);
}
}
}
if (regexpSetting.left(2) == "[^") {
regexpSetting = regexpSetting.left(2) + t10::token_new_line
+ t10::token_tab + regexpSetting.mid(2);
content.replace(QRegularExpression(regexpSetting), "");
}
return content;
}
QString TrainingSql::getKeyboardLayoutReplaceRoutine(QString layout)
{
QSqlQuery query;
QString replaceRoutine = "";
if (!query.exec("SELECT layout_replace "
"FROM language_layouts "
"WHERE layout_key = '"
+ layout + "';")) {
return "";
}
if (query.first()) {
replaceRoutine = query.value(0).toString();
}
return replaceRoutine;
}
QString TrainingSql::getKeyboardLayoutRegexpRoutine(QString layout)
{
QSqlQuery query;
QString regexpRoutine = "";
if (!query.exec("SELECT layout_regexp "
"FROM language_layouts "
"WHERE layout_key = '"
+ layout + "';")) {
return "";
}
if (query.first()) {
regexpRoutine = query.value(0).toString();
}
return regexpRoutine;
}
|