From 9306cb60c32082c5403931de0823a9fd5daa196c Mon Sep 17 00:00:00 2001 From: Jason Katz-Brown Date: Sun, 25 Aug 2013 02:17:13 -0700 Subject: Initial git commit. --- quackleio/flexiblealphabet.cpp | 131 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 quackleio/flexiblealphabet.cpp (limited to 'quackleio/flexiblealphabet.cpp') diff --git a/quackleio/flexiblealphabet.cpp b/quackleio/flexiblealphabet.cpp new file mode 100644 index 0000000..aca6e00 --- /dev/null +++ b/quackleio/flexiblealphabet.cpp @@ -0,0 +1,131 @@ +/* + * Quackle -- Crossword game artificial intelligence and analysis tool + * Copyright (C) 2005-2006 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include + +#include "flexiblealphabet.h" +#include "util.h" + +using namespace QuackleIO; + +FlexibleAlphabetParameters::FlexibleAlphabetParameters() +{ +} + +bool FlexibleAlphabetParameters::load(const QString &filename) +{ + QFile file(filename); + + if (!file.exists()) + { + UVcout << "alphabet parameters do not exist: " << QuackleIO::Util::qstringToString(filename) << endl; + return false; + } + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + UVcout << "Could not open " << QuackleIO::Util::qstringToString(filename) << endl; + return false; + } + + QTextStream stream(&file); + stream.setCodec(QTextCodec::codecForName("UTF-8")); + + QString line; + Quackle::Letter letter = QUACKLE_FIRST_LETTER; + while (!stream.atEnd()) + { + line = stream.readLine().simplified(); + QStringList strings = line.split(QRegExp("\\s+")); + + if (line.startsWith("#")) + continue; + + if (strings.isEmpty()) + continue; + + QString text = strings.front(); + const UVString textUV = QuackleIO::Util::qstringToString(text); + strings.pop_front(); + + const bool isBlank = text.startsWith("blank", Qt::CaseInsensitive); + + if (isBlank) + { + if (strings.size() < 2) + { + UVcerr << "Error reading in alphabet: Blank specification does not specify count and score."; + continue; + } + } + else + { + if (strings.size() < 4) + { + UVcerr << "Error reading in alphabet: Letter specification does specify blank text, count, score, and vowelness."; + continue; + } + } + + QString blankText; + if (!isBlank) + { + blankText = strings.front(); + strings.pop_front(); + } + + + bool ok = false; + int score = strings.takeFirst().toInt(&ok); + if (!ok) + UVcerr << "Score of letter " << textUV << " is unparsable."; + + int count = strings.takeFirst().toInt(&ok); + if (!ok) + UVcerr << "Count of letter " << textUV << " is unparsable."; + + bool isVowel = false; + if (!isBlank) + { + isVowel = strings.takeFirst().toInt(&ok); + if (!ok) + UVcerr << "Vowelness of letter " << textUV << " is unparsable. (Must be 0 or 1.)"; + } + + if (isBlank) + { + setCount(QUACKLE_BLANK_MARK, count); + setScore(QUACKLE_BLANK_MARK, score); + } + else + { + const UVString blankTextUV = QuackleIO::Util::qstringToString(blankText); + + setLetterParameter(letter, Quackle::LetterParameter(letter, textUV, blankTextUV, score, count, isVowel)); + ++letter; + + //UVcout << "New letter " << textUV << " [" << (int)letter << "]" << endl; + } + } + + file.close(); + return true; +} + -- cgit v1.2.3