summaryrefslogtreecommitdiff
path: root/quacker
diff options
context:
space:
mode:
authorJohn Fultz <jfultz@wolfram.com>2015-09-07 14:19:46 -0500
committerJohn Fultz <jfultz@wolfram.com>2015-09-07 15:45:41 -0500
commit5350a57f1be22b28914fca14225c73dac5b30b24 (patch)
tree399a309a1302d30ec83cc5d7281ac7286882523a /quacker
parent9ea9637922ca68d24d7517cf61870d8cee31f6c5 (diff)
Auto-generate gaddags
Need to add a user interface, but gaddags are now auto-generated if they can't be found. Some specific improvements here: * FixedLengthString gained a pop_back member. * Add code to allow v1 gaddags and v0 dawgs to work together. * Change memory allocation of dawgs and gaddags to be dynamic (the old limit didn't accommodate the ridiculously large Polish dictionary in the gaddag) * The Settings class now knows a bit about generating gaddags. This will be important for giving UI feedback. * Fixed several places using filenames which should be using string, not UVString. * Dawg/GaddagFactory should have been using UVString, not QString. My misunderstanding.
Diffstat (limited to 'quacker')
-rw-r--r--quacker/settings.cpp86
-rw-r--r--quacker/settings.h9
2 files changed, 74 insertions, 21 deletions
diff --git a/quacker/settings.cpp b/quacker/settings.cpp
index 362e916..1febdb5 100644
--- a/quacker/settings.cpp
+++ b/quacker/settings.cpp
@@ -93,6 +93,8 @@ Settings::Settings(QWidget *parent)
m_appDataDir = directory.absolutePath();
}
m_userDataDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
+ QDir qdir(m_userDataDir);
+ qdir.mkpath("lexica");
}
void Settings::createGUI()
@@ -195,49 +197,97 @@ void Settings::initialize()
if (lexiconName == "cswfeb07")
lexiconName = "cswapr07";
- setQuackleToUseLexiconName(QuackleIO::Util::qstringToStdString(lexiconName));
- setQuackleToUseAlphabetName(QuackleIO::Util::qstringToStdString(settings.value("quackle/settings/alphabet-name", QString("english")).toString()));
+ setQuackleToUseLexiconName(lexiconName);
+ setQuackleToUseAlphabetName(settings.value("quackle/settings/alphabet-name", QString("english")).toString());
setQuackleToUseThemeName(settings.value("quackle/settings/theme-name", QString("traditional")).toString());
setQuackleToUseBoardName(settings.value("quackle/settings/board-name", QString("")).toString());
}
-void Settings::setQuackleToUseLexiconName(const string &lexiconName)
+void Settings::buildGaddag(const string &filename)
{
- if (QUACKLE_LEXICON_PARAMETERS->lexiconName() != lexiconName)
+ GaddagFactory factory((UVString()));
+ Quackle::LetterString word;
+
+ pushIndex(factory, word, 1);
+ factory.generate();
+ factory.writeIndex(filename);
+}
+
+void Settings::pushIndex(GaddagFactory &factory, Quackle::LetterString &word, int index)
+{
+ unsigned int p;
+ Quackle::Letter letter;
+ bool t;
+ bool lastchild;
+ bool british;
+ int playability;
+
+ do
+ {
+ QUACKLE_LEXICON_PARAMETERS->dawgAt(index, p, letter, t, lastchild, british, playability);
+ word.push_back(letter);
+ if (t)
+ factory.pushWord(word);
+ if (p)
+ pushIndex(factory, word, p);
+ index++;
+ word.pop_back();
+ } while (!lastchild);
+}
+
+
+void Settings::setQuackleToUseLexiconName(const QString &lexiconName)
+{
+ string lexiconNameStr = lexiconName.toStdString();
+ if (QUACKLE_LEXICON_PARAMETERS->lexiconName() != lexiconNameStr)
{
- QUACKLE_LEXICON_PARAMETERS->setLexiconName(lexiconName);
+ QUACKLE_LEXICON_PARAMETERS->setLexiconName(lexiconNameStr);
- string dawgFile = Quackle::LexiconParameters::findDictionaryFile(lexiconName + ".dawg");
+ string dawgFile = Quackle::LexiconParameters::findDictionaryFile(lexiconNameStr + ".dawg");
if (dawgFile.empty())
{
- UVcout << "Dawg for lexicon '" << lexiconName << "' does not exist." << endl;
+ UVcout << "Dawg for lexicon '" << lexiconNameStr << "' does not exist." << endl;
QUACKLE_LEXICON_PARAMETERS->unloadDawg();
}
else
QUACKLE_LEXICON_PARAMETERS->loadDawg(dawgFile);
- string gaddagFile = Quackle::LexiconParameters::findDictionaryFile(lexiconName + ".gaddag");
+ if (!QUACKLE_LEXICON_PARAMETERS->hasDawg())
+ {
+ QUACKLE_LEXICON_PARAMETERS->unloadGaddag();
+ return;
+ }
+
+ string gaddagFile = Quackle::LexiconParameters::findDictionaryFile(lexiconNameStr + ".gaddag");
if (gaddagFile.empty())
{
- UVcout << "Gaddag for lexicon '" << lexiconName << "' does not exist." << endl;
+ UVcout << "Gaddag for lexicon '" << lexiconNameStr << "' does not exist." << endl;
QUACKLE_LEXICON_PARAMETERS->unloadGaddag();
}
else
QUACKLE_LEXICON_PARAMETERS->loadGaddag(gaddagFile);
- QUACKLE_STRATEGY_PARAMETERS->initialize(lexiconName);
+ 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);
}
}
-void Settings::setQuackleToUseAlphabetName(const string &alphabetName)
+void Settings::setQuackleToUseAlphabetName(const QString &alphabetName)
{
- if (QUACKLE_ALPHABET_PARAMETERS->alphabetName() != alphabetName)
+ string alphabetNameStr = alphabetName.toStdString();
+ if (QUACKLE_ALPHABET_PARAMETERS->alphabetName() != alphabetNameStr)
{
- QString alphabetFile = QuackleIO::Util::stdStringToQString(Quackle::AlphabetParameters::findAlphabetFile(alphabetName + ".quackle_alphabet"));
+ QString alphabetFileStr = QuackleIO::Util::stdStringToQString(Quackle::AlphabetParameters::findAlphabetFile(alphabetNameStr + ".quackle_alphabet"));
QuackleIO::FlexibleAlphabetParameters *flexure = new QuackleIO::FlexibleAlphabetParameters;
- flexure->setAlphabetName(alphabetName);
- if (flexure->load(alphabetFile))
+ flexure->setAlphabetName(alphabetNameStr);
+ if (flexure->load(alphabetFileStr))
{
if (flexure->length() != QUACKLE_ALPHABET_PARAMETERS->length() && QUACKLE_ALPHABET_PARAMETERS->alphabetName() != "default")
{
@@ -295,8 +345,7 @@ void Settings::lexiconChanged(const QString &lexiconName)
editLexicon();
return;
}
- string lexiconNameString = QuackleIO::Util::qstringToStdString(lexiconName);
- setQuackleToUseLexiconName(lexiconNameString);
+ setQuackleToUseLexiconName(lexiconName);
CustomQSettings settings;
settings.setValue("quackle/settings/lexicon-name", lexiconName);
@@ -311,8 +360,7 @@ void Settings::alphabetChanged(const QString &alphabetName)
editAlphabet();
return;
}
- string alphabetNameString = QuackleIO::Util::qstringToStdString(alphabetName);
- setQuackleToUseAlphabetName(alphabetNameString);
+ setQuackleToUseAlphabetName(alphabetName);
CustomQSettings settings;
settings.setValue("quackle/settings/alphabet-name", alphabetName);
diff --git a/quacker/settings.h b/quacker/settings.h
index cee0562..fab2f3f 100644
--- a/quacker/settings.h
+++ b/quacker/settings.h
@@ -24,6 +24,8 @@
#include <QWidget>
#include <QSettings>
+#include "quackleio/gaddagfactory.h"
+
class QComboBox;
class QCheckBox;
class QPushButton;
@@ -72,8 +74,8 @@ protected slots:
void editAlphabet();
void editTheme();
- void setQuackleToUseLexiconName(const string &lexiconName);
- void setQuackleToUseAlphabetName(const string &alphabetName);
+ void setQuackleToUseLexiconName(const QString &lexiconName);
+ void setQuackleToUseAlphabetName(const QString &alphabetName);
void setQuackleToUseThemeName(const QString &themeName);
void setQuackleToUseBoardName(const QString &lexiconName);
@@ -94,6 +96,9 @@ private:
// populate the popup based on what's in QSettings
void loadBoardNameCombo();
+ void buildGaddag(const string &filename);
+ void pushIndex(GaddagFactory &factory, Quackle::LetterString &word, int index);
+
static Settings *m_self;
};