-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdictionary.cpp
231 lines (197 loc) · 6.52 KB
/
dictionary.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
// Copyright (c) 2010-2014 Sami Väisänen, Ensisoft
//
// http://www.ensisoft.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "config.h"
#include "warnpush.h"
# include <QtDebug>
# include <QFile>
# include <QTextStream>
# include <QStringList>
# include "pinyin.h"
#include "warnpop.h"
#include <stdexcept>
#include "dictionary.h"
#include "format.h"
QString make_dictionary_key(const QString& pinyin)
{
std::wstring wide = pinyin.toStdWString();
std::wstring ret;
for (const auto& c : wide)
{
ret.push_back(pinyin::toneunmap(c));
}
return QString::fromStdWString(ret);
}
QString make_dictionary_syllable(const QString& key, int tone)
{
std::wstring wide = key.toStdWString();
std::wstring syllable = pinyin::make_pinyin_syllable(wide, tone);
return QString::fromStdWString(syllable);
}
namespace pime
{
dictionary::dictionary() : wordguid_(1)
{}
dictionary::~dictionary()
{}
void dictionary::load(const QString& file, quint32 metakey)
{
QFile io(file);
if (!io.open(QIODevice::ReadOnly))
throw std::runtime_error(utf8("open dictionary failed: _1", file));
QTextStream stream(&io);
stream.setCodec("UTF-8");
while (!stream.atEnd())
{
const auto& line = stream.readLine();
const auto& toks = line.split("|");
const auto& key = make_dictionary_key(toks[2]);
const auto index = words_.size();
dictionary::word word;
word.key = key;
word.traditional = toks[0];
word.simplified = toks[1];
word.pinyin = toks[2];
word.description = toks[3];
word.guid = wordguid_++;
word.meta = metakey;
word.erased = false;
word.frequency = 0;
// bool duplicate = false;
// auto lower = words_.lower_bound(key);
// auto upper = words_.upper_bound(key);
// for (; lower != upper; ++lower)
// {
// if (lower->second.pinyin == word.pinyin)
// {
// duplicate = true;
// break;
// }
// }
//if (!duplicate)
words_.push_back(word);
index_.insert(std::make_pair(key, index));
}
}
void dictionary::save(const QString& file, quint32 metakey)
{
QFile io(file);
if (!io.open(QIODevice::WriteOnly | QIODevice::Truncate))
throw std::runtime_error(utf8("save dictionary failed: _1", file));
QTextStream stream(&io);
stream.setCodec("UTF-8");
auto beg = index_.begin();
auto end = index_.end();
for (; beg != end; ++beg)
{
const auto& key = beg->first;
const auto& word = words_[beg->second];
if (word.meta != metakey)
continue;
if (word.erased)
continue;
stream << word.traditional << "|" << word.simplified << "|" << word.pinyin << "|" << word.description;
stream << "\n";
}
}
std::vector<const dictionary::word*> dictionary::lookup(const QString& key) const
{
std::vector<const word*> ret;
auto lower = index_.lower_bound(key);
auto upper = index_.end();
for (; lower != upper; ++lower)
{
const auto& k = lower->first;
if (!k.startsWith(key))
break;
ret.push_back(&words_[lower->second]);
}
return ret;
}
std::vector<const dictionary::word*> dictionary::search(const QString& str) const
{
std::vector<const word*> ret;
auto beg = index_.begin();
auto end = index_.end();
for (; beg != end; ++beg)
{
const auto& word = words_[beg->second];
if (word.description.indexOf(str) != -1)
ret.push_back(&word);
else if (word.traditional.indexOf(str) != -1)
ret.push_back(&word);
else if (word.simplified.indexOf(str) != -1)
ret.push_back(&word);
}
return ret;
}
std::vector<const dictionary::word*> dictionary::flatten() const
{
std::vector<const word*> ret;
for (const auto& pair : index_)
ret.push_back(&words_[pair.second]);
return ret;
}
bool dictionary::store(dictionary::word& word)
{
Q_ASSERT(!word.traditional.isEmpty());
Q_ASSERT(!word.simplified.isEmpty());
Q_ASSERT(!word.pinyin.isEmpty());
word.key = make_dictionary_key(word.pinyin);
auto lower = index_.lower_bound(word.key);
auto upper = index_.upper_bound(word.key);
for (; lower != upper; ++lower)
{
auto& w = words_[lower->second];
if (w.guid != word.guid)
continue;
w.traditional = word.traditional;
w.simplified = word.simplified;
w.pinyin = word.pinyin;
w.description = word.description;
qDebug() << "Updated word: " << word.key << "Pinyin: " << word.pinyin << "Ch: " << word.traditional;
return true;
}
const auto index = words_.size();
word.guid = wordguid_++;
index_.insert(std::make_pair(word.key, index));
words_.push_back(word);
qDebug() << "Stored new word: " << word.key << "Pinyin: " << word.pinyin << " Ch: " << word.traditional;
return false;
}
bool dictionary::erase(const dictionary::word& word)
{
Q_ASSERT(!word.key.isEmpty());
Q_ASSERT(word.guid);
auto lower = index_.lower_bound(word.key);
auto upper = index_.upper_bound(word.key);
for (; lower != upper; ++lower)
{
const auto& w = words_[lower->second];
if (w.guid != word.guid)
continue;
words_[lower->second].erased = true;
index_.erase(lower);
return true;
}
return false;
}
} // pime