File: dice.cpp

package info (click to toggle)
tanglet 1.6.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 108,916 kB
  • sloc: cpp: 5,411; sh: 97; makefile: 16
file content (383 lines) | stat: -rw-r--r-- 10,645 bytes parent folder | download | duplicates (2)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
	SPDX-FileCopyrightText: 2012-2020 Graeme Gott <graeme@gottcode.org>

	SPDX-License-Identifier: GPL-3.0-or-later
*/

#include <QByteArray>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
#include <QString>
#include <QTextStream>

#include <zlib.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <stdexcept>
#include <unordered_map>
#include <vector>

//-----------------------------------------------------------------------------

class Exception : public std::runtime_error
{
public:
	explicit Exception(const QString& what_arg) : runtime_error(what_arg.toStdString()) { }
	explicit Exception(const std::string& what_arg) : runtime_error(what_arg) { }
	explicit Exception(const char* what_arg) : runtime_error(what_arg) { }
};

//-----------------------------------------------------------------------------

std::vector<QString> readWords(const QString& path)
{
	QByteArray data;

	// Open file
	QFile file(path);
	if (!file.open(QFile::ReadOnly)) {
		throw Exception("Unable to open file '" + path +"' for reading.");
	}
	gzFile gz = gzdopen(file.handle(), "rb");
	if (!gz) {
		throw Exception("Unable to open file '" + path + "' for decompressing.");
	}

	// Decompress file
	QByteArray buffer(0x40000, 0);
	int read = 0;
	do {
		data.append(buffer.constData(), read);
		read = gzread(gz, buffer.data(), buffer.size());
		if (read == -1) {
			throw Exception("Error while reading file '" + path + "'.");
		}
	} while (read > 0);
	gzclose(gz);

	// Find words
	std::vector<QString> words;
	QTextStream stream(&data);
	while (!stream.atEnd()) {
		words.push_back(stream.readLine().trimmed().split(' ').first().toUpper());
	}

	return words;
}

//-----------------------------------------------------------------------------

void saveDice(const QString& path, const std::vector<QString>& small, const std::vector<QString>& large)
{
	QFile out(path);
	if (!out.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
		throw Exception("Unable to open file '" + path + "' for writing.");
	}

	QTextStream stream(&out);

	for (const QString& line : small) {
		stream << line << '\n';
	}

	stream << '\n';

	for (const QString& line : large) {
		stream << line << '\n';
	}

	out.close();
}

//-----------------------------------------------------------------------------

std::unordered_map<QString, qreal> findLetterFrequency(const std::vector<QString>& words, bool use_bigrams, bool discard_infrequent)
{
	std::unordered_map<QString, int> letters;

	// Find counts of letters
	int total = 0;
	{
		std::unordered_map<QChar, int> chars;
		for (const auto& word : words) {
			for (const auto& c : word) {
				++total;
				++chars[c];
			}
		}
		for (auto i = chars.cbegin(), end = chars.cend(); i != end; ++i) {
			letters.emplace(i->first, i->second);
		}
	}

	// Discard letters that occur less than 0.001%
	if (discard_infrequent) {
		for (auto i = letters.begin(); i != letters.end(); ++i) {
			const qreal probability = (i->second * 100.0) / total;
			if (probability < 0.001) {
				total -= i->second;
				std::cout << QString("Discarded '%1', probability: %2%")
						.arg(i->first)
						.arg(probability, 0, 'f')
						.toStdString()
						<< std::endl;
				i = letters.erase(i);
				if (i == letters.end()) {
					break;
				}
			}
		}
	}

	if (use_bigrams) {
		// Find counts of bigrams
		std::unordered_map<QString, int> bigrams;
		QString bigram(2, '\0');
		for (const auto& word : words) {
			bigram[0] = word[0];
			for (int i = 1, end = word.length(); i < end; ++i) {
				const QChar c = word[i];
				bigram[1] = c;
				++bigrams[bigram];
				bigram[0] = c;
			}
		}

		// Find letters where bigrams are 99% of occurrences
		std::unordered_map<QString, QString> replace;
		std::unordered_map<QString, int> confidences;
		for (auto i = letters.cbegin(), end = letters.cend(); i != end; ++i) {
			const QString& letter = i->first;

			int letter_count = 0;
			std::vector<QString> letter_bigrams;
			for (auto b = bigrams.cbegin(), end_b = bigrams.cend(); b != end_b; ++b) {
				const QString& bigram = b->first;
				if (bigram.startsWith(letter)) {
					letter_bigrams.push_back(bigram);
					letter_count += b->second;
				}
			}

			for (const auto& bigram : letter_bigrams) {
				const qreal confidence = qreal(bigrams[bigram] * 100) / qreal(letter_count);
				if (confidence >= 99) {
					replace[letter] = bigram;
					confidences[letter] = std::round(confidence);
					break;
				}
			}
		}

		// Replace letter with bigram
		for (auto i = replace.cbegin(), end = replace.cend(); i != end; ++i) {
			const QString& letter = i->first;
			const QString& bigram = i->second;
			const int count = bigrams[bigram];

			letters[letter + bigram[1].toLower()] = count;
			letters[bigram[1]] -= count;

			letters.erase(letter);
			total -= count;

			std::cout << QString("Replaced '%1' with '%2', confidence: %3%")
					.arg(letter)
					.arg(letter + bigram[1].toLower())
					.arg(confidences[letter])
					.toStdString()
					<< std::endl;
		}
	}

	// Adjust letter frequencies to be in the range 0-1
	std::unordered_map<QString, qreal> result;
	const qreal inverse_total = 1.0 / qreal(total);
	for (auto i = letters.cbegin(), end = letters.cend(); i != end; ++i) {
		result.emplace(i->first, i->second * inverse_total);
	}
	return result;
}

