From dc92d571f4f97f6420fdf1a94cc41c1d2808d71b Mon Sep 17 00:00:00 2001 From: John Fultz Date: Sun, 9 Aug 2015 05:13:19 -0500 Subject: Progress on edit lexicon dialog. * Files can now be loaded from user directory as well as app directory. * Edit lexicon dialog has been added, and pretty much all of the GUI elements framed out. Not actually implemented, yet. * Embiggen the board configuration dialog. * Some bits of code refactoring. --- quacker/lexicondialog.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 quacker/lexicondialog.cpp (limited to 'quacker/lexicondialog.cpp') diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp new file mode 100644 index 0000000..f812eed --- /dev/null +++ b/quacker/lexicondialog.cpp @@ -0,0 +1,134 @@ +/* + * Quackle -- Crossword game artificial intelligence and analysis tool + * Copyright (C) 2005-2014 Jason Katz-Brown and John O'Laughlin. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include + +#include "lexicondialog.h" +#include "customqsettings.h" +#include "geometry.h" + + +LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDialog(parent) +{ + m_originalName = originalName; + + resize(450,350); + + // construct the UI elements + m_lexiconName = new QLineEdit(); + m_alphabetCombo = new QComboBox(); + + m_addWordsFromFile = new QPushButton(tr("Add words from &file...")); + m_clearAllWords = new QPushButton(tr("Clear &words and start again")); + + + m_lexiconInformation = new QLabel(""); + m_lexiconInformation->setWordWrap(true); + + m_saveChanges = new QPushButton(tr("&Save Changes")); + m_cancel = new QPushButton(tr("&Cancel")); + m_deleteLexicon = new QPushButton(tr("&Delete Lexicon")); + + QLabel * lexiconNameLabel = new QLabel(tr("&Lexicon name:")); + QLabel * alphabetLabel = new QLabel(tr("&Alphabet:")); + lexiconNameLabel->setBuddy(m_lexiconName); + alphabetLabel->setBuddy(m_alphabetCombo); + + QVBoxLayout * layout = new QVBoxLayout; + Geometry::setupFramedLayout(layout); + QHBoxLayout * lexiconRow = new QHBoxLayout; + Geometry::setupInnerLayout(lexiconRow); + QHBoxLayout * addRemoveWordsRow = new QHBoxLayout; + Geometry::setupInnerLayout(addRemoveWordsRow); + QHBoxLayout * buttonRow = new QHBoxLayout; + Geometry::setupInnerLayout(buttonRow); + QGroupBox * lexiconInformationGroup = new QGroupBox(tr("Lexicon information:")); + QVBoxLayout * lexiconInformationLayout = new QVBoxLayout(lexiconInformationGroup); + + // build the layout + lexiconRow->addWidget(lexiconNameLabel); + lexiconRow->addWidget(m_lexiconName); + lexiconRow->addStretch(); + lexiconRow->addWidget(alphabetLabel); + lexiconRow->addWidget(m_alphabetCombo); + + addRemoveWordsRow->addWidget(m_addWordsFromFile); + addRemoveWordsRow->addWidget(m_clearAllWords); + + lexiconInformationLayout->addWidget(m_lexiconInformation); + + buttonRow->addWidget(m_deleteLexicon); + buttonRow->addStretch(); + buttonRow->addWidget(m_cancel); + buttonRow->addWidget(m_saveChanges); + + layout->addLayout(lexiconRow); + layout->addLayout(addRemoveWordsRow); + layout->addWidget(lexiconInformationGroup); + layout->addStretch(); + layout->addLayout(buttonRow); + + setLayout(layout); + m_saveChanges->setDefault(true); + + // hook up signals and slots + // connect(m_lexiconName, SIGNAL(textEdited(const QString &)), this, SLOT(parametersChanged(const QString &))); + connect(m_addWordsFromFile, SIGNAL(clicked()), this, SLOT(addWordsFromFile())); + connect(m_saveChanges, SIGNAL(clicked()), this, SLOT(accept())); + connect(m_cancel, SIGNAL(clicked()), this, SLOT(reject())); + connect(m_deleteLexicon, SIGNAL(clicked()), this, SLOT(deleteLexicon())); + + setWindowTitle(tr("Configure Lexicon - Quackle")); + updateLexiconInformation(); + + // sync game board with control states and draw board +} + +LexiconDialog::~LexiconDialog() +{ + +} + +void LexiconDialog::deleteLexicon() +{ + +} + +void LexiconDialog::addWordsFromFile() +{ + +} + +void LexiconDialog::accept() +{ + QDialog::accept(); +} + +void LexiconDialog::updateLexiconInformation() +{ + QString text; + text.append(tr("File name:")); + text.append(tr("\n\nFile size:")); + text.append(tr("\n\nWord count:")); + text.append(tr("\n\nLexicon hash:")); + + m_lexiconInformation->setText(text); +} -- cgit v1.2.3 From f814628b3aabef20db3320a5e58217f758fa2760 Mon Sep 17 00:00:00 2001 From: John Fultz Date: Tue, 18 Aug 2015 10:30:10 -0500 Subject: Populate alphabet popup in lexicon dialog. Moved Settings::populateComboFromFileNames() to be a static method, then invoked it from the lexicon dialog. --- quacker/lexicondialog.cpp | 13 ++++++++----- quacker/settings.cpp | 4 ++-- quacker/settings.h | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) (limited to 'quacker/lexicondialog.cpp') diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp index f812eed..9d1998c 100644 --- a/quacker/lexicondialog.cpp +++ b/quacker/lexicondialog.cpp @@ -23,6 +23,7 @@ #include "lexicondialog.h" #include "customqsettings.h" +#include "settings.h" #include "geometry.h" @@ -60,7 +61,7 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi Geometry::setupInnerLayout(addRemoveWordsRow); QHBoxLayout * buttonRow = new QHBoxLayout; Geometry::setupInnerLayout(buttonRow); - QGroupBox * lexiconInformationGroup = new QGroupBox(tr("Lexicon information:")); + QGroupBox * lexiconInformationGroup = new QGroupBox(tr("Lexicon information")); QVBoxLayout * lexiconInformationLayout = new QVBoxLayout(lexiconInformationGroup); // build the layout @@ -97,6 +98,8 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi connect(m_deleteLexicon, SIGNAL(clicked()), this, SLOT(deleteLexicon())); setWindowTitle(tr("Configure Lexicon - Quackle")); + + Settings::populateComboFromFilenames(m_alphabetCombo, "alphabets", ""); updateLexiconInformation(); // sync game board with control states and draw board @@ -125,10 +128,10 @@ void LexiconDialog::accept() void LexiconDialog::updateLexiconInformation() { QString text; - text.append(tr("File name:")); - text.append(tr("\n\nFile size:")); - text.append(tr("\n\nWord count:")); - text.append(tr("\n\nLexicon hash:")); + text.append(tr("File name: ")); + text.append(tr("\n\nFile size: ")); + text.append(tr("\n\nWord count: ")); + text.append(tr("\n\nLexicon hash: ")); m_lexiconInformation->setText(text); } diff --git a/quacker/settings.cpp b/quacker/settings.cpp index c516b91..3c42a39 100644 --- a/quacker/settings.cpp +++ b/quacker/settings.cpp @@ -476,10 +476,10 @@ void Settings::editTheme() void Settings::populateComboFromFilenames(QComboBox* combo, const QString &path, const QString &label) { QStringList fileList; - QDir dir(m_appDataDir); + QDir dir(self()->m_appDataDir); if (dir.cd(path)) fileList << dir.entryList(QDir::Files | QDir::Readable, QDir::Name); - dir = QDir(m_userDataDir); + dir = QDir(self()->m_userDataDir); if (dir.cd(path)) fileList << dir.entryList(QDir::Files | QDir::Readable, QDir::Name); diff --git a/quacker/settings.h b/quacker/settings.h index ee1b59a..cee0562 100644 --- a/quacker/settings.h +++ b/quacker/settings.h @@ -39,6 +39,9 @@ public: static Settings *self(); + // load up an item list based on a list of filenames + static void populateComboFromFilenames(QComboBox* combo, const QString &path, const QString &label); + signals: void refreshViews(); @@ -91,9 +94,6 @@ private: // populate the popup based on what's in QSettings void loadBoardNameCombo(); - // load up an item list based on a list of filenames - void populateComboFromFilenames(QComboBox* combo, const QString &path, const QString &label); - static Settings *m_self; }; -- cgit v1.2.3 From 1214533715a1acfbc35ebe29ff78afee2f850226 Mon Sep 17 00:00:00 2001 From: John Fultz Date: Sat, 26 Sep 2015 10:47:07 -0500 Subject: Work on DAWG generation. V1 DAWGs now include an alphabet. Begin creating DAWGs which extend other DAWGs. In general, laying the groundwork for plain text import to DAWG. --- lexiconparameters.cpp | 8 ++++++++ lexiconparameters.h | 5 +++++ quacker/lexicondialog.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- quacker/lexicondialog.h | 11 ++++++++++- quackleio/dawgfactory.cpp | 31 +++++++++++++++++++++---------- quackleio/dawgfactory.h | 1 + 6 files changed, 86 insertions(+), 13 deletions(-) (limited to 'quacker/lexicondialog.cpp') diff --git a/lexiconparameters.cpp b/lexiconparameters.cpp index 6761fc1..9da3b70 100644 --- a/lexiconparameters.cpp +++ b/lexiconparameters.cpp @@ -75,6 +75,14 @@ class Quackle::V1LexiconInterpreter : public LexiconInterpreter file.get(); // skip past version byte file.read(lexparams.m_hash, sizeof(lexparams.m_hash)); file.read((char*)bytes, 3); + + lexparams.m_utf8Alphabet.resize(file.get()); + for (size_t i = 0; i < lexparams.m_utf8Alphabet.size(); i++) + { + file >> lexparams.m_utf8Alphabet[i]; + file.get(); // separator space + } + file.get(); // whitespace separator lexparams.m_wordcount = (bytes[0] << 16) | (bytes[1] << 8) | bytes[2]; while (!file.eof()) { diff --git a/lexiconparameters.h b/lexiconparameters.h index 3890d8d..9f34be6 100644 --- a/lexiconparameters.h +++ b/lexiconparameters.h @@ -19,11 +19,15 @@ #ifndef QUACKLE_LEXICONPARAMETERS_H #define QUACKLE_LEXICONPARAMETERS_H +#include + #include "gaddag.h" namespace Quackle { +class LexiconParameters; + class LexiconInterpreter { public: @@ -84,6 +88,7 @@ protected: LexiconInterpreter *m_interpreter; char m_hash[16]; int m_wordcount; + vector m_utf8Alphabet; LexiconInterpreter* createInterpreter(char version) const; }; diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp index 9d1998c..f9c6399 100644 --- a/quacker/lexicondialog.cpp +++ b/quacker/lexicondialog.cpp @@ -25,9 +25,10 @@ #include "customqsettings.h" #include "settings.h" #include "geometry.h" +#include "quackleio/dawgfactory.h" - -LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDialog(parent) +LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDialog(parent), + m_wordFactory(NULL) { m_originalName = originalName; @@ -117,7 +118,45 @@ void LexiconDialog::deleteLexicon() void LexiconDialog::addWordsFromFile() { + QFileDialog browser(this, tr("Choose a file containing words to be added to the lexicon...")); +} + +void LexiconDialog::addWordsFromDawg(const string &dawgfile, const string &alphabetfile) +{ + delete m_wordFactory; + m_wordFactory = NULL; + + LexiconParameters lexParams; + lexParams.loadDawg(dawgfile); + if (!lexParams.hasDawg()) + return; + m_wordFactory = new DawgFactory(alphabetfile); + Quackle::LetterString word; + + addWordsFromDawgRecursive(lexParams, word, 1); +} + +void LexiconDialog::addWordsFromDawgRecursive(const LexiconParameters &lexParams, Quackle::LetterString &word, int index) +{ + unsigned int p; + Quackle::Letter letter; + bool t; + bool lastchild; + bool british; + int playability; + + do + { + lexParams.dawgAt(index, p, letter, t, lastchild, british, playability); + word.push_back(letter); + if (t) + m_wordFactory->pushWord(word, !british, playability); + if (p) + addWordsFromDawgRecursive(lexParams, word, p); + index++; + word.pop_back(); + } while (!lastchild); } void LexiconDialog::accept() diff --git a/quacker/lexicondialog.h b/quacker/lexicondialog.h index cdc0a59..573d48b 100644 --- a/quacker/lexicondialog.h +++ b/quacker/lexicondialog.h @@ -20,17 +20,20 @@ #define QUACKER_LEXICONDIALOG_H #include -#include +#include "game.h" +#include "lexiconparameters.h" #include #include using namespace std; +using namespace Quackle; class QComboBox; class QLabel; class QLineEdit; class QPushButton; +class DawgFactory; class LexiconDialog : public QDialog { @@ -47,6 +50,10 @@ protected slots: void deleteLexicon(); void addWordsFromFile(); +protected: + void addWordsFromDawg(const string &dawgfile, const string &alphabetfile); + void addWordsFromDawgRecursive(const LexiconParameters &lexParams, Quackle::LetterString &word, int index); + private: QLineEdit *m_lexiconName; QComboBox *m_alphabetCombo; @@ -59,6 +66,8 @@ private: QPushButton *m_deleteLexicon; QString m_originalName; + + DawgFactory *m_wordFactory; }; #endif diff --git a/quackleio/dawgfactory.cpp b/quackleio/dawgfactory.cpp index 3a971a3..565778a 100644 --- a/quackleio/dawgfactory.cpp +++ b/quackleio/dawgfactory.cpp @@ -50,21 +50,24 @@ bool DawgFactory::pushWord(const UVString& word, bool inSmaller, int playability UVString leftover; Quackle::LetterString encodedWord = m_alphas->encode(word, &leftover); if (leftover.empty()) - { - if (m_root.pushWord(encodedWord, inSmaller, playability)) - { - ++m_encodableWords; - hashWord(encodedWord); - return true; - } - ++m_duplicateWords; - return false; - } + return pushWord(encodedWord, inSmaller, playability); ++m_unencodableWords; return false; } +bool DawgFactory::pushWord(const Quackle::LetterString& word, bool inSmaller, int playability) +{ + if (m_root.pushWord(word, inSmaller, playability)) + { + ++m_encodableWords; + hashWord(word); + return true; + } + ++m_duplicateWords; + return false; +} + void DawgFactory::hashWord(const Quackle::LetterString &word) { QCryptographicHash wordhash(QCryptographicHash::Md5); @@ -139,6 +142,14 @@ void DawgFactory::writeIndex(const UVString& filename) out.put(1); // DAWG format version 1 out.write(m_hash.charptr, sizeof(m_hash.charptr)); out.write((char*)bytes, 3); + out.put((char)m_alphas->length()); + for (Quackle::Letter i = m_alphas->firstLetter(); i <= m_alphas->lastLetter(); i++) + { + QString letterText = QuackleIO::Util::uvStringToQString(m_alphas->letterParameter(i).text()); + QByteArray utf8bytes = letterText.toUtf8(); + string utf8LetterText(utf8bytes.constData()); + out << utf8LetterText << ' '; + } for (unsigned int i = 0; i < m_nodelist.size(); i++) { //cout << m_nodelist[i]->c << " " << m_nodelist[i]->pointer << " " << m_nodelist[i]->t << " " << m_nodelist[i]->lastchild << endl; diff --git a/quackleio/dawgfactory.h b/quackleio/dawgfactory.h index 051e632..2a55461 100644 --- a/quackleio/dawgfactory.h +++ b/quackleio/dawgfactory.h @@ -36,6 +36,7 @@ public: int duplicateWords() const { return m_duplicateWords; }; bool pushWord(const UVString& word, bool inSmaller, int playability); + bool pushWord(const Quackle::LetterString& word, bool inSmaller, int playability); void hashWord(const Quackle::LetterString &word); void generate(); void writeIndex(const UVString& filename); -- cgit v1.2.3 From 81554a201cc5e0748110add6eca05cc16c18850c Mon Sep 17 00:00:00 2001 From: John Fultz Date: Mon, 28 Sep 2015 13:00:57 -0500 Subject: Now able to load text and dawg files. Words are loaded and hashed. Duplicates are discovered. Alphabets are dealt with. Merging of multiple word lists works. This is good stuff. Saving the resulting dictionaries has not been tried, yet, and the gui code for saving needs to be finished off, yet. --- quacker/lexicondialog.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++---- quacker/lexicondialog.h | 5 +++- quackleio/dawgfactory.cpp | 4 +-- quackleio/dawgfactory.h | 2 +- 4 files changed, 73 insertions(+), 9 deletions(-) (limited to 'quacker/lexicondialog.cpp') diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp index f9c6399..a7566c6 100644 --- a/quacker/lexicondialog.cpp +++ b/quacker/lexicondialog.cpp @@ -97,10 +97,12 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi connect(m_saveChanges, SIGNAL(clicked()), this, SLOT(accept())); connect(m_cancel, SIGNAL(clicked()), this, SLOT(reject())); connect(m_deleteLexicon, SIGNAL(clicked()), this, SLOT(deleteLexicon())); - + connect(m_alphabetCombo, SIGNAL(activated(const QString &)), this, SLOT(alphabetChanged(const QString &))); + setWindowTitle(tr("Configure Lexicon - Quackle")); Settings::populateComboFromFilenames(m_alphabetCombo, "alphabets", ""); + alphabetChanged(m_alphabetCombo->currentText()); updateLexiconInformation(); // sync game board with control states and draw board @@ -113,25 +115,49 @@ LexiconDialog::~LexiconDialog() void LexiconDialog::deleteLexicon() { - + delete m_wordFactory; + m_wordFactory = NULL; + updateLexiconInformation(); } void LexiconDialog::addWordsFromFile() { QFileDialog browser(this, tr("Choose a file containing words to be added to the lexicon...")); + QStringList filters; + filters << "Dictionary files (*.txt *.dawg *.raw)" + << "All files (*.*)"; + browser.setNameFilters(filters); + browser.setFileMode(QFileDialog::ExistingFiles); + browser.exec(); + + QStringList files = browser.selectedFiles(); + for (QList::const_iterator it = files.begin(); it != files.end(); it++) + { + if (it->endsWith(".dawg", Qt::CaseInsensitive)) + addWordsFromDawgFile(*it, m_alphabetCombo->currentText()); + else + addWordsFromTextFile(*it, m_alphabetCombo->currentText()); + } + updateLexiconInformation(); } -void LexiconDialog::addWordsFromDawg(const string &dawgfile, const string &alphabetfile) +void LexiconDialog::alphabetChanged(const QString &alphabet) { delete m_wordFactory; m_wordFactory = NULL; + updateLexiconInformation(); + m_alphabetFileName = QString::fromStdString(AlphabetParameters::findAlphabetFile(QuackleIO::Util::qstringToStdString(alphabet))); +} +void LexiconDialog::addWordsFromDawgFile(const QString &dawgfile, const QString &alphabetfile) +{ + if (!m_wordFactory) + m_wordFactory = new DawgFactory(m_alphabetFileName); LexiconParameters lexParams; - lexParams.loadDawg(dawgfile); + lexParams.loadDawg(QuackleIO::Util::qstringToStdString(dawgfile)); if (!lexParams.hasDawg()) return; - m_wordFactory = new DawgFactory(alphabetfile); Quackle::LetterString word; addWordsFromDawgRecursive(lexParams, word, 1); @@ -159,6 +185,37 @@ void LexiconDialog::addWordsFromDawgRecursive(const LexiconParameters &lexParams } while (!lastchild); } +void LexiconDialog::addWordsFromTextFile(const QString &textFile, const QString &alphabetfile) +{ + if (!m_wordFactory) + m_wordFactory = new DawgFactory(m_alphabetFileName); + + QFile file(textFile); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + return; + + QTextStream stream(&file); + stream.setCodec("UTF-8"); + QString word; + while (!stream.atEnd()) + { + stream >> word; + word = word.trimmed().toUpper(); + if (word.isEmpty()) + continue; + QChar firstChar = word[0]; + if (firstChar < 'A') + continue; // allows the usage of most punctuation characters as comments + int playability = 0; + for (int i = word.size() - 1; i > 0; i--) + { + if (word[i].isDigit()) + playability = playability * 10 + word[i].digitValue(); + } + m_wordFactory->pushWord(QuackleIO::Util::qstringToString(word), true, playability); + } +} + void LexiconDialog::accept() { QDialog::accept(); @@ -166,11 +223,15 @@ void LexiconDialog::accept() void LexiconDialog::updateLexiconInformation() { + int wordCount = m_wordFactory ? m_wordFactory->wordCount() : 0; + QByteArray hash = m_wordFactory ? QByteArray(m_wordFactory->hashBytes(), 16).toHex() : ""; QString text; text.append(tr("File name: ")); text.append(tr("\n\nFile size: ")); text.append(tr("\n\nWord count: ")); + text.append(QString("%L1").arg(wordCount)); text.append(tr("\n\nLexicon hash: ")); + text.append(hash); m_lexiconInformation->setText(text); } diff --git a/quacker/lexicondialog.h b/quacker/lexicondialog.h index 573d48b..4df6138 100644 --- a/quacker/lexicondialog.h +++ b/quacker/lexicondialog.h @@ -49,10 +49,12 @@ public: protected slots: void deleteLexicon(); void addWordsFromFile(); + void alphabetChanged(const QString &); protected: - void addWordsFromDawg(const string &dawgfile, const string &alphabetfile); + void addWordsFromDawgFile(const QString &dawgfile, const QString &alphabetfile); void addWordsFromDawgRecursive(const LexiconParameters &lexParams, Quackle::LetterString &word, int index); + void addWordsFromTextFile(const QString &textFile, const QString &alphabetfile); private: QLineEdit *m_lexiconName; @@ -66,6 +68,7 @@ private: QPushButton *m_deleteLexicon; QString m_originalName; + QString m_alphabetFileName; DawgFactory *m_wordFactory; }; diff --git a/quackleio/dawgfactory.cpp b/quackleio/dawgfactory.cpp index 565778a..e7ada85 100644 --- a/quackleio/dawgfactory.cpp +++ b/quackleio/dawgfactory.cpp @@ -25,10 +25,10 @@ #include "util.h" -DawgFactory::DawgFactory(const UVString& alphabetFile) +DawgFactory::DawgFactory(const QString &alphabetFile) { QuackleIO::FlexibleAlphabetParameters *flexure = new QuackleIO::FlexibleAlphabetParameters; - flexure->load(QuackleIO::Util::uvStringToQString(alphabetFile)); + flexure->load(alphabetFile); m_alphas = flexure; m_root.insmallerdict = false; diff --git a/quackleio/dawgfactory.h b/quackleio/dawgfactory.h index 2a55461..1a1aa7d 100644 --- a/quackleio/dawgfactory.h +++ b/quackleio/dawgfactory.h @@ -26,7 +26,7 @@ class DawgFactory { public: - DawgFactory(const UVString& alphabetFile); + DawgFactory(const QString &alphabetFile); ~DawgFactory(); int wordCount() const { return m_root.wordCount(); }; -- cgit v1.2.3 From 69e3dcefb882c743b136df8e5c81b4182b135f6b Mon Sep 17 00:00:00 2001 From: John Fultz Date: Sat, 10 Oct 2015 19:09:36 -0500 Subject: Progress on the lexicon dialog. Now prints better stats. Now loads the dictionary you're editing. Now disables the Delete button at appropriate times. --- datamanager.cpp | 6 ++++++ datamanager.h | 3 +++ lexiconparameters.cpp | 5 +++++ lexiconparameters.h | 1 + quacker/lexicondialog.cpp | 36 +++++++++++++++++++++++++++--------- quacker/lexicondialog.h | 4 ++-- quacker/settings.cpp | 30 ++++++++++++++++++++++++++---- quacker/settings.h | 2 ++ quackleio/dawgfactory.cpp | 37 +++++++++++++++++++++++++++++++++++-- quackleio/dawgfactory.h | 8 ++++++-- 10 files changed, 113 insertions(+), 19 deletions(-) (limited to 'quacker/lexicondialog.cpp') diff --git a/datamanager.cpp b/datamanager.cpp index eb65afd..916610a 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -156,6 +156,12 @@ string DataManager::findDataFile(const string &subDirectory, const string &file) return fname; } +bool DataManager::hasUserDataFile(const string &subDirectory, const string &file) +{ + string fname = makeDataFilename(subDirectory, file, true); + return fileExists(fname); +} + string DataManager::makeDataFilename(const string &subDirectory, const string &lexicon, const string &file, bool user) { return (user ? m_userDataDirectory : m_appDataDirectory) + "/" + subDirectory + "/" + lexicon + "/" + file; diff --git a/datamanager.h b/datamanager.h index 196d525..75bce54 100644 --- a/datamanager.h +++ b/datamanager.h @@ -105,6 +105,9 @@ public: // Returns empty string if the file is not found. string findDataFile(const string &subDirectory, const string &file); + // Returns true if the data file is in user-land. + bool hasUserDataFile(const string &subDirectory, const string &file); + // returns similarly-named file string makeDataFilename(const string &subDirectory, const string &lexicon, const string &file, bool user); string makeDataFilename(const string &subDirectory, const string &file, bool user); diff --git a/lexiconparameters.cpp b/lexiconparameters.cpp index 9da3b70..bc10773 100644 --- a/lexiconparameters.cpp +++ b/lexiconparameters.cpp @@ -224,6 +224,11 @@ string LexiconParameters::findDictionaryFile(const string &lexicon) return QUACKLE_DATAMANAGER->findDataFile("lexica", lexicon); } +bool LexiconParameters::hasUserDictionaryFile(const string &lexicon) +{ + return QUACKLE_DATAMANAGER->hasUserDataFile("lexica", lexicon); +} + UVString LexiconParameters::hashString(bool shortened) const { const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; diff --git a/lexiconparameters.h b/lexiconparameters.h index 9f34be6..4eda4f3 100644 --- a/lexiconparameters.h +++ b/lexiconparameters.h @@ -68,6 +68,7 @@ public: // finds a file in the lexica data directory static string findDictionaryFile(const string &lexicon); + static bool hasUserDictionaryFile(const string &lexicon); // a convenience field; this is unused by libquackle string lexiconName() const { return m_lexiconName; }; diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp index a7566c6..e11ae41 100644 --- a/quacker/lexicondialog.cpp +++ b/quacker/lexicondialog.cpp @@ -103,9 +103,21 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi Settings::populateComboFromFilenames(m_alphabetCombo, "alphabets", ""); alphabetChanged(m_alphabetCombo->currentText()); - updateLexiconInformation(); - // sync game board with control states and draw board + string dawgFileName = originalName.toStdString() + ".dawg"; + QString dawgFullFileName; + if (!originalName.isEmpty()) + dawgFullFileName = QString::fromStdString(Quackle::LexiconParameters::findDictionaryFile(dawgFileName)); + + if (!dawgFullFileName.isEmpty()) + { + m_deleteLexicon->setEnabled(Quackle::LexiconParameters::hasUserDictionaryFile(dawgFileName)); + addWordsFromDawgFile(dawgFullFileName); + } + else + m_deleteLexicon->setEnabled(false); + + updateLexiconInformation(); } LexiconDialog::~LexiconDialog() @@ -134,9 +146,9 @@ void LexiconDialog::addWordsFromFile() for (QList::const_iterator it = files.begin(); it != files.end(); it++) { if (it->endsWith(".dawg", Qt::CaseInsensitive)) - addWordsFromDawgFile(*it, m_alphabetCombo->currentText()); + addWordsFromDawgFile(*it); else - addWordsFromTextFile(*it, m_alphabetCombo->currentText()); + addWordsFromTextFile(*it); } updateLexiconInformation(); } @@ -149,7 +161,7 @@ void LexiconDialog::alphabetChanged(const QString &alphabet) m_alphabetFileName = QString::fromStdString(AlphabetParameters::findAlphabetFile(QuackleIO::Util::qstringToStdString(alphabet))); } -void LexiconDialog::addWordsFromDawgFile(const QString &dawgfile, const QString &alphabetfile) +void LexiconDialog::addWordsFromDawgFile(const QString &dawgfile) { if (!m_wordFactory) m_wordFactory = new DawgFactory(m_alphabetFileName); @@ -185,7 +197,7 @@ void LexiconDialog::addWordsFromDawgRecursive(const LexiconParameters &lexParams } while (!lastchild); } -void LexiconDialog::addWordsFromTextFile(const QString &textFile, const QString &alphabetfile) +void LexiconDialog::addWordsFromTextFile(const QString &textFile) { if (!m_wordFactory) m_wordFactory = new DawgFactory(m_alphabetFileName); @@ -224,14 +236,20 @@ void LexiconDialog::accept() void LexiconDialog::updateLexiconInformation() { int wordCount = m_wordFactory ? m_wordFactory->wordCount() : 0; - QByteArray hash = m_wordFactory ? QByteArray(m_wordFactory->hashBytes(), 16).toHex() : ""; + QByteArray hash = (m_wordFactory && wordCount) ? QByteArray(m_wordFactory->hashBytes(), 16).toHex() : ""; QString text; + QString lengthText; + if (m_wordFactory) + lengthText = QString::fromStdString(m_wordFactory->letterCountString()); + text.append(tr("File name: ")); text.append(tr("\n\nFile size: ")); text.append(tr("\n\nWord count: ")); text.append(QString("%L1").arg(wordCount)); - text.append(tr("\n\nLexicon hash: ")); - text.append(hash); + text.append("\n"); + text.append(lengthText); + text.append(tr("\nLexicon hash: ")); + text.append(hash.left(8)); m_lexiconInformation->setText(text); } diff --git a/quacker/lexicondialog.h b/quacker/lexicondialog.h index 4df6138..39cd546 100644 --- a/quacker/lexicondialog.h +++ b/quacker/lexicondialog.h @@ -52,9 +52,9 @@ protected slots: void alphabetChanged(const QString &); protected: - void addWordsFromDawgFile(const QString &dawgfile, const QString &alphabetfile); + void addWordsFromDawgFile(const QString &dawgfile); void addWordsFromDawgRecursive(const LexiconParameters &lexParams, Quackle::LetterString &word, int index); - void addWordsFromTextFile(const QString &textFile, const QString &alphabetfile); + void addWordsFromTextFile(const QString &textFile); private: QLineEdit *m_lexiconName; diff --git a/quacker/settings.cpp b/quacker/settings.cpp index ce8583f..3319955 100644 --- a/quacker/settings.cpp +++ b/quacker/settings.cpp @@ -172,9 +172,11 @@ void Settings::createGUI() void Settings::load() { m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_LEXICON_PARAMETERS->lexiconName()))); + m_lastGoodLexiconValue = m_lexiconNameCombo->currentIndex(); m_alphabetNameCombo->setCurrentIndex(m_alphabetNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_ALPHABET_PARAMETERS->alphabetName()))); m_themeNameCombo->setCurrentIndex(m_themeNameCombo->findText(m_themeName)); m_boardNameCombo->setCurrentIndex(m_boardNameCombo->findText(QuackleIO::Util::uvStringToQString(QUACKLE_BOARD_PARAMETERS->name()))); + m_lastGoodBoardValue = m_boardNameCombo->currentIndex(); } void Settings::preInitialize() @@ -343,9 +345,13 @@ void Settings::lexiconChanged(const QString &lexiconName) if (m_lexiconNameCombo->currentIndex() == m_lexiconNameCombo->count() - 1) { editLexicon(); + if (m_lexiconNameCombo->currentIndex() == m_lexiconNameCombo->count() - 1 && + m_lexiconNameCombo->currentIndex() != 0) + m_lexiconNameCombo->setCurrentIndex(m_lastGoodLexiconValue); return; } setQuackleToUseLexiconName(lexiconName); + m_lastGoodLexiconValue = m_lexiconNameCombo->currentIndex(); CustomQSettings settings; settings.setValue("quackle/settings/lexicon-name", lexiconName); @@ -388,6 +394,9 @@ void Settings::boardChanged(const QString &boardName) if (m_boardNameCombo->currentIndex() == m_boardNameCombo->count() - 1) { addBoard(); + if (m_boardNameCombo->currentIndex() == m_boardNameCombo->count() - 1 && + m_boardNameCombo->currentIndex() != 0) + m_boardNameCombo->setCurrentIndex(m_lastGoodBoardValue); return; } CustomQSettings settings; @@ -530,14 +539,14 @@ void Settings::populateComboFromFilenames(QComboBox* combo, const QString &path, if (dir.cd(path)) fileList << dir.entryList(QDir::Files | QDir::Readable, QDir::Name); - QStringListIterator i(fileList); + QStringList::iterator i; QString fileName; QStringList list; int periodPos; - while (i.hasNext()) + for (i = fileList.begin(); i != fileList.end(); ++i) { - fileName = i.next(); + fileName = *i; periodPos = fileName.indexOf('.'); if (periodPos) { @@ -545,7 +554,20 @@ void Settings::populateComboFromFilenames(QComboBox* combo, const QString &path, list << fileName; } } - list.removeDuplicates(); + + for (i = fileList.begin(); i != fileList.end(); ++i) + { + QStringList::iterator j = i; + for (++j; j != fileList.end(); ++j) + { + if (*i == *j) + { + *i = "* " + *i; + list.erase(j); + break; + } + } + } combo->addItems(list); if (label.size() > 0) diff --git a/quacker/settings.h b/quacker/settings.h index fab2f3f..7c5738e 100644 --- a/quacker/settings.h +++ b/quacker/settings.h @@ -100,6 +100,8 @@ private: void pushIndex(GaddagFactory &factory, Quackle::LetterString &word, int index); static Settings *m_self; + int m_lastGoodLexiconValue; + int m_lastGoodBoardValue; }; #endif diff --git a/quackleio/dawgfactory.cpp b/quackleio/dawgfactory.cpp index e7ada85..362dfdc 100644 --- a/quackleio/dawgfactory.cpp +++ b/quackleio/dawgfactory.cpp @@ -17,6 +17,8 @@ */ +#include +#include #include #include #include @@ -184,6 +186,34 @@ void DawgFactory::writeIndex(const UVString& filename) } } +int DawgFactory::wordCount() const +{ + m_countsByLength.resize(0); + return m_root.wordCount(0, m_countsByLength); +} + +string DawgFactory::letterCountString() const +{ + ostringstream str; + if (m_countsByLength.size() < 16) + m_countsByLength.resize(16, 0); + str << "2s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[2]; + str << "\t6s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[6]; + str << "\t10s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[10]; + str << "\t14s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[14]; + str << "\n3s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[3]; + str << "\t7s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[7]; + str << "\t11s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[11]; + str << "\t15s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[15]; + str << "\n4s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[4]; + str << "\t8s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[8]; + str << "\t12s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[12]; + str << "\n5s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[5]; + str << "\t9s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[9]; + str << "\t13s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[13]; + str << "\n"; + return str.str(); +} void DawgFactory::Node::print(vector< Node* >& nodelist) @@ -287,11 +317,14 @@ bool DawgFactory::Node::equals(const Node &n) const return true; } -int DawgFactory::Node::wordCount() const +int DawgFactory::Node::wordCount(unsigned int depth, vector &countsByLength) const { int wordCount = ((playability == 0) ? 0 : 1); + if (countsByLength.size() < depth + 1) + countsByLength.resize(depth + 1, 0); + countsByLength[depth] += wordCount; for (size_t i = 0; i < children.size(); i++) - wordCount += children[i].wordCount(); + wordCount += children[i].wordCount(depth + 1, countsByLength); return wordCount; } diff --git a/quackleio/dawgfactory.h b/quackleio/dawgfactory.h index 1a1aa7d..8dd6e03 100644 --- a/quackleio/dawgfactory.h +++ b/quackleio/dawgfactory.h @@ -19,6 +19,7 @@ #ifndef QUACKLE_DAWGFACTORY_H #define QUACKLE_DAWGFACTORY_H +#include #include #include "flexiblealphabet.h" @@ -29,7 +30,8 @@ public: DawgFactory(const QString &alphabetFile); ~DawgFactory(); - int wordCount() const { return m_root.wordCount(); }; + int wordCount() const; + string letterCountString() const; int nodeCount() const { return m_nodelist.size(); }; int encodableWords() const { return m_encodableWords; }; int unencodableWords() const { return m_unencodableWords; }; @@ -50,7 +52,7 @@ private: void print(vector< Node* >& m_nodelist); int letterSum() const; - int wordCount() const; + int wordCount(unsigned int depth, vector &countsByLength) const; bool equals(const Node &n) const; Quackle::Letter c; @@ -65,6 +67,7 @@ private: mutable bool sumexplored; mutable int sum; + mutable vector counts; bool deleted; Node* cloneof; @@ -75,6 +78,7 @@ private: int m_unencodableWords; int m_duplicateWords; vector< Node* > m_nodelist; + mutable vector m_countsByLength; Quackle::AlphabetParameters *m_alphas; Node m_root; union { -- cgit v1.2.3 From 6339dec22e2190fd341500206c80425593324bdc Mon Sep 17 00:00:00 2001 From: John Fultz Date: Sun, 11 Oct 2015 18:19:20 -0500 Subject: Fix up lexicon dialog box checks. Get enables and disables right, efficient computation of word counts, etc. --- data/lexica/csw12.gaddag | Bin 25846356 -> 0 bytes data/lexica/twl06.gaddag | Bin 16811716 -> 0 bytes quacker/lexicondialog.cpp | 46 ++++++++++++++++++++++++++++++++++++++++------ quacker/lexicondialog.h | 7 ++++++- quackleio/dawgfactory.cpp | 5 +++-- quackleio/dawgfactory.h | 4 +++- 6 files changed, 52 insertions(+), 10 deletions(-) delete mode 100644 data/lexica/csw12.gaddag delete mode 100644 data/lexica/twl06.gaddag (limited to 'quacker/lexicondialog.cpp') diff --git a/data/lexica/csw12.gaddag b/data/lexica/csw12.gaddag deleted file mode 100644 index 511c99f..0000000 Binary files a/data/lexica/csw12.gaddag and /dev/null differ diff --git a/data/lexica/twl06.gaddag b/data/lexica/twl06.gaddag deleted file mode 100644 index db93e2e..0000000 Binary files a/data/lexica/twl06.gaddag and /dev/null differ diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp index e11ae41..f92efb1 100644 --- a/quacker/lexicondialog.cpp +++ b/quacker/lexicondialog.cpp @@ -27,6 +27,18 @@ #include "geometry.h" #include "quackleio/dawgfactory.h" +class FileNameValidator : public QValidator +{ +public: + virtual State validate(QString &input, int &pos) const + { + for (QString::ConstIterator i = input.begin(); i != input.end(); ++i) + if (*i == '/' || *i == '?' || *i == '\\' || *i == '*') + return Invalid; + return Acceptable; + } +}; + LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDialog(parent), m_wordFactory(NULL) { @@ -37,6 +49,7 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi // construct the UI elements m_lexiconName = new QLineEdit(); m_alphabetCombo = new QComboBox(); + m_fileNameValidator = new FileNameValidator(); m_addWordsFromFile = new QPushButton(tr("Add words from &file...")); m_clearAllWords = new QPushButton(tr("Clear &words and start again")); @@ -92,7 +105,7 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi m_saveChanges->setDefault(true); // hook up signals and slots - // connect(m_lexiconName, SIGNAL(textEdited(const QString &)), this, SLOT(parametersChanged(const QString &))); + connect(m_lexiconName, SIGNAL(textEdited(const QString &)), this, SLOT(parametersChanged(const QString &))); connect(m_addWordsFromFile, SIGNAL(clicked()), this, SLOT(addWordsFromFile())); connect(m_saveChanges, SIGNAL(clicked()), this, SLOT(accept())); connect(m_cancel, SIGNAL(clicked()), this, SLOT(reject())); @@ -109,6 +122,9 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi if (!originalName.isEmpty()) dawgFullFileName = QString::fromStdString(Quackle::LexiconParameters::findDictionaryFile(dawgFileName)); + m_lexiconName->setValidator(m_fileNameValidator); + m_lexiconName->setText(m_originalName); + if (!dawgFullFileName.isEmpty()) { m_deleteLexicon->setEnabled(Quackle::LexiconParameters::hasUserDictionaryFile(dawgFileName)); @@ -117,12 +133,13 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi else m_deleteLexicon->setEnabled(false); - updateLexiconInformation(); + updateLexiconInformation(true); } LexiconDialog::~LexiconDialog() { - + delete m_fileNameValidator; + delete m_wordFactory; } void LexiconDialog::deleteLexicon() @@ -233,15 +250,30 @@ void LexiconDialog::accept() QDialog::accept(); } -void LexiconDialog::updateLexiconInformation() +void LexiconDialog::updateLexiconInformation(bool firstTime) { - int wordCount = m_wordFactory ? m_wordFactory->wordCount() : 0; - QByteArray hash = (m_wordFactory && wordCount) ? QByteArray(m_wordFactory->hashBytes(), 16).toHex() : ""; + QByteArray hash = m_wordFactory ? QByteArray(m_wordFactory->hashBytes(), 16).toHex() : ""; QString text; QString lengthText; + + // only recompute word count when the dictionary changes + if (m_wordFactory && hash != m_previousHash) + { + m_wordFactory->computeWordCount(); + m_previousHash = hash; + } + int wordCount = m_wordFactory ? m_wordFactory->wordCount() : 0; + if (wordCount == 0) + { + delete m_wordFactory; + m_wordFactory = NULL; + } if (m_wordFactory) lengthText = QString::fromStdString(m_wordFactory->letterCountString()); + if (firstTime) + m_originalHash = hash; + text.append(tr("File name: ")); text.append(tr("\n\nFile size: ")); text.append(tr("\n\nWord count: ")); @@ -252,4 +284,6 @@ void LexiconDialog::updateLexiconInformation() text.append(hash.left(8)); m_lexiconInformation->setText(text); + + m_saveChanges->setEnabled(hash != m_originalHash && !m_lexiconName->text().isEmpty()); } diff --git a/quacker/lexicondialog.h b/quacker/lexicondialog.h index 39cd546..fa80ec1 100644 --- a/quacker/lexicondialog.h +++ b/quacker/lexicondialog.h @@ -34,6 +34,7 @@ class QLabel; class QLineEdit; class QPushButton; class DawgFactory; +class FileNameValidator; class LexiconDialog : public QDialog { @@ -44,9 +45,10 @@ public: ~LexiconDialog(); virtual void accept(); - void updateLexiconInformation(); + void updateLexiconInformation(bool firstTime = false); protected slots: + void parametersChanged(const QString &) { updateLexiconInformation(); }; void deleteLexicon(); void addWordsFromFile(); void alphabetChanged(const QString &); @@ -62,6 +64,7 @@ private: QPushButton *m_addWordsFromFile; QPushButton *m_clearAllWords; QLabel *m_lexiconInformation; + FileNameValidator * m_fileNameValidator; QPushButton *m_saveChanges; QPushButton *m_cancel; @@ -69,6 +72,8 @@ private: QString m_originalName; QString m_alphabetFileName; + QByteArray m_originalHash; + QByteArray m_previousHash; DawgFactory *m_wordFactory; }; diff --git a/quackleio/dawgfactory.cpp b/quackleio/dawgfactory.cpp index 362dfdc..869ef8e 100644 --- a/quackleio/dawgfactory.cpp +++ b/quackleio/dawgfactory.cpp @@ -40,6 +40,7 @@ DawgFactory::DawgFactory(const QString &alphabetFile) m_root.lastchild = true; m_hash.int32ptr[0] = m_hash.int32ptr[1] = m_hash.int32ptr[2] = m_hash.int32ptr[3] = 0; + m_encodableWords = m_unencodableWords = m_duplicateWords = m_wordCount = 0; } DawgFactory::~DawgFactory() @@ -186,10 +187,10 @@ void DawgFactory::writeIndex(const UVString& filename) } } -int DawgFactory::wordCount() const +void DawgFactory::computeWordCount() const { m_countsByLength.resize(0); - return m_root.wordCount(0, m_countsByLength); + m_wordCount = m_root.wordCount(0, m_countsByLength); } string DawgFactory::letterCountString() const diff --git a/quackleio/dawgfactory.h b/quackleio/dawgfactory.h index 8dd6e03..5872dc3 100644 --- a/quackleio/dawgfactory.h +++ b/quackleio/dawgfactory.h @@ -30,7 +30,8 @@ public: DawgFactory(const QString &alphabetFile); ~DawgFactory(); - int wordCount() const; + void computeWordCount() const; + int wordCount() const { return m_wordCount; }; string letterCountString() const; int nodeCount() const { return m_nodelist.size(); }; int encodableWords() const { return m_encodableWords; }; @@ -78,6 +79,7 @@ private: int m_unencodableWords; int m_duplicateWords; vector< Node* > m_nodelist; + mutable int m_wordCount; mutable vector m_countsByLength; Quackle::AlphabetParameters *m_alphas; Node m_root; -- cgit v1.2.3 From ef4273ba47a2da9cea0aed59235e2d0a86bb8d7e Mon Sep 17 00:00:00 2001 From: John Fultz Date: Tue, 13 Oct 2015 12:00:13 -0500 Subject: Saving custom dictionaries now really works. * Fix a number of remaining bugs in the lexicon dialog. * Fix an error reading the v1 DAWG. * Improve the word counting mechanism. * Make sure the lexicn dialog properly selects and loads its dictionary after it's done. * Implement deleting of user dictionaries. * Clean up dictionary info text in lexicon dialog. * Disable gaddag generation...still have to fix that up to happen at sensible times and with user notification. --- lexiconparameters.cpp | 7 ++--- lexiconparameters.h | 2 +- quacker/lexicondialog.cpp | 72 +++++++++++++++++++++++++++++------------------ quacker/lexicondialog.h | 7 ++++- quacker/settings.cpp | 68 +++++++++++++++++++++++++++++--------------- quacker/settings.h | 2 +- quackleio/dawgfactory.cpp | 60 +++++++++++++-------------------------- quackleio/dawgfactory.h | 17 +++++------ 8 files changed, 128 insertions(+), 107 deletions(-) (limited to 'quacker/lexicondialog.cpp') diff --git a/lexiconparameters.cpp b/lexiconparameters.cpp index bc10773..74de78f 100644 --- a/lexiconparameters.cpp +++ b/lexiconparameters.cpp @@ -82,8 +82,6 @@ class Quackle::V1LexiconInterpreter : public LexiconInterpreter file >> lexparams.m_utf8Alphabet[i]; file.get(); // separator space } - file.get(); // whitespace separator - lexparams.m_wordcount = (bytes[0] << 16) | (bytes[1] << 8) | bytes[2]; while (!file.eof()) { file.read((char*)(lexparams.m_dawg) + i, 7); @@ -123,18 +121,18 @@ class Quackle::V1LexiconInterpreter : public LexiconInterpreter p = (dawg[index] << 16) + (dawg[index + 1] << 8) + (dawg[index + 2]); letter = dawg[index + 3]; - t = (p != 0); lastchild = ((letter & 64) != 0); british = !(letter & 128); letter = (letter & 63) + QUACKLE_FIRST_LETTER; playability = (dawg[index + 4] << 16) + (dawg[index + 5] << 8) + (dawg[index + 6]); + t = (playability != 0); } virtual int versionNumber() const { return 1; } }; LexiconParameters::LexiconParameters() - : m_dawg(NULL), m_gaddag(NULL), m_interpreter(NULL), m_wordcount(0) + : m_dawg(NULL), m_gaddag(NULL), m_interpreter(NULL), m_wordCount(0) { memset(m_hash, 0, sizeof(m_hash)); } @@ -155,6 +153,7 @@ void LexiconParameters::unloadDawg() delete[] m_dawg; m_dawg = NULL; delete m_interpreter; + m_interpreter = NULL; } void LexiconParameters::unloadGaddag() diff --git a/lexiconparameters.h b/lexiconparameters.h index 4eda4f3..f29a589 100644 --- a/lexiconparameters.h +++ b/lexiconparameters.h @@ -88,7 +88,7 @@ protected: string m_lexiconName; LexiconInterpreter *m_interpreter; char m_hash[16]; - int m_wordcount; + int m_wordCount; vector m_utf8Alphabet; LexiconInterpreter* createInterpreter(char version) const; diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp index f92efb1..6630e1f 100644 --- a/quacker/lexicondialog.cpp +++ b/quacker/lexicondialog.cpp @@ -40,7 +40,7 @@ public: }; LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDialog(parent), - m_wordFactory(NULL) + m_deleted(false), m_wordFactory(NULL) { m_originalName = originalName; @@ -57,6 +57,7 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi m_lexiconInformation = new QLabel(""); m_lexiconInformation->setWordWrap(true); + m_lexiconInformation->setTextInteractionFlags(Qt::TextBrowserInteraction); m_saveChanges = new QPushButton(tr("&Save Changes")); m_cancel = new QPushButton(tr("&Cancel")); @@ -107,6 +108,7 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi // hook up signals and slots connect(m_lexiconName, SIGNAL(textEdited(const QString &)), this, SLOT(parametersChanged(const QString &))); connect(m_addWordsFromFile, SIGNAL(clicked()), this, SLOT(addWordsFromFile())); + connect(m_clearAllWords, SIGNAL(clicked()), this, SLOT(loadOriginalDictionary())); connect(m_saveChanges, SIGNAL(clicked()), this, SLOT(accept())); connect(m_cancel, SIGNAL(clicked()), this, SLOT(reject())); connect(m_deleteLexicon, SIGNAL(clicked()), this, SLOT(deleteLexicon())); @@ -114,26 +116,13 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi setWindowTitle(tr("Configure Lexicon - Quackle")); - Settings::populateComboFromFilenames(m_alphabetCombo, "alphabets", ""); + Settings::populateComboFromFilenames(m_alphabetCombo, "alphabets", ".quackle_alphabet", ""); alphabetChanged(m_alphabetCombo->currentText()); - string dawgFileName = originalName.toStdString() + ".dawg"; - QString dawgFullFileName; - if (!originalName.isEmpty()) - dawgFullFileName = QString::fromStdString(Quackle::LexiconParameters::findDictionaryFile(dawgFileName)); - m_lexiconName->setValidator(m_fileNameValidator); m_lexiconName->setText(m_originalName); - if (!dawgFullFileName.isEmpty()) - { - m_deleteLexicon->setEnabled(Quackle::LexiconParameters::hasUserDictionaryFile(dawgFileName)); - addWordsFromDawgFile(dawgFullFileName); - } - else - m_deleteLexicon->setEnabled(false); - - updateLexiconInformation(true); + loadOriginalDictionary(); } LexiconDialog::~LexiconDialog() @@ -144,9 +133,11 @@ LexiconDialog::~LexiconDialog() void LexiconDialog::deleteLexicon() { - delete m_wordFactory; - m_wordFactory = NULL; - updateLexiconInformation(); + string lexiconNameStr = m_originalName.toStdString(); + string filename = QUACKLE_DATAMANAGER->makeDataFilename("lexica", lexiconNameStr + ".dawg", true); + QFile(QString::fromStdString(filename)).remove(); + m_deleted = true; + QDialog::accept(); } void LexiconDialog::addWordsFromFile() @@ -245,8 +236,40 @@ void LexiconDialog::addWordsFromTextFile(const QString &textFile) } } +void LexiconDialog::loadOriginalDictionary() +{ + delete m_wordFactory; + m_wordFactory = NULL; + string dawgFileName = m_originalName.toStdString() + ".dawg"; + QString dawgFullFileName; + if (!m_originalName.isEmpty()) + dawgFullFileName = QString::fromStdString(Quackle::LexiconParameters::findDictionaryFile(dawgFileName)); + + if (!dawgFullFileName.isEmpty()) + { + m_deleteLexicon->setEnabled(Quackle::LexiconParameters::hasUserDictionaryFile(dawgFileName)); + m_lexiconInformation->setText(tr("Loading dictionary...")); + show(); + qApp->processEvents(); + addWordsFromDawgFile(dawgFullFileName); + } + else + m_deleteLexicon->setEnabled(false); + + updateLexiconInformation(true); +} + void LexiconDialog::accept() { + string lexiconNameStr = m_lexiconName->text().toStdString(); + string filename = QUACKLE_DATAMANAGER->makeDataFilename("lexica", lexiconNameStr + ".dawg", true); + m_lexiconInformation->setText(tr("Compressing and writing dictionary file...\nThis may take a few minutes.")); + qApp->processEvents(); + m_wordFactory->generate(); + m_lexiconInformation->setText(tr("Writing dictionary file...")); + qApp->processEvents(); + m_wordFactory->writeIndex(filename); + m_finalLexiconName = m_lexiconName->text(); QDialog::accept(); } @@ -256,12 +279,6 @@ void LexiconDialog::updateLexiconInformation(bool firstTime) QString text; QString lengthText; - // only recompute word count when the dictionary changes - if (m_wordFactory && hash != m_previousHash) - { - m_wordFactory->computeWordCount(); - m_previousHash = hash; - } int wordCount = m_wordFactory ? m_wordFactory->wordCount() : 0; if (wordCount == 0) { @@ -274,9 +291,7 @@ void LexiconDialog::updateLexiconInformation(bool firstTime) if (firstTime) m_originalHash = hash; - text.append(tr("File name: ")); - text.append(tr("\n\nFile size: ")); - text.append(tr("\n\nWord count: ")); + text.append(tr("Word count: ")); text.append(QString("%L1").arg(wordCount)); text.append("\n"); text.append(lengthText); @@ -286,4 +301,5 @@ void LexiconDialog::updateLexiconInformation(bool firstTime) m_lexiconInformation->setText(text); m_saveChanges->setEnabled(hash != m_originalHash && !m_lexiconName->text().isEmpty()); + m_clearAllWords->setEnabled(hash != m_originalHash); } diff --git a/quacker/lexicondialog.h b/quacker/lexicondialog.h index fa80ec1..1f1605b 100644 --- a/quacker/lexicondialog.h +++ b/quacker/lexicondialog.h @@ -45,6 +45,9 @@ public: ~LexiconDialog(); virtual void accept(); + bool itemWasDeleted() { return m_deleted; }; + const QString &lexiconName() { return m_finalLexiconName; }; + void updateLexiconInformation(bool firstTime = false); protected slots: @@ -52,6 +55,7 @@ protected slots: void deleteLexicon(); void addWordsFromFile(); void alphabetChanged(const QString &); + void loadOriginalDictionary(); protected: void addWordsFromDawgFile(const QString &dawgfile); @@ -73,7 +77,8 @@ private: QString m_originalName; QString m_alphabetFileName; QByteArray m_originalHash; - QByteArray m_previousHash; + QString m_finalLexiconName; + bool m_deleted; DawgFactory *m_wordFactory; }; diff --git a/quacker/settings.cpp b/quacker/settings.cpp index 3319955..6435605 100644 --- a/quacker/settings.cpp +++ b/quacker/settings.cpp @@ -107,7 +107,7 @@ void Settings::createGUI() m_lexiconNameCombo = new QComboBox; connect(m_lexiconNameCombo, SIGNAL(activated(const QString &)), this, SLOT(lexiconChanged(const QString &))); - populateComboFromFilenames(m_lexiconNameCombo, "lexica", "lexicon"); + populateComboFromFilenames(m_lexiconNameCombo, "lexica", ".dawg", "lexicon"); QLabel *lexiconNameLabel = new QLabel(tr("&Lexicon:")); lexiconNameLabel->setBuddy(m_lexiconNameCombo); @@ -118,7 +118,7 @@ void Settings::createGUI() m_alphabetNameCombo = new QComboBox; connect(m_alphabetNameCombo, SIGNAL(activated(const QString &)), this, SLOT(alphabetChanged(const QString &))); - populateComboFromFilenames(m_alphabetNameCombo, "alphabets", ""); + populateComboFromFilenames(m_alphabetNameCombo, "alphabets", ".quackle_alphabet", ""); QLabel *alphabetNameLabel = new QLabel(tr("&Alphabet:")); alphabetNameLabel->setBuddy(m_alphabetNameCombo); @@ -129,7 +129,7 @@ void Settings::createGUI() m_themeNameCombo = new QComboBox; connect(m_themeNameCombo, SIGNAL(activated(const QString &)), this, SLOT(themeChanged(const QString &))); - populateComboFromFilenames(m_themeNameCombo, "themes", ""); + populateComboFromFilenames(m_themeNameCombo, "themes", ".ini", ""); QLabel *themeNameLabel = new QLabel(tr("&Theme:")); themeNameLabel->setBuddy(m_themeNameCombo); @@ -140,7 +140,7 @@ void Settings::createGUI() m_boardNameCombo = new QComboBox; connect(m_boardNameCombo, SIGNAL(activated(const QString &)), this, SLOT(boardChanged(const QString &))); - populateComboFromFilenames(m_boardNameCombo, "boards", "board"); + populateComboFromFilenames(m_boardNameCombo, "boards", "", "board"); QLabel *boardNameLabel = new QLabel(tr("&Board:")); boardNameLabel->setBuddy(m_boardNameCombo); @@ -172,6 +172,8 @@ void Settings::createGUI() void Settings::load() { m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_LEXICON_PARAMETERS->lexiconName()))); + if (m_lexiconNameCombo->currentIndex() == -1) + m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_LEXICON_PARAMETERS->lexiconName()) + "*")); m_lastGoodLexiconValue = m_lexiconNameCombo->currentIndex(); m_alphabetNameCombo->setCurrentIndex(m_alphabetNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_ALPHABET_PARAMETERS->alphabetName()))); m_themeNameCombo->setCurrentIndex(m_themeNameCombo->findText(m_themeName)); @@ -269,12 +271,12 @@ void Settings::setQuackleToUseLexiconName(const QString &lexiconName) else QUACKLE_LEXICON_PARAMETERS->loadGaddag(gaddagFile); - if (!QUACKLE_LEXICON_PARAMETERS->hasGaddag()) - { - gaddagFile = QUACKLE_DATAMANAGER->makeDataFilename("lexica", lexiconNameStr + ".gaddag", true); - buildGaddag(gaddagFile); - QUACKLE_LEXICON_PARAMETERS->loadGaddag(gaddagFile); - } + // if (!QUACKLE_LEXICON_PARAMETERS->hasGaddag()) + // { + // gaddagFile = QUACKLE_DATAMANAGER->makeDataFilename("lexica", lexiconNameStr + ".gaddag", true); + // buildGaddag(gaddagFile); + // QUACKLE_LEXICON_PARAMETERS->loadGaddag(gaddagFile); + // } QUACKLE_STRATEGY_PARAMETERS->initialize(lexiconNameStr); } @@ -342,6 +344,9 @@ void Settings::setQuackleToUseBoardName(const QString &boardName) void Settings::lexiconChanged(const QString &lexiconName) { + QString lexicon = lexiconName; + if (lexicon.endsWith("*")) + lexicon.truncate(lexicon.size() - 1); if (m_lexiconNameCombo->currentIndex() == m_lexiconNameCombo->count() - 1) { editLexicon(); @@ -350,11 +355,11 @@ void Settings::lexiconChanged(const QString &lexiconName) m_lexiconNameCombo->setCurrentIndex(m_lastGoodLexiconValue); return; } - setQuackleToUseLexiconName(lexiconName); + setQuackleToUseLexiconName(lexicon); m_lastGoodLexiconValue = m_lexiconNameCombo->currentIndex(); CustomQSettings settings; - settings.setValue("quackle/settings/lexicon-name", lexiconName); + settings.setValue("quackle/settings/lexicon-name", lexicon); emit refreshViews(); } @@ -465,9 +470,6 @@ void Settings::editBoard() void Settings::loadBoardNameCombo() { - if (m_lexiconNameCombo == 0) - return; - while (m_boardNameCombo->count() > 0) m_boardNameCombo->removeItem(0); @@ -489,12 +491,30 @@ void Settings::loadBoardNameCombo() void Settings::editLexicon() { QString name = m_lexiconNameCombo->currentText(); + if (name.endsWith("*")) + name.truncate(name.size() - 1); if (m_lexiconNameCombo->currentIndex() == m_lexiconNameCombo->count() - 1) name = ""; LexiconDialog dialog(this, name); if (dialog.exec()) { - populateComboFromFilenames(m_lexiconNameCombo, "lexica", "lexicon"); + populateComboFromFilenames(m_lexiconNameCombo, "lexica", ".dawg", "lexicon"); + qApp->processEvents(); + if (dialog.itemWasDeleted()) + { + m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(name)); + QUACKLE_LEXICON_PARAMETERS->setLexiconName(""); // force lexicon to reload + QUACKLE_LEXICON_PARAMETERS->unloadAll(); + if (m_lexiconNameCombo->currentIndex() != -1) + setQuackleToUseLexiconName(name); + } + else if (!dialog.lexiconName().isEmpty()) + { + QUACKLE_LEXICON_PARAMETERS->setLexiconName(""); // force lexicon to reload + QUACKLE_LEXICON_PARAMETERS->unloadAll(); + setQuackleToUseLexiconName(dialog.lexiconName()); + m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(name + "*")); + } load(); } } @@ -508,7 +528,7 @@ void Settings::editAlphabet() AlphabetDialog dialog(this); if (dialog.exec()) { - populateComboFromFilenames(m_alphabetNameCombo, "alphabets", "alphabet"); + populateComboFromFilenames(m_alphabetNameCombo, "alphabets", ".quackle_alphabet", "alphabet"); load(); } #endif // 0 @@ -529,8 +549,11 @@ void Settings::editTheme() #endif // 0 } -void Settings::populateComboFromFilenames(QComboBox* combo, const QString &path, const QString &label) +void Settings::populateComboFromFilenames(QComboBox* combo, const QString &path, const QString &extension, const QString &label) { + while (combo->count() > 0) + combo->removeItem(0); + QStringList fileList; QDir dir(self()->m_appDataDir); if (dir.cd(path)) @@ -547,6 +570,8 @@ void Settings::populateComboFromFilenames(QComboBox* combo, const QString &path, for (i = fileList.begin(); i != fileList.end(); ++i) { fileName = *i; + if (!fileName.endsWith(extension)) + continue; periodPos = fileName.indexOf('.'); if (periodPos) { @@ -555,14 +580,13 @@ void Settings::populateComboFromFilenames(QComboBox* combo, const QString &path, } } - for (i = fileList.begin(); i != fileList.end(); ++i) + for (i = list.begin(); i != list.end(); ++i) { - QStringList::iterator j = i; - for (++j; j != fileList.end(); ++j) + for (QStringList::iterator j = i + 1; j != list.end(); ++j) { if (*i == *j) { - *i = "* " + *i; + *i = *i + "*"; list.erase(j); break; } diff --git a/quacker/settings.h b/quacker/settings.h index 7c5738e..babea3c 100644 --- a/quacker/settings.h +++ b/quacker/settings.h @@ -42,7 +42,7 @@ public: static Settings *self(); // load up an item list based on a list of filenames - static void populateComboFromFilenames(QComboBox* combo, const QString &path, const QString &label); + static void populateComboFromFilenames(QComboBox* combo, const QString &path, const QString &extension, const QString &label); signals: void refreshViews(); diff --git a/quackleio/dawgfactory.cpp b/quackleio/dawgfactory.cpp index 869ef8e..4dd4ec3 100644 --- a/quackleio/dawgfactory.cpp +++ b/quackleio/dawgfactory.cpp @@ -28,6 +28,8 @@ DawgFactory::DawgFactory(const QString &alphabetFile) + : m_encodableWords(0), m_unencodableWords(0), m_duplicateWords(0), + m_countsByLength(Quackle::FixedLengthString::maxSize, 0) { QuackleIO::FlexibleAlphabetParameters *flexure = new QuackleIO::FlexibleAlphabetParameters; flexure->load(alphabetFile); @@ -40,7 +42,6 @@ DawgFactory::DawgFactory(const QString &alphabetFile) m_root.lastchild = true; m_hash.int32ptr[0] = m_hash.int32ptr[1] = m_hash.int32ptr[2] = m_hash.int32ptr[3] = 0; - m_encodableWords = m_unencodableWords = m_duplicateWords = m_wordCount = 0; } DawgFactory::~DawgFactory() @@ -48,7 +49,7 @@ DawgFactory::~DawgFactory() delete m_alphas; } -bool DawgFactory::pushWord(const UVString& word, bool inSmaller, int playability) +bool DawgFactory::pushWord(const UVString &word, bool inSmaller, int playability) { UVString leftover; Quackle::LetterString encodedWord = m_alphas->encode(word, &leftover); @@ -59,11 +60,12 @@ bool DawgFactory::pushWord(const UVString& word, bool inSmaller, int playability return false; } -bool DawgFactory::pushWord(const Quackle::LetterString& word, bool inSmaller, int playability) +bool DawgFactory::pushWord(const Quackle::LetterString &word, bool inSmaller, int playability) { if (m_root.pushWord(word, inSmaller, playability)) { ++m_encodableWords; + ++m_countsByLength[word.length()]; hashWord(word); return true; } @@ -133,7 +135,7 @@ void DawgFactory::generate() m_root.print(m_nodelist); } -void DawgFactory::writeIndex(const UVString& filename) +void DawgFactory::writeIndex(const string &filename) { ofstream out(filename.c_str(), ios::out | ios::binary); unsigned char bytes[7]; @@ -187,37 +189,26 @@ void DawgFactory::writeIndex(const UVString& filename) } } -void DawgFactory::computeWordCount() const -{ - m_countsByLength.resize(0); - m_wordCount = m_root.wordCount(0, m_countsByLength); -} - string DawgFactory::letterCountString() const { ostringstream str; - if (m_countsByLength.size() < 16) - m_countsByLength.resize(16, 0); - str << "2s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[2]; - str << "\t6s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[6]; - str << "\t10s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[10]; - str << "\t14s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[14]; - str << "\n3s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[3]; - str << "\t7s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[7]; - str << "\t11s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[11]; - str << "\t15s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[15]; - str << "\n4s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[4]; - str << "\t8s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[8]; - str << "\t12s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[12]; - str << "\n5s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[5]; - str << "\t9s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[9]; - str << "\t13s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[13]; - str << "\n"; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + const int letterCount = j * 4 + i + 2; + if (j != 0) + str << "\t"; + if (m_countsByLength[letterCount] > 0) + str << letterCount << "s: " << std::setw(7) << std::right << std::setfill(' ') << m_countsByLength[letterCount]; + } + str << "\n"; + } return str.str(); } -void DawgFactory::Node::print(vector< Node* >& nodelist) +void DawgFactory::Node::print(vector< Node* > &nodelist) { written = true; @@ -255,7 +246,7 @@ void DawgFactory::Node::print(vector< Node* >& nodelist) // returns true if the word was actually added...false if it's a duplicate. -bool DawgFactory::Node::pushWord(const Quackle::LetterString& word, bool inSmaller, int pb) +bool DawgFactory::Node::pushWord(const Quackle::LetterString &word, bool inSmaller, int pb) { bool added; if (word.length() == 0) { @@ -318,17 +309,6 @@ bool DawgFactory::Node::equals(const Node &n) const return true; } -int DawgFactory::Node::wordCount(unsigned int depth, vector &countsByLength) const -{ - int wordCount = ((playability == 0) ? 0 : 1); - if (countsByLength.size() < depth + 1) - countsByLength.resize(depth + 1, 0); - countsByLength[depth] += wordCount; - for (size_t i = 0; i < children.size(); i++) - wordCount += children[i].wordCount(depth + 1, countsByLength); - return wordCount; -} - int DawgFactory::Node::letterSum() const { if (sumexplored) diff --git a/quackleio/dawgfactory.h b/quackleio/dawgfactory.h index 5872dc3..efcc455 100644 --- a/quackleio/dawgfactory.h +++ b/quackleio/dawgfactory.h @@ -30,19 +30,18 @@ public: DawgFactory(const QString &alphabetFile); ~DawgFactory(); - void computeWordCount() const; - int wordCount() const { return m_wordCount; }; + int wordCount() const { return m_encodableWords; }; string letterCountString() const; int nodeCount() const { return m_nodelist.size(); }; int encodableWords() const { return m_encodableWords; }; int unencodableWords() const { return m_unencodableWords; }; int duplicateWords() const { return m_duplicateWords; }; - bool pushWord(const UVString& word, bool inSmaller, int playability); - bool pushWord(const Quackle::LetterString& word, bool inSmaller, int playability); + bool pushWord(const UVString &word, bool inSmaller, int playability); + bool pushWord(const Quackle::LetterString &word, bool inSmaller, int playability); void hashWord(const Quackle::LetterString &word); void generate(); - void writeIndex(const UVString& filename); + void writeIndex(const string &filename); const char* hashBytes() { return m_hash.charptr; }; @@ -50,10 +49,9 @@ private: class Node { public: bool pushWord(const Quackle::LetterString& word, bool inSmaller, int pb); - void print(vector< Node* >& m_nodelist); + void print(vector< Node* > &m_nodelist); int letterSum() const; - int wordCount(unsigned int depth, vector &countsByLength) const; bool equals(const Node &n) const; Quackle::Letter c; @@ -67,7 +65,7 @@ private: bool lastchild; mutable bool sumexplored; - mutable int sum; + mutable unsigned int sum; mutable vector counts; bool deleted; @@ -79,8 +77,7 @@ private: int m_unencodableWords; int m_duplicateWords; vector< Node* > m_nodelist; - mutable int m_wordCount; - mutable vector m_countsByLength; + vector m_countsByLength; Quackle::AlphabetParameters *m_alphas; Node m_root; union { -- cgit v1.2.3 From fd6daafffbcce3bfc385d7508a1ca50174840912 Mon Sep 17 00:00:00 2001 From: John Fultz Date: Wed, 14 Oct 2015 01:29:30 -0500 Subject: Convert dictionaries, add csw15. * All dictionaries are now in v1 DAWG format. * Three corrupt dictionaries were removed. * Implement a way to tag dictionaries for copyright purposes. * CSW15 is now a thing. --- data/lexica/copyrights.txt | 2 ++ data/lexica/csw12.dawg | Bin 1600543 -> 1600616 bytes data/lexica/csw15.dawg | Bin 0 -> 1459769 bytes data/lexica/cswapr07.dawg | Bin 2641009 -> 2641082 bytes data/lexica/greek.dawg | Bin 1248807 -> 1248900 bytes data/lexica/korean.dawg | Bin 1014944 -> 1015145 bytes data/lexica/norwegian.dawg | Bin 1701427 -> 1701518 bytes data/lexica/ods5.dawg | Bin 899171 -> 899244 bytes data/lexica/osps.dawg | Bin 2570477 -> 2570571 bytes data/lexica/russian.dawg | Bin 240528 -> 0 bytes data/lexica/sowpods.dawg | Bin 1473486 -> 1473559 bytes data/lexica/tuvan.dawg | Bin 15492 -> 0 bytes data/lexica/twl06.dawg | Bin 1816584 -> 1816657 bytes data/lexica/twl06_wild.dawg | Bin 6215056 -> 0 bytes data/lexica/twl98.dawg | Bin 938070 -> 938143 bytes lexiconparameters.cpp | 27 +++++++++++++++++++++------ lexiconparameters.h | 4 ++-- quacker/lexicondialog.cpp | 3 ++- quacker/quacker.cpp | 32 +++++++++++++++++++++++--------- quacker/settings.cpp | 12 +++++++++--- quacker/settings.h | 2 ++ quackle.sublime-project | 4 ++-- 22 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 data/lexica/copyrights.txt create mode 100644 data/lexica/csw15.dawg delete mode 100644 data/lexica/russian.dawg delete mode 100644 data/lexica/tuvan.dawg delete mode 100644 data/lexica/twl06_wild.dawg (limited to 'quacker/lexicondialog.cpp') diff --git a/data/lexica/copyrights.txt b/data/lexica/copyrights.txt new file mode 100644 index 0000000..1e3e553 --- /dev/null +++ b/data/lexica/copyrights.txt @@ -0,0 +1,2 @@ +eea8dfe5:Collins Scrabble™ Words 2012, ©HarperCollins Publishers Ltd 2015 +48dea2c8:Collins Scrabble™ Words 2015, ©HarperCollins Publishers Ltd 2015 diff --git a/data/lexica/csw12.dawg b/data/lexica/csw12.dawg index f98ef8b..822bd95 100644 Binary files a/data/lexica/csw12.dawg and b/data/lexica/csw12.dawg differ diff --git a/data/lexica/csw15.dawg b/data/lexica/csw15.dawg new file mode 100644 index 0000000..da4defa Binary files /dev/null and b/data/lexica/csw15.dawg differ diff --git a/data/lexica/cswapr07.dawg b/data/lexica/cswapr07.dawg index a2863ac..6e6dca7 100644 Binary files a/data/lexica/cswapr07.dawg and b/data/lexica/cswapr07.dawg differ diff --git a/data/lexica/greek.dawg b/data/lexica/greek.dawg index 90835e5..b8666ec 100644 Binary files a/data/lexica/greek.dawg and b/data/lexica/greek.dawg differ diff --git a/data/lexica/korean.dawg b/data/lexica/korean.dawg index 6aa5d3e..c55769e 100644 Binary files a/data/lexica/korean.dawg and b/data/lexica/korean.dawg differ diff --git a/data/lexica/norwegian.dawg b/data/lexica/norwegian.dawg index 0acdb33..2db2269 100644 Binary files a/data/lexica/norwegian.dawg and b/data/lexica/norwegian.dawg differ diff --git a/data/lexica/ods5.dawg b/data/lexica/ods5.dawg index eabc68a..8d440d9 100644 Binary files a/data/lexica/ods5.dawg and b/data/lexica/ods5.dawg differ diff --git a/data/lexica/osps.dawg b/data/lexica/osps.dawg index 6c02ecd..6d81d3d 100644 Binary files a/data/lexica/osps.dawg and b/data/lexica/osps.dawg differ diff --git a/data/lexica/russian.dawg b/data/lexica/russian.dawg deleted file mode 100644 index 3b01c8e..0000000 Binary files a/data/lexica/russian.dawg and /dev/null differ diff --git a/data/lexica/sowpods.dawg b/data/lexica/sowpods.dawg index b5d89df..e052222 100644 Binary files a/data/lexica/sowpods.dawg and b/data/lexica/sowpods.dawg differ diff --git a/data/lexica/tuvan.dawg b/data/lexica/tuvan.dawg deleted file mode 100644 index f5f31b3..0000000 Binary files a/data/lexica/tuvan.dawg and /dev/null differ diff --git a/data/lexica/twl06.dawg b/data/lexica/twl06.dawg index 9388487..e0afed7 100644 Binary files a/data/lexica/twl06.dawg and b/data/lexica/twl06.dawg differ diff --git a/data/lexica/twl06_wild.dawg b/data/lexica/twl06_wild.dawg deleted file mode 100644 index 34e78a0..0000000 Binary files a/data/lexica/twl06_wild.dawg and /dev/null differ diff --git a/data/lexica/twl98.dawg b/data/lexica/twl98.dawg index a835b7c..e6f451e 100644 Binary files a/data/lexica/twl98.dawg and b/data/lexica/twl98.dawg differ diff --git a/lexiconparameters.cpp b/lexiconparameters.cpp index 74de78f..f04a941 100644 --- a/lexiconparameters.cpp +++ b/lexiconparameters.cpp @@ -132,7 +132,7 @@ class Quackle::V1LexiconInterpreter : public LexiconInterpreter }; LexiconParameters::LexiconParameters() - : m_dawg(NULL), m_gaddag(NULL), m_interpreter(NULL), m_wordCount(0) + : m_dawg(NULL), m_gaddag(NULL), m_interpreter(NULL) { memset(m_hash, 0, sizeof(m_hash)); } @@ -228,22 +228,37 @@ bool LexiconParameters::hasUserDictionaryFile(const string &lexicon) return QUACKLE_DATAMANAGER->hasUserDataFile("lexica", lexicon); } -UVString LexiconParameters::hashString(bool shortened) const +string LexiconParameters::hashString(bool shortened) const { - const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; string hashStr; for (size_t i = 0; i < sizeof(m_hash); i++) { hashStr.push_back(hex[(m_hash[i] & 0xF0) >> 4]); hashStr.push_back(hex[m_hash[i] & 0x0F]); - if (shortened && i == 5) + if (shortened && i == 3) break; - if (i % 2 == 1) - hashStr.push_back('-'); } return hashStr; } +string LexiconParameters::copyrightString() const +{ + string copyrightsFilename = QUACKLE_DATAMANAGER->makeDataFilename("lexica", "copyrights.txt", false); + fstream copyrightsFile(copyrightsFilename, ios_base::in); + while (copyrightsFile.good() && !copyrightsFile.eof()) + { + string line; + getline(copyrightsFile, line); + if (line.size() < 9 || line.find_first_of(':') != 8) + continue; + if (hashString(true).compare(line.substr(0,8)) != 0) + continue; + return line.substr(9, line.size()); + } + return string(); +} + LexiconInterpreter* LexiconParameters::createInterpreter(char version) const { switch(version) diff --git a/lexiconparameters.h b/lexiconparameters.h index f29a589..d19f695 100644 --- a/lexiconparameters.h +++ b/lexiconparameters.h @@ -80,7 +80,8 @@ public: } const GaddagNode *gaddagRoot() const { return (GaddagNode *) &m_gaddag[0]; }; - UVString hashString(bool shortened) const; + string hashString(bool shortened) const; + string copyrightString() const; protected: unsigned char *m_dawg; @@ -88,7 +89,6 @@ protected: string m_lexiconName; LexiconInterpreter *m_interpreter; char m_hash[16]; - int m_wordCount; vector m_utf8Alphabet; LexiconInterpreter* createInterpreter(char version) const; diff --git a/quacker/lexicondialog.cpp b/quacker/lexicondialog.cpp index 6630e1f..91eb676 100644 --- a/quacker/lexicondialog.cpp +++ b/quacker/lexicondialog.cpp @@ -117,6 +117,7 @@ LexiconDialog::LexiconDialog(QWidget *parent, const QString &originalName) : QDi setWindowTitle(tr("Configure Lexicon - Quackle")); Settings::populateComboFromFilenames(m_alphabetCombo, "alphabets", ".quackle_alphabet", ""); + m_alphabetCombo->setCurrentIndex(m_alphabetCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_ALPHABET_PARAMETERS->alphabetName()))); alphabetChanged(m_alphabetCombo->currentText()); m_lexiconName->setValidator(m_fileNameValidator); @@ -300,6 +301,6 @@ void LexiconDialog::updateLexiconInformation(bool firstTime) m_lexiconInformation->setText(text); - m_saveChanges->setEnabled(hash != m_originalHash && !m_lexiconName->text().isEmpty()); + m_saveChanges->setEnabled(true/*hash != m_originalHash && !m_lexiconName->text().isEmpty()*/); m_clearAllWords->setEnabled(hash != m_originalHash); } diff --git a/quacker/quacker.cpp b/quacker/quacker.cpp index a1f7064..2f407fa 100644 --- a/quacker/quacker.cpp +++ b/quacker/quacker.cpp @@ -155,13 +155,10 @@ void TopLevel::finishInitialization() void TopLevel::introduceToUser() { CustomQSettings settings; - QString lexiconName = settings.value("quackle/settings/lexicon-name", QString("twl06")).toString(); - if (lexiconName == "csw12") { - statusMessage(tr("The WESPA wordlist (CSW12) is copyright Harper Collins 2011.")); - } else { - statusMessage(tr("Enjoy your quackling. Choose \"New game...\" from the Game menu to begin.")); - } - + QString statusText = QString::fromUtf8(QUACKLE_LEXICON_PARAMETERS->copyrightString().c_str()); + if (statusText.isEmpty()) + statusText = tr("Enjoy your quackling. Choose \"New game...\" from the Game menu to begin."); + statusMessage(statusText); parseCommandLineOptions(); if (!CustomQSettings().contains("quackle/hasBeenRun")) @@ -2120,7 +2117,7 @@ void TopLevel::firstTimeRun() void TopLevel::about() { - QMessageBox::about(this, tr("About Quackle 1.0"), dialogText(tr( + QString aboutText = tr( "

