forked from quackle/quackle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabetparameters.cpp
347 lines (285 loc) · 8.88 KB
/
alphabetparameters.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* 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 <algorithm>
#include <cassert>
#include "alphabetparameters.h"
#include "datamanager.h"
using namespace Quackle;
LetterString String::left(const LetterString &letterString, int number)
{
LetterString ret;
int i = 0;
LetterString::const_iterator myEnd(letterString.end());
for (LetterString::const_iterator it = letterString.begin(); i < number && it != myEnd; ++it, ++i)
ret += *it;
return ret;
}
LetterString String::alphabetize(const LetterString &letterString)
{
LetterString ret = letterString;
sort(ret.begin(), ret.end());
return ret;
}
LetterString String::clearBlankness(const LetterString &letterString)
{
LetterString ret;
LetterString::const_iterator myEnd(letterString.end());
for (LetterString::const_iterator it = letterString.begin(); it != myEnd; ++it)
ret += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(*it);
return ret;
}
LetterString String::setBlankness(const LetterString &letterString)
{
LetterString ret;
LetterString::const_iterator myEnd(letterString.end());
for (LetterString::const_iterator it = letterString.begin(); it != myEnd; ++it)
ret += QUACKLE_ALPHABET_PARAMETERS->setBlankness(*it);
return ret;
}
LetterString String::usedTiles(const LetterString &letterString)
{
LetterString used;
const LetterString::const_iterator end(letterString.end());
for (LetterString::const_iterator it = letterString.begin(); it != end; ++it)
{
if (*it == QUACKLE_BLANK_MARK || QUACKLE_ALPHABET_PARAMETERS->isBlankLetter(*it))
used += QUACKLE_BLANK_MARK;
else if (QUACKLE_ALPHABET_PARAMETERS->isPlainLetter(*it))
used += *it;
}
return used;
}
void String::counts(const LetterString &letterString, char *countsArray)
{
for (int j = 0; j < QUACKLE_FIRST_LETTER + QUACKLE_MAXIMUM_ALPHABET_SIZE; j++)
countsArray[j] = 0;
const LetterString::const_iterator end(letterString.end());
for (LetterString::const_iterator it = letterString.begin(); it != end; ++it)
countsArray[(int)*it]++;
}
void String::counts(const LongLetterString &letterString, char *countsArray)
{
for (int j = 0; j < QUACKLE_FIRST_LETTER + QUACKLE_MAXIMUM_ALPHABET_SIZE; j++)
countsArray[j] = 0;
const LongLetterString::const_iterator end(letterString.end());
for (LongLetterString::const_iterator it = letterString.begin(); it != end; ++it)
countsArray[(int)*it]++;
}
////////////
AlphabetParameters::AlphabetParameters()
: m_length(0)
{
setAlphabet(emptyAlphabet());
}
void AlphabetParameters::setAlphabet(const Alphabet &alphabet)
{
m_alphabet = alphabet;
updateLength();
const Alphabet::const_iterator alphabetEnd(m_alphabet.end());
Alphabet::const_iterator alphabetIt;
for (alphabetIt = m_alphabet.begin(); alphabetIt != alphabetEnd; ++alphabetIt) {
assert(m_letterLookup.find(alphabetIt->text()) == m_letterLookup.end());
m_letterLookup[alphabetIt->text()] = int(alphabetIt - m_alphabet.begin());
}
}
void AlphabetParameters::setLetterParameter(Letter letter, const LetterParameter &letterParameter)
{
assert(m_letterLookup.find(letterParameter.text()) == m_letterLookup.end());
if (letter >= (int) m_alphabet.size())
{
m_alphabet.resize(letter + 1);
updateLength();
}
m_alphabet[letter] = letterParameter;
m_letterLookup[letterParameter.text()] = letter;
}
void AlphabetParameters::updateLength()
{
m_length = int(m_alphabet.size() - QUACKLE_FIRST_LETTER);
}
Alphabet AlphabetParameters::emptyAlphabet()
{
Alphabet ret(QUACKLE_FIRST_LETTER);
ret[QUACKLE_NULL_MARK] = LetterParameter(QUACKLE_NULL_MARK, MARK_UV(" "), MARK_UV(""));
ret[QUACKLE_BLANK_MARK] = LetterParameter(QUACKLE_BLANK_MARK, MARK_UV("?"), MARK_UV(""));
ret[QUACKLE_PLAYED_THRU_MARK] = LetterParameter(QUACKLE_PLAYED_THRU_MARK, MARK_UV("."), MARK_UV(""));
ret[QUACKLE_PLAYTHRU_START_MARK] = LetterParameter(QUACKLE_PLAYTHRU_START_MARK, MARK_UV("("), MARK_UV(""));
ret[QUACKLE_PLAYTHRU_END_MARK] = LetterParameter(QUACKLE_PLAYTHRU_END_MARK, MARK_UV(")"), MARK_UV(""));
return ret;
}
void AlphabetParameters::setCount(Letter letter, int count)
{
m_alphabet[letter].setCount(count);
}
void AlphabetParameters::setScore(Letter letter, int score)
{
m_alphabet[letter].setScore(score);
}
LetterString AlphabetParameters::clearBlankness(const LetterString &letterString) const
{
LetterString ret;
const LetterString::const_iterator end(letterString.end());
for (LetterString::const_iterator it = letterString.begin(); it != end; ++it)
ret += clearBlankness(*it);
return ret;
}
UVString AlphabetParameters::userVisible(const LetterString &letterString) const
{
UVString ret;
const LetterString::const_iterator end(letterString.end());
for (LetterString::const_iterator it = letterString.begin(); it != end; ++it)
ret += userVisible(*it);
return ret;
}
UVString AlphabetParameters::userVisible(Letter letter) const
{
if (isBlankLetter(letter))
return letterParameter(letter - QUACKLE_BLANK_OFFSET).blankText();
else
return letterParameter(letter).text();
}
LetterString AlphabetParameters::encode(const UVString &word, UVString *leftover) const
{
LetterString ret;
UVString leftoverQuery;
const Alphabet::const_iterator alphabetEnd(m_alphabet.end());
LetterLookupMap::const_iterator lookupEnd = m_letterLookup.end();
const UVString::const_iterator end(word.end());
for (UVString::const_iterator it = word.begin(); it != end; ++it)
{
UVString query(leftoverQuery);
query += *it;
bool blank = false;
Alphabet::const_iterator alphabetIt;
LetterLookupMap::const_iterator lookupIt = m_letterLookup.find(query);
if (lookupIt == lookupEnd)
{
for (alphabetIt = m_alphabet.begin(); alphabetIt != alphabetEnd; ++alphabetIt)
if (alphabetIt->blankText() == query)
break;
if (alphabetIt == alphabetEnd)
{
leftoverQuery = query;
continue;
}
blank = true;
ret += blank? setBlankness(alphabetIt->letter()) : alphabetIt->letter();
} else {
ret += blank? setBlankness(m_alphabet[lookupIt->second].letter()) : m_alphabet[lookupIt->second].letter();
}
leftoverQuery.clear();
}
//UVcout << "encoded " << word << " to " << ret << endl;
if (leftover)
*leftover = leftoverQuery;
return ret;
}
string AlphabetParameters::findAlphabetFile(const string &alphabet)
{
return DataManager::self()->findDataFile("alphabets", alphabet + ".quackle_alphabet");
}
////////
EnglishAlphabetParameters::EnglishAlphabetParameters()
{
m_alphabetName = "default";
const int englishTileScores[26] =
{
1, // A
3, // B
3, // C
2, // D
1, // E
4, // F
2, // G
4, // H
1, // I
8, // J
5, // K
1, // L
3, // M
1, // N
1, // O
3, // P
10,// Q
1, // R
1, // S
1, // T
1, // U
4, // V
4, // W
8, // X
4, // Y
10,// Z
};
const int englishTileCounts[27] =
{
9, // A
2, // B
2, // C
4, // D
12, // E
2, // F
3, // G
2, // H
9, // I
1, // J
1, // K
4, // L
2, // M
6, // N
8, // O
2, // P
1, // Q
6, // R
4, // S
6, // T
4, // U
2, // V
2, // W
1, // X
2, // Y
1, // Z
2, // ?
};
Letter letter = QUACKLE_FIRST_LETTER;
for (UVChar charIndex = MARK_UV('A'); charIndex <= MARK_UV('Z'); ++charIndex, ++letter)
{
UVString letterString;
UVString lowerLetterString;
letterString += charIndex;
lowerLetterString += (UVChar)towlower(charIndex);
const bool isVowel = (charIndex == MARK_UV('A')) || (charIndex == MARK_UV('E')) || (charIndex == MARK_UV('I')) || (charIndex == MARK_UV('O')) || (charIndex == MARK_UV('U'));
const int score = englishTileScores[letter - QUACKLE_FIRST_LETTER];
const int count = englishTileCounts[letter - QUACKLE_FIRST_LETTER];
setLetterParameter(letter, LetterParameter(letter, letterString, lowerLetterString, score, count, isVowel));
}
setCount(QUACKLE_BLANK_MARK, englishTileCounts[26]);
}
UVOStream &operator<<(UVOStream& o, const Quackle::LetterParameter &letterParameter)
{
o << letterParameter.letter() << " [" << letterParameter.text() << ", " << letterParameter.blankText() << "]";
return o;
}
UVOStream &operator<<(UVOStream& o, const Quackle::Alphabet &alphabet)
{
o << "Alphabet (size " << alphabet.size() << "): ";
Alphabet::const_iterator end(alphabet.end());
for (Alphabet::const_iterator it = alphabet.begin(); it != end; ++it)
o << (*it) << " ";
return o;
}