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
|
#include "thesaurusdialog.h"
#include "smallUsefulFunctions.h"
#include "universalinputdialog.h"
#include <QMutex>
#if QT_VERSION >= 0x040400
#include <QFuture>
#include <QtConcurrentRun>
#endif
//==============================Database=============================
class ThesaurusDatabaseType
{
public:
struct TinyStringRef {
int start, length;
TinyStringRef(int astart, int alen);
QString toStringWithBuffer(const QString *buffer);
};
QString fileName, userFileName;
QString *buffer;
QMultiMap<QStringRef, TinyStringRef> thesaurus; //maps category => word1|word2|... in most memory efficent format
QMap<QString, QStringList> userWords; //maps category => Category/words
QMultiMap<QString, QString> userCategories; //maps word => Category
void clear();
void load(QFile &file);
void saveUser();
ThesaurusDatabaseType();
~ThesaurusDatabaseType();
};
void ThesaurusDatabaseType::clear()
{
if (buffer) delete buffer;
buffer = 0;
thesaurus.clear();
fileName.clear();
}
void ThesaurusDatabaseType::saveUser()
{
if (userFileName.isEmpty() || userWords.isEmpty()) return;
QFile f(userFileName);
if (!f.open(QFile::WriteOnly)) return;
QTextStream s(&f);
s.setCodec(QTextCodec::codecForMib(MIB_UTF8));
for (QMap<QString, QStringList>::const_iterator it = userWords.constBegin(), end = userWords.constEnd(); it != end; ++it ) {
if (it.value().size() < 2) continue;
s << it.value().join("|") << "\n";
}
//userFileName.clear(); why was it there? save only once?
}
void ThesaurusDatabaseType::load(QFile &file)
{
REQUIRE(!buffer); //only call it once
QTextStream stream(&file);
QString line;
QStringRef key;
line = stream.readLine();
stream.setCodec(qPrintable(line));
buffer = new QString();
buffer->reserve(file.size());
do {
int currentBufferLength = buffer->length();
line = stream.readLine();
int firstSplitter = line.indexOf('|');
if (firstSplitter >= 0) {
if (line.startsWith("-|") || line.startsWith("(") || line.startsWith("|")) {
buffer->append(line.mid(firstSplitter + 1));
thesaurus.insert(key, ThesaurusDatabaseType::TinyStringRef(currentBufferLength, buffer->length() - currentBufferLength));
//using TinyStringRef instead of QString reduces the memory usage (with German thesaurus) by 4mb without any noticable performance decrease (it could even be faster, because the buffer fits better in the cache).
//TODO: do something something that word type is included in key and still correct search is possible
} else {
buffer->append(line.left(firstSplitter));
key = QStringRef(buffer, currentBufferLength, firstSplitter);
}
}
} while (!line.isNull());
buffer->squeeze();
if (!userFileName.isEmpty()) {
//simpler format: category|word|word|...
QFile f(userFileName);
if (f.open(QIODevice::ReadOnly)) {
QTextStream s(&f);
s.setCodec(QTextCodec::codecForMib(MIB_UTF8));
do {
line = s.readLine();
if (line.startsWith("#") || line.startsWith("%")) continue; //comments
QStringList splitted = line.split("|", QString::SkipEmptyParts);
if (splitted.size() < 2) continue;
userWords.insert(splitted[0].toLower(), splitted);
for (int i = 1; i < splitted.length(); i++)
userCategories.insert(splitted[i].toLower(), splitted[0]);
} while (!line.isNull());
}
}
}
ThesaurusDatabaseType::ThesaurusDatabaseType(): buffer(0)
{
}
ThesaurusDatabaseType::TinyStringRef::TinyStringRef(int astart, int alen): start(astart), length(alen)
{
}
ThesaurusDatabaseType::~ThesaurusDatabaseType()
{
saveUser();
}
QString ThesaurusDatabaseType::TinyStringRef::toStringWithBuffer(const QString *buffer)
{
return buffer->mid(start, length);
}
static ThesaurusDatabaseType globalThesaurus;
static QMutex thesaurusLock;
#if QT_VERSION >= 0x040500
static QFuture<void> thesaurusFuture;
#endif
static QString globalThesaurusNeededFileName;
QString ThesaurusDialog::userPath;
//=============================Dialog==============================
ThesaurusDialog::ThesaurusDialog(QWidget *parent)
: QDialog(parent)
{
replaceBt = new QPushButton(tr("Replace"), this);
lookupBt = new QPushButton(tr("Lookup"), this);
startsWithBt = new QPushButton(tr("Starts With ..."), this);
containsBt = new QPushButton(tr("Contains ..."), this);
cancelBt = new QPushButton(tr("Cancel"), this);
addBt = new QPushButton(tr("Add Own Word"), this);
removeBt = new QPushButton(tr("Remove Own Word"), this);
searchWrdLe = new QLineEdit("", this);
replaceWrdLe = new QLineEdit("", this);
//replaceWrdLe->setEnabled(false);
classlistWidget = new QListWidget(this);
replacelistWidget = new QListWidget(this);
replacelistWidget->setSortingEnabled(true);
replacelistWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
classlistWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
searchWrdLe->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
replaceWrdLe->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(searchWrdLe, 0, 0);
gridLayout->addWidget(replaceWrdLe, 0, 1);
gridLayout->addWidget(replaceBt, 0, 2, Qt::AlignTop);
gridLayout->addWidget(classlistWidget, 1, 0);
gridLayout->addWidget(replacelistWidget, 1, 1);
gridLayout->setColumnStretch(0, 10);
gridLayout->setColumnStretch(1, 10);
gridLayout->setRowStretch(1, 10);
QVBoxLayout *verticalLayout = new QVBoxLayout();
verticalLayout->addWidget(lookupBt, 0, Qt::AlignTop);
verticalLayout->addWidget(startsWithBt, 0, Qt::AlignTop);
verticalLayout->addWidget(containsBt, 0, Qt::AlignTop);
verticalLayout->addWidget(cancelBt, 0, Qt::AlignTop);
verticalLayout->insertStretch(1);
verticalLayout->addWidget(addBt, 0, Qt::AlignTop);
verticalLayout->addWidget(removeBt, 0, Qt::AlignTop);
verticalLayout->insertStretch(-1, 10);
gridLayout->addItem(verticalLayout, 1, 2);
searchWrdLe->setEnabled(false);
setLayout(gridLayout);
setWindowTitle(tr("Thesaurus"));
connect(replaceBt, SIGNAL(clicked()), this, SLOT(accept()));
connect(lookupBt, SIGNAL(clicked()), this, SLOT(lookupClicked()));
connect(startsWithBt, SIGNAL(clicked()), this, SLOT(startsWithClicked()));
connect(containsBt, SIGNAL(clicked()), this, SLOT(containsClicked()));
connect(cancelBt, SIGNAL(clicked()), this, SLOT(reject()));
connect(addBt, SIGNAL(clicked()), this, SLOT(addUserWordClicked()));
connect(removeBt, SIGNAL(clicked()), this, SLOT(removeUserWordClicked()));
//connect(classlistWidget, SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(classClicked(QListWidgetItem*)));
connect(classlistWidget, SIGNAL(currentRowChanged(int)), SLOT(classChanged(int)));
connect(replacelistWidget, SIGNAL(currentRowChanged(int)), SLOT(wordChanged(int)));
thesaurus = retrieveDatabase();
}
ThesaurusDialog::~ThesaurusDialog()
{
if (!thesaurus) return;
thesaurusLock.lock();
if (thesaurus) thesaurus->saveUser();
thesaurusLock.unlock();
}
void ThesaurusDialog::prepareDatabase(const QString &filename)
{
if (filename == globalThesaurusNeededFileName) return;
if (!QFile(filename).exists()) {
#if QT_VERSION >= 0x040500
thesaurusLock.lock();
globalThesaurusNeededFileName = "";
globalThesaurus.clear();
thesaurusLock.unlock();
#else
globalThesaurusNeededFileName = "";
#endif
return;
}
#if QT_VERSION >= 0x040500
thesaurusLock.lock();
globalThesaurusNeededFileName = filename;
thesaurusFuture = QtConcurrent::run(&ThesaurusDialog::loadDatabase, globalThesaurusNeededFileName);
thesaurusLock.unlock();
#else
globalThesaurusNeededFileName = filename;
#endif
}
void ThesaurusDialog::setUserPath(const QString &userDir)
{
ThesaurusDialog::userPath = userDir;
}
void ThesaurusDialog::setSearchWord(const QString &word)
{
if (!thesaurus) return;
searchWrdLe->setText(word);
replaceWrdLe->setText(word);
//clear Lists
classlistWidget->clear();
replacelistWidget->clear();
// do all the other calculations
QString lowerWord = word.trimmed().toLower();
QList<ThesaurusDatabaseType::TinyStringRef> result = thesaurus->thesaurus.values(QStringRef(&lowerWord, 0, lowerWord.length()));
// set word classes
QString first;
if (result.count() > 0) classlistWidget->addItem(tr("<all>"));
QStringList realCats;
foreach (ThesaurusDatabaseType::TinyStringRef elem, result) {
QString selem = elem.toStringWithBuffer(thesaurus->buffer);
first = selem.left(selem.indexOf('|'));
classlistWidget->addItem(first);
realCats << first.toLower();
}
foreach (const QString &c, thesaurus->userCategories.values(lowerWord))
if (!realCats.contains(c))
classlistWidget->addItem(c);
classlistWidget->setCurrentRow(0);
classChanged(0);
}
QString ThesaurusDialog::getReplaceWord()
{
QString word = replaceWrdLe->text();
word.replace(QRegExp(" \\(.*"), "");
return word;
}
void ThesaurusDialog::classChanged(int row)
{
if (!thesaurus || row < 0) return;
QString lowerWord = searchWrdLe->text().trimmed().toLower();
QList<ThesaurusDatabaseType::TinyStringRef> result = thesaurus->thesaurus.values(QStringRef(&lowerWord, 0, lowerWord.length()));
QStringList userCats = thesaurus->userCategories.values(lowerWord);
if (row - 1 >= userCats.size() + result.size()) return;
replacelistWidget->clear();
if (row == 0) {
foreach (ThesaurusDatabaseType::TinyStringRef elem, result)
addItems(elem.toStringWithBuffer(thesaurus->buffer));
foreach (const QString &elem, userCats)
addItems(QStringList(thesaurus->userWords.value(elem.toLower(), QStringList() << "").mid(1)).join("|"));
} else if (row - 1 < result.size())
addItems(result[row - 1].toStringWithBuffer(thesaurus->buffer));
else if (row - 1 - result.size() < userCats.size())
addItems(QStringList(thesaurus->userWords.value(userCats[row - 1 - result.size()].toLower(), QStringList() << "").mid(1)).join("|"));
}
void ThesaurusDialog::addItems(const QString &className)
{
if (replacelistWidget->count() == 0) duplicatesCheck.clear();
QStringList list = className.split("|");
if (list.isEmpty()) return;
if (thesaurus->userWords.contains(list.first().toLower()))
list << thesaurus->userWords.value(list.first().toLower()).mid(1);
foreach (const QString &w, list) {
if (duplicatesCheck.contains(w)) continue;
duplicatesCheck << w;
replacelistWidget->addItem(w);
}
}
void ThesaurusDialog::wordChanged(int row)
{
if (row < 0 || row >= replacelistWidget->count()) return;
replaceWrdLe->setText(replacelistWidget->item(row)->text());
}
void ThesaurusDialog::lookupClicked()
{
QString word = replaceWrdLe->text();
word.replace(QRegExp(" \\(.*"), "");
setSearchWord(word);
}
void ThesaurusDialog::containsClicked()
{
if (!thesaurus) return;
QString word = replaceWrdLe->text().trimmed().toLower();
word.replace(QRegExp(" \\(.*"), "");
classlistWidget->clear();
replacelistWidget->clear();
QMultiMap<QStringRef, ThesaurusDatabaseType::TinyStringRef>::const_iterator i = thesaurus->thesaurus.constBegin();
QMultiMap<QStringRef, ThesaurusDatabaseType::TinyStringRef>::const_iterator end = thesaurus->thesaurus.constEnd();
while (i != end) {
QString skey = i.key().toString();
if (!replacelistWidget->findItems(skey, Qt::MatchExactly).count() && skey.contains(word)) replacelistWidget->addItem(skey);
++i;
}
}
void ThesaurusDialog::startsWithClicked()
{
if (!thesaurus) return;
QString word = replaceWrdLe->text().trimmed().toLower();
word.replace(QRegExp(" \\(.*"), "");
classlistWidget->clear();
replacelistWidget->clear();
QMultiMap<QStringRef, ThesaurusDatabaseType::TinyStringRef>::const_iterator i = thesaurus->thesaurus.lowerBound(QStringRef(&word, 0, word.length()));
QMultiMap<QStringRef, ThesaurusDatabaseType::TinyStringRef>::const_iterator end = thesaurus->thesaurus.constEnd();
while (i != end) {
QString skey = i.key().toString();
if (!skey.startsWith(word)) break;
if (!replacelistWidget->findItems(skey, Qt::MatchExactly).count()) replacelistWidget->addItem(skey);
++i;
}
}
void ThesaurusDialog::addUserWordClicked()
{
if (!thesaurus) return;
QString word;
QStringList categories;
for (int i = 1; i < classlistWidget->count(); i++)
categories << classlistWidget->item(i)->text();
if (classlistWidget->currentItem() && categories.indexOf(classlistWidget->currentItem()->text()) >= 0 )
categories.swap(0, categories.indexOf(classlistWidget->currentItem()->text()));
UniversalInputDialog uid;
uid.addVariable(&word, tr("New Word:"));
uid.addVariable(&categories, tr("Category:"))->setEditable(true);
if (!uid.exec()) return;
if (categories.isEmpty()) return;
QString category = categories.first();
QStringList &sl = thesaurus->userWords[category.toLower()];
if (sl.contains(word)) return;
if (sl.isEmpty()) sl.append(category);
sl.append(word);
thesaurus->userCategories.insert(word.toLower(), category);
bool found = false;
for (int i = 0; i < classlistWidget->count(); i++)
if (classlistWidget->item(i)->text() == category) {
found = true;
break;
}
if (found) classChanged(classlistWidget->currentRow());
else {
QString oldWord = searchWrdLe->text();
if (!sl.contains(oldWord)) {
if (txsConfirm(tr("Do you want to add \"%1\" as synonym for \"%2\" or \"%3\"?").arg(oldWord).arg(word).arg(category))) {
sl.append(oldWord);
thesaurus->userCategories.insert(oldWord.toLower(), category);
}
}
setSearchWord(oldWord);
}
}
void ThesaurusDialog::removeUserWordClicked()
{
if (!thesaurus) return;
QString word, category;
if (replacelistWidget->currentItem()) word = replacelistWidget->currentItem()->text();
if (classlistWidget->currentItem()) category = classlistWidget->currentItem()->text();
UniversalInputDialog uid;
uid.addVariable(&word, tr("New Word:"));
uid.addVariable(&category, tr("Category:"));
if (!uid.exec()) return;
if (!thesaurus->userWords.contains(category.toLower())) return;
QStringList &sl = thesaurus->userWords[category.toLower()];
int i = sl.lastIndexOf(word);
if (i <= 0) return;
sl.removeAt(i);
classChanged(classlistWidget->currentRow());
}
void ThesaurusDialog::loadDatabase(const QString &fileName )
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) return;
QFileInfo fi(fileName);
ThesaurusDatabaseType result;
result.fileName = fileName;
result.userFileName = fi.absolutePath() + "/" + fi.completeBaseName() + ".my";
if (!isFileRealWritable(result.userFileName))
result.userFileName = userPath + fi.completeBaseName() + ".my";
if (!isFileRealWritable(result.userFileName))
result.userFileName.clear();
result.load(file);
thesaurusLock.lock();
if (globalThesaurusNeededFileName == fileName) {
globalThesaurus.saveUser();
globalThesaurus.clear();
globalThesaurus = result;
} else result.clear(); //our result isn't actually needed anymore
thesaurusLock.unlock();
}
ThesaurusDatabaseType *ThesaurusDialog::retrieveDatabase()
{
if (globalThesaurusNeededFileName == "") return 0;
#if QT_VERSION >= 0x040500
if (thesaurusFuture.isRunning())
thesaurusFuture.waitForFinished();
#else
if (globalThesaurus.fileName != globalThesaurusNeededFileName) {
loadDatabase(globalThesaurusNeededFileName);
}
#endif
if (globalThesaurus.fileName != globalThesaurusNeededFileName) return 0;
return &globalThesaurus;
}
|