Quackle 1.0 is a crossword game playing, analysis, and study tool. Visit the Quackle homepage at http://quackle.org for more information.

" "

Quackle was written by Jason Katz-Brown, John O'Laughlin, John Fultz, Matt Liberty, and Anand Buddhdev. We thank the anonymous donor who made this software free.

" "

Copyright 2005-2015 by

" @@ -2130,7 +2127,24 @@ void TopLevel::about() "" "

Quackle is free, open-source software licensed under the terms of the GNU General Public License Version 3. See

" "

http://quackle.org/LICENSE

" -))); +"

Dictionary copyrights

    " +); + + FILE* file = fopen(QUACKLE_DATAMANAGER->makeDataFilename("lexica", "copyrights.txt", false).c_str(), "r"); + if (file) + { + QTextStream strm(file); + while (!strm.atEnd()) + { + QString line = strm.readLine(); + int pos = line.indexOf(':'); + if (pos != -1 && pos + 1 < line.size()) + aboutText += "
  • " + line.mid(pos + 1) + "
  • "; + } + fclose(file); + aboutText += "
"; + } + QMessageBox::about(this, tr("About Quackle 1.0"), dialogText(aboutText)); } void TopLevel::hints() diff --git a/quacker/settings.cpp b/quacker/settings.cpp index 6435605..e22a29e 100644 --- a/quacker/settings.cpp +++ b/quacker/settings.cpp @@ -148,6 +148,8 @@ void Settings::createGUI() m_editBoard->setMaximumWidth(60); connect(m_editBoard, SIGNAL(clicked()), this, SLOT(editBoard())); + m_copyrightLabel = new QLabel(); + layout->addWidget(lexiconNameLabel, 0, 0, Qt::AlignRight); layout->addWidget(m_lexiconNameCombo, 0, 1); layout->addWidget(m_editLexicon, 0, 2); @@ -160,25 +162,28 @@ void Settings::createGUI() layout->addWidget(boardNameLabel, 3, 0, Qt::AlignRight); layout->addWidget(m_boardNameCombo, 3, 1); layout->addWidget(m_editBoard, 3, 2); + layout->addWidget(m_copyrightLabel, 4, 0, 1, -1, Qt::AlignTop); layout->setColumnMinimumWidth(3, 0); layout->setColumnStretch(3, 1); layout->setRowMinimumHeight(4, 0); layout->setRowStretch(4, 1); + load(); } void Settings::load() { - m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_LEXICON_PARAMETERS->lexiconName()))); + m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(QString::fromUtf8(QUACKLE_LEXICON_PARAMETERS->lexiconName().c_str()))); if (m_lexiconNameCombo->currentIndex() == -1) - m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_LEXICON_PARAMETERS->lexiconName()) + "*")); + m_lexiconNameCombo->setCurrentIndex(m_lexiconNameCombo->findText(QString::fromUtf8(QUACKLE_LEXICON_PARAMETERS->lexiconName().c_str()) + "*")); m_lastGoodLexiconValue = m_lexiconNameCombo->currentIndex(); - m_alphabetNameCombo->setCurrentIndex(m_alphabetNameCombo->findText(QuackleIO::Util::stdStringToQString(QUACKLE_ALPHABET_PARAMETERS->alphabetName()))); + m_alphabetNameCombo->setCurrentIndex(m_alphabetNameCombo->findText(QString::fromUtf8(QUACKLE_ALPHABET_PARAMETERS->alphabetName().c_str()))); m_themeNameCombo->setCurrentIndex(m_themeNameCombo->findText(m_themeName)); m_boardNameCombo->setCurrentIndex(m_boardNameCombo->findText(QuackleIO::Util::uvStringToQString(QUACKLE_BOARD_PARAMETERS->name()))); m_lastGoodBoardValue = m_boardNameCombo->currentIndex(); + m_copyrightLabel->setText(QString::fromUtf8(QUACKLE_LEXICON_PARAMETERS->copyrightString().c_str())); } void Settings::preInitialize() @@ -279,6 +284,7 @@ void Settings::setQuackleToUseLexiconName(const QString &lexiconName) // } QUACKLE_STRATEGY_PARAMETERS->initialize(lexiconNameStr); + m_copyrightLabel->setText(QString::fromUtf8(QUACKLE_LEXICON_PARAMETERS->copyrightString().c_str())); } } diff --git a/quacker/settings.h b/quacker/settings.h index babea3c..ef3449e 100644 --- a/quacker/settings.h +++ b/quacker/settings.h @@ -29,6 +29,7 @@ class QComboBox; class QCheckBox; class QPushButton; +class QLabel; using namespace std; @@ -88,6 +89,7 @@ protected: QPushButton *m_editAlphabet; QPushButton *m_editTheme; QPushButton *m_editBoard; + QLabel *m_copyrightLabel; QString m_appDataDir; QString m_userDataDir; QString m_themeName; diff --git a/quackle.sublime-project b/quackle.sublime-project index 2ca8db5..c7f47cd 100644 --- a/quackle.sublime-project +++ b/quackle.sublime-project @@ -6,8 +6,8 @@ "file_exclude_patterns" : ["*.tgz", "*.sublime-workspace", ".tags*", "dawginput.raw", "playabilities.raw", "smaller.raw", ".gitattributes", "*.Debug", "*.Release", "*.pfx", "*.cer", - "makegaddag", "makeminidawg", "gaddagize", "Makefile"], - "folder_exclude_patterns" : ["obj", "moc", "build", "*.xcodeproj", "lib", "lexica", + "makegaddag", "makeminidawg", "gaddagize", "Makefile", "*.dawg", "*.gaddag"], + "folder_exclude_patterns" : ["obj", "moc", "build", "*.xcodeproj", "lib", "strategy", "debug", "release", "makeswelexicon", "lisp", "DerivedData"] } ] -- cgit v1.2.3