Draft of normalization routine
authorBartosz Szatkowski <bulislaw@linux.com>
Tue, 24 Aug 2010 10:47:21 +0000 (12:47 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Tue, 24 Aug 2010 10:47:21 +0000 (12:47 +0200)
trunk/src/plugins/xdxf/src/XdxfCachingDialog.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.h

index c3b2cfd..48e6c11 100644 (file)
@@ -29,9 +29,9 @@
 
 
 XdxfCachingDialog::XdxfCachingDialog(XdxfPlugin *parent) :
-    QDialog(0)
+    QDialog((QWidget*)parent)
 {
-    verticalLayout = new QVBoxLayout;
+    verticalLayout = new QVBoxLayout(this);
     setLayout(verticalLayout);
 
     setWindowTitle(tr("Caching dictionary, please wait"));
@@ -61,6 +61,7 @@ XdxfCachingDialog::XdxfCachingDialog(XdxfPlugin *parent) :
 }
 
 void XdxfCachingDialog::updateCachingProgress(int progress, int time) {
+    qDebug() << "INININININININI";
     cachingProgressBar->setValue(progress);
 
     if(!cachingLabel->isVisible())
index e99f6ba..518031c 100644 (file)
@@ -31,12 +31,15 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent),
                     _type(tr("xdxf")), _infoNote(tr("")) {
     _wordsCount = -1;
     _settings = new Settings();
-    _dictDialog = new XdxfDictDialog(this, this);
-    cachingDialog = new XdxfCachingDialog(this);
+    _dictDialog = new XdxfDictDialog(this);
+    cachingDialog = new XdxfCachingDialog();
 
     connect(cachingDialog, SIGNAL(cancelCaching()),
             this, SLOT(stop()));
 
+    connect(this, SIGNAL(updateCachingProgress(int,int)),
+            cachingDialog, SLOT(updateCachingProgress(int,int)));
+
     _settings->setValue("type","xdxf");
 
     stopped = false;
@@ -100,7 +103,6 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         word = word.toLower();
         word = word.replace("*", "%");
         word = word.replace("?", "_");
-        word = removeAccents(word);
 
         QSqlQuery cur(db);
         if(limit !=0)
@@ -111,7 +113,9 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         if(limit !=0)
             cur.addBindValue(limit);
         cur.exec();
+        bool in = false;
         while(cur.next()){
+            in = true;
             bool ok=true;
             Translation *tran;
             foreach(tran,translations) {
@@ -123,16 +127,42 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
                         cur.value(0).toString().toLower(),
                         _infoNote, this));
         }
+        if(!in) {
+
+            QSqlQuery cur(db);
+            if(limit !=0)
+                cur.prepare("select word from dict where normalized like ? limit ?");
+            else
+                cur.prepare("select word from dict where normalized like ?");
+            cur.addBindValue(word);
+            if(limit !=0)
+                cur.addBindValue(limit);
+            cur.exec();
+            while(cur.next()){
+                bool ok=true;
+                Translation *tran;
+                foreach(tran,translations) {
+                    if(tran->key().toLower()==cur.value(0).toString().toLower())
+                            ok=false;
+                }
+                if(ok)  /*add key word to list*/
+                    translations.insert(new TranslationXdxf(
+                            cur.value(0).toString().toLower(),
+                            _infoNote, this));
+            }
+        }
         db.close();
     return translations.toList();
 }
 
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
+    QTime time;
+    time.start();
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
 
     word = word.toLower();
-    word = removeAccents(word);
+    //word = removeAccents(word);
 
     stopped = false;
     QRegExp regWord(word);
@@ -148,6 +178,9 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     QXmlStreamReader reader(&dictionaryFile);
     /*search words list*/
     QString a;
+    QRegExp regexp(removeAccents(a), Qt::CaseInsensitive, QRegExp::Wildcard);
+    regexp.setMinimal(true);
+
     int i=0;
     while(!reader.atEnd() && !stopped){
         reader.readNextStartElement();
@@ -156,7 +189,10 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                 reader.readNextStartElement();
             if(!reader.atEnd())
                 a = reader.readElementText();
-            if(regWord.exactMatch(removeAccents(a)) && (i<limit || limit==0)) {
+            regexp.setPattern(a);
+            if((regWord.exactMatch(a) || regexp.exactMatch(
+                    word.left(word.size()-1))) &&
+                    (i<limit || limit==0)) {
                 bool ok=true;
                 Translation *tran;
                 foreach(tran,translations) {
@@ -175,6 +211,7 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     }
     stopped=false;
     dictionaryFile.close();
+    qDebug() << time.elapsed();
     return translations.toList();
 }
 
@@ -378,20 +415,12 @@ void XdxfPlugin::getDictionaryInfo() {
     dictionaryFile.close();
 }
 
-QString XdxfPlugin::removeAccents(QString string) {
-    string = string.replace(QString::fromUtf8("ł"), "l", Qt::CaseInsensitive);
-    QString normalized = string.normalized(QString::NormalizationForm_D);
-    normalized = normalized;
+QString XdxfPlugin::removeAccents(QString string, QChar wildcard) {
+    QString normalized = string.toLower();
     for(int i=0; i<normalized.size(); i++) {
-        if( !normalized[i].isLetterOrNumber() &&
-            !normalized[i].isSpace() &&
-            !normalized[i].isDigit() &&
-            normalized[i] != '*' &&
-            normalized[i] != '%' &&
-            normalized[i] != '_' &&
-            normalized[i] != '?' ) {
-            normalized.remove(i,1);
-        }
+        if(normalized[i].isLetterOrNumber() && ((normalized.at(1) < QChar('a'))
+                || (normalized.at(i) > QChar('z'))))
+            normalized[i] = wildcard;
     }
     return normalized;
 }
@@ -448,7 +477,6 @@ bool XdxfPlugin::makeCache(QString dir) {
                 .arg(name()));
         return 0;
     }
-
     QXmlStreamReader reader(&dictionaryFile);
 
     db.setDatabaseName(cachePathN);
@@ -465,7 +493,7 @@ bool XdxfPlugin::makeCache(QString dir) {
     cur.exec("PRAGMA synchronous = 0");
     cur.exec("drop table dict");
     QCoreApplication::processEvents();
-    cur.exec("create table dict(word text ,translation text)");
+    cur.exec("create table dict(word text, normalized text ,translation text)");
     int counter = 0;
     cur.exec("BEGIN;");
 
@@ -510,13 +538,15 @@ bool XdxfPlugin::makeCache(QString dir) {
                 temp.remove(0,1);
             temp=tr("<key>") + a + tr("</key>") + tr("<t>") + temp+ tr("</t>");
             match=false;
-            cur.prepare("insert into dict values(?,?)");
+            cur.prepare("insert into dict values(?,?,?)");
             cur.addBindValue(a);
+            cur.addBindValue(removeAccents(a,'_'));
             cur.addBindValue(temp);
             cur.exec();
             counter++;
             int prog = counter*100/_wordsCount;
             if(prog % 5 == 0 && lastProg != prog) {
+                qDebug() << prog;
                 Q_EMIT updateCachingProgress(prog,
                                              timer.restart());
                 lastProg = prog;
index e413e2c..e7f3ff4 100644 (file)
@@ -120,7 +120,7 @@ Q_SIGNALS:
 
 
 protected:
-    QString removeAccents(QString);
+    QString removeAccents(QString, QChar wildcard= '?');
 
 private:
 /*! returns true or false depending on whether the dictionary is cached