summaryrefslogtreecommitdiff
path: root/quackleio/flexiblealphabet.cpp
blob: 6baf1139a0e231f21cd1ce653be9ddfed64fcea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 *  Quackle -- Crossword game artificial intelligence and analysis tool
 *  Copyright (C) 2005-2019 Jason Katz-Brown, John O'Laughlin, and John Fultz.
 *
 *  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 <http://www.gnu.org/licenses/>.
 */

#include <QtCore>

#include "flexiblealphabet.h"
#include "util.h"

using namespace QuackleIO;
using namespace std;

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);
	SET_QTEXTSTREAM_TO_UTF8(stream);

	QString line;
	Quackle::Letter letter = QUACKLE_FIRST_LETTER;
	while (!stream.atEnd())
	{
		line = stream.readLine().simplified();
		QStringList strings = line.split(QRegularExpression("\\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;
}