1 /***********************************************************************
2 *
3 * Copyright (C) 2013, 2014 Graeme Gott <graeme@gottcode.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***********************************************************************/
19
20 #include "wordlist.h"
21
22 #include <QFile>
23 #include <QTextStream>
24
25 #include <algorithm>
26
27 //-----------------------------------------------------------------------------
28
WordList(QObject * parent)29 WordList::WordList(QObject* parent)
30 : QObject(parent), m_length(0) {
31 }
32
33 //-----------------------------------------------------------------------------
34
filter(const QString & known_letters) const35 QStringList WordList::filter(const QString& known_letters) const {
36 QRegExp filter(known_letters);
37 QStringList filtered;
38 for (auto i = m_words.constBegin(), end = m_words.constEnd(); i != end; ++i) {
39 if (filter.exactMatch(*i)) {
40 QString sorted = *i;
41 std::sort(sorted.begin(), sorted.end());
42 if (m_anagram_filters.contains(sorted)) {
43 continue;
44 }
45 filtered += *i;
46 }
47 }
48 return filtered;
49 }
50
51 //-----------------------------------------------------------------------------
52
addAnagramFilter(const QString & word)53 void WordList::addAnagramFilter(const QString& word) {
54 QString sorted_letters = word;
55 std::sort(sorted_letters.begin(), sorted_letters.end());
56 m_anagram_filters.append(sorted_letters);
57 }
58
59 //-----------------------------------------------------------------------------
60
resetAnagramFilters()61 void WordList::resetAnagramFilters() {
62 m_anagram_filters.clear();
63 }
64
65 //-----------------------------------------------------------------------------
66
setLanguage(const QString & langcode)67 void WordList::setLanguage(const QString& langcode) {
68 if (m_langcode == langcode) {
69 return;
70 }
71 m_langcode = langcode;
72
73 static QHash<QString, QSharedPointer<WordListData> > languages;
74 if (!languages.contains(langcode)) {
75 languages.insert(langcode, QSharedPointer<WordListData>(new WordListData(langcode)));
76 }
77 m_data = languages[langcode];
78 resetWords();
79
80 emit languageChanged(m_langcode);
81 }
82
83 //-----------------------------------------------------------------------------
84
setLength(int length)85 void WordList::setLength(int length) {
86 m_length = length;
87 resetWords();
88 }
89
90 //-----------------------------------------------------------------------------
91
defaultLanguage()92 QString WordList::defaultLanguage() {
93 QString language = QLocale().name();
94 if (!QFile::exists("connectagram:" + language)) {
95 language = language.left(2);
96 if (!QFile::exists("connectagram:" + language)) {
97 language = "en";
98 }
99 }
100 return language;
101 }
102
103 //-----------------------------------------------------------------------------
104
resetWords()105 void WordList::resetWords() {
106 m_words = m_data->words(m_length);
107 }
108
109 //-----------------------------------------------------------------------------
110
WordListData(const QString & language)111 WordList::WordListData::WordListData(const QString& language) :
112 m_maximum_length(0)
113 {
114 // Read words from disk
115 QFile file("connectagram:" + language + "/words");
116 if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
117 return;
118 }
119
120 QTextStream in(&file);
121 in.setCodec("UTF-8");
122 while (!in.atEnd()) {
123 QStringList spellings = in.readLine().simplified().split(" ", QString::SkipEmptyParts);
124 if (spellings.isEmpty()) {
125 continue;
126 }
127
128 QString word = spellings.takeFirst();
129 int length = word.length();
130 if (length < 5) {
131 continue;
132 }
133
134 m_maximum_length = std::max(m_maximum_length, length);
135 m_all_words[length - 1].append(word.toUpper());
136
137 if (!spellings.isEmpty()) {
138 m_spellings[word] = spellings;
139 }
140 }
141
142 // Adjust maximum length to account for maximum amount of words
143 for (int i = m_maximum_length - 1; i > 0; --i) {
144 if (m_all_words[i].size() >= 20) {
145 m_maximum_length = i + 1;
146 break;
147 }
148 }
149 }
150
151 //-----------------------------------------------------------------------------
152