//-----------------------------------------------------------------------------

std::unordered_map<QString, int> roundLetters(const std::unordered_map<QString, qreal>& letters, int count)
{
	const int sides = count * 6;

	// Scale letters by dice sides
	std::unordered_map<QString, qreal> scaled;
	std::vector<QString> frequent;
	for (auto i = letters.cbegin(), end = letters.cend(); i != end; ++i) {
		qreal value = i->second * sides;
		if (value < 1.0) {
			value = 1.0;
		}
		const QString& letter = i->first;
		scaled[letter] = value;

		// Track letter frequency
		auto f = frequent.begin();
		for (auto end_f = frequent.end(); f != end_f; ++f) {
			if (value > scaled[*f]) {
				break;
			}
		}
		frequent.insert(f, letter);
	}

	// Round letters by 2 digits after decimal
	std::map<qreal, QString> deltas;
	std::unordered_map<QString, int> result;
	int rounded = 0;
	for (const auto& letter : frequent) {
		const int value = std::lround(std::round(scaled[letter] * 10.0) / 10.0);
		if (value > 1) {
			deltas[std::abs(value + 0.5 - scaled[letter])] = letter;
		}
		result[letter] = value;
		rounded += value;
	}
	if (rounded < sides) {
		throw Exception("Rounded frequencies are less than dice sides.");
	}

	// Reduce closest rounded letters so that letter count matches sides
	rounded -= sides;
	auto letter = deltas.rbegin();
	for (int i = 0; i < rounded; ++i) {
		if (--result[letter->second] == 0) {
			throw Exception("'" + letter->second + "' has frequency of 0.");
		}
		++letter;
		if (letter == deltas.rend()) {
			throw Exception("Not enough closely rounded frequencies to match sides.");
		}
	}

	return result;
}

//-----------------------------------------------------------------------------

std::vector<QString> generateDice(const std::unordered_map<QString, qreal>& letters, int count, std::mt19937& random)
{
	std::vector<std::vector<QString>> dice(count);

	// Find groups of letters
	const auto scaled_letters = roundLetters(letters, count);
	std::vector<QString> single_letters;
	std::vector<QString> multi_letters;
	for (auto i = scaled_letters.cbegin(), end = scaled_letters.cend(); i != end; ++i) {
		const QString& letter = i->first;
		const int& value = i->second;
		if (value == 1) {
			single_letters.push_back(letter);
		} else {
			multi_letters.insert(multi_letters.end(), value, letter);
		}
	}

	// Add single-occurrence letters to dice first
	std::shuffle(single_letters.begin(), single_letters.end(), random);
	int dice_i = 0;
	for (const auto& letter : single_letters) {
		dice[dice_i].push_back(letter);
		++dice_i;
		if (dice_i >= count) {
			dice_i = 0;
		}
	}

	// Add multiple-occurrence letters to dice
	std::shuffle(multi_letters.begin(), multi_letters.end(), random);
	int pos = 0;
	for (auto& i : dice) {
		const int size = 6 - i.size();
		if (size > 0) {
			const auto m = multi_letters.cbegin() + pos;
			i.insert(i.end(), m, m + size);
			pos += size;
		}
	}

	// Alphabetize dice
	std::vector<QString> result;
	for (auto& i : dice) {
		std::sort(i.begin(), i.end());
		result.push_back(i[0] + ',' + i[1] + ',' + i[2] + ',' + i[3] + ',' + i[4] + ',' + i[5]);
	}
	std::sort(result.begin(), result.end());

	return result;
}

//-----------------------------------------------------------------------------

int main(int argc, char** argv)
{
	QCoreApplication app(argc, argv);

	try {
		QCommandLineParser parser;
		parser.setApplicationDescription("Create Tanglet dice from a Tanglet word list.");
		parser.addHelpOption();
		parser.addOption(QCommandLineOption({"b", "bigrams"}, "Automatically detect bigrams."));
		parser.addOption(QCommandLineOption({"d", "discard"}, "Discard infreqeunt letters."));
		parser.addOption(QCommandLineOption({"o", "output"}, "Place dice in <output> instead of default file.", "output"));
		parser.addOption(QCommandLineOption({"s", "seed"}, "Specify random <seed>.", "seed"));
		parser.addOption(QCommandLineOption({"v", "verbose"}, "Print status messages."));
		parser.addPositionalArgument("file", "The <file> to analyze for letter frequency.");
		parser.process(app);

		QString filename;
		const QStringList files = parser.positionalArguments();
		if (files.isEmpty()) {
			parser.showHelp();
		}
		if (files.size() > 1) {
			throw Exception("Multiple 'file' arguments specified.");
		}
		filename = files.first();

		const bool use_bigrams = parser.isSet("bigrams");

		const bool discard_infrequent = parser.isSet("discard");

		QString outfilename = "dice";
		if (parser.isSet("output")) {
			outfilename = parser.value("output");
		}

		uint64_t seed = 0x54414e47;
		if (parser.isSet("seed")) {
			seed = parser.value("seed").toULongLong();
		}
		std::mt19937 random(seed);

		if (!parser.isSet("verbose")) {
			std::cout.setstate(std::ios::failbit);
		}

		// Read lines from file
		const auto words = readWords(filename);

		// Create dice
		const auto letters = findLetterFrequency(words, use_bigrams, discard_infrequent);
		const auto small = generateDice(letters, 16, random);
		const auto large = generateDice(letters, 25, random);

		// Save dice to disk
		saveDice(outfilename, small, large);
	} catch (const std::exception& err) {
		std::cerr << err.what() << std::endl;
		return -1;
	}
}

//-----------------------------------------------------------------------------