-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDictionary.cc
342 lines (327 loc) · 8.09 KB
/
Dictionary.cc
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
#include "Dictionary.h"
Dictionary::Dictionary()
{
}
void Dictionary::create(const string& fileName)
{
dict.clear();
open(fileName);
}
bool Dictionary::empty() const
{
return dict.empty();
}
void Dictionary::open(const string& fileName)
{
string kakxih = fileName.substr(fileName.find_last_of("."),4);
if(kakxih == ".txt")
openTXTFile(fileName);
else
openSKKFile(fileName);
}
void Dictionary::openSKKFile(const string& fileName)
{
//out << fileName.c_str() << endl;
ifstream in(fileName);
if(!in)
{
cout << "打开文件出错!" << endl;
return;
}
string line_before;
while(getline(in, line_before)) //一行一字
{
if(line_before=="") continue;
vector<string> line_after;
split(line_before, "!", line_after); //拆分字、音: 干 k#on#1:天干;5:事干|k#an#1;5
string zy = line_after[0]; //存字: 干
string in_before = line_after[1]; //存音: k#on#1:天干;5:事干|k#an#1;5
vector<In> in;
vector<string> in_after;
split(in_before, "|", in_after); //拆音: k#on#1:天干;5:事干 k#an#1;5
for(auto each_in : in_after) //每个音: k#on#1:天干;5:事干
{
In i;
vector<string> each_in_after;
split(each_in, "_", each_in_after); //拆声、韵、调: k on 1:天干;5:事干
i.xin = each_in_after[0]; //存声 k
i.yvn = each_in_after[1]; //存韵 on
i.binlih = each_in_after[2]; //存频率
string diao_before = each_in_after[2]; //存调 1:天干&5:事干
map<string, vector<string>> diao_map;
vector<string> diao_after;
split(diao_before,"&", diao_after); // 1:天干 5:事干
for(auto each_diao : diao_after) // 1:天干
{
vector<string> each_diao_after;
split(each_diao, ":", each_diao_after);
string diao = each_diao_after[0]; // 1
vector<string> kaxihs;
if(each_diao_after.size()>1) {
string kaxih_before = each_diao_after[1]; // 天干;干支
split(kaxih_before, ";", kaxihs);
}
i.diao_map[diao]=kaxihs;
}
in.push_back(i);
}
dict[zy] = in;
}
in.close();
}
void Dictionary::openTXTFile(const string& fileName)
{
//out << fileName.c_str() << endl;
ifstream in(fileName);
if(!in)
{
cout << "打开文件出错!" << endl;
return;
}
string line;
string zy, xin, binlih, yvn, diao, kaxih;
while(getline(in, line) ) //一行
{
if(line=="") continue;
vector<string> input;
split(line, "\t", input);
zy = input.at(0);
xin = input.at(1);
yvn = input.at(2);
binlih = input.at(3);
diao = input.at(4);
kaxih = input.at(5);
if(!add(zy,xin,yvn,binlih,diao,kaxih)) cout << "Add Error" << endl;
}
in.close();
}
void Dictionary::print(ostream& out) const
{ //读取
for(auto word : dict)
{
out << word.first << "!";
bool first_word = true;
for(auto in : word.second)
{
if(!first_word) out << "|";
else first_word = false;
out << in.xin << "_";
out << in.yvn << "_";
out << in.binlih << "_";
bool first_diao = true;
for(auto diao : in.diao_map)
{
if(!first_diao) out << "&";
else first_diao = false;
out << diao.first;
if(diao.second.size()) {
out << ":";
bool first = true;
for(auto kaxih : diao.second)
{
if(!first) out << ";";
else first = false;
out << kaxih;
}
}
}
}
out << endl;
}
}
void Dictionary::printTXT(ostream& out) const
{
for(auto word : dict)
{
for(auto in : word.second)
{
for(auto diao : in.diao_map)
{
out << word.first << "\t";
out << in.xin << "\t";
out << in.yvn << "\t";
out << in.binlih << "\t";
out << diao.first << "\t";
if(diao.second.size()) {
bool first = true;
for(auto kaxih : diao.second)
{
if(!first) out << ";";
else first = false;
out << kaxih;
}
}
out << endl;
}
}
}
}
In Dictionary::addIn(const string& xin, const string& yvn,
const string& binlih, const string& diao,
const string& kaxih)
{
In in;
in.xin = xin;
in.yvn = yvn;
in.binlih = binlih;
map<string, vector<string>> diao_map;
vector<string> kaxihs;
if(kaxih!="") kaxihs.push_back(kaxih);
in.diao_map[diao]=kaxihs;
return in;
}
bool Dictionary::add(const string& zy, string xin,
string yvn, string binlih,
string diao, string kaxih)
{
if(xin=="?") xin="";
if(yvn=="?") yvn="";
if(binlih=="?") binlih="";
if(diao=="?") diao="";
if(kaxih=="?") kaxih="";
if(dict.find(zy)==dict.end())
{
//cout << "(1)呒" << zy << xin << yvn << diao << ",加字."<<endl;
vector<In> ins;
ins.push_back(addIn(xin,yvn,binlih,diao,kaxih));
dict[zy] = ins;
//cout << "加字成功!" << endl;
}
else
{
//cout << "(2)有" << zy << "," <<endl;
auto begin_dict = dict[zy].begin(), end_dict = dict[zy].end();
while(begin_dict!=end_dict)
{
//cout << "(3)寻" <<xin << yvn << diao <<"," <<endl;
if(begin_dict->xin == xin && begin_dict->yvn == yvn)
{
//cout << "(4)有音" << xin << yvn << "," << endl;
if(begin_dict->diao_map.find(diao)==begin_dict->diao_map.end())
{
//cout << "(6)呒调" << diao << ",加调."<<endl;
vector<string> kaxihs;
if(kaxih!="") {
split(kaxih,";",kaxihs);
}
begin_dict->diao_map[diao]=kaxihs;
}
else{
//cout << "(7)有调" << diao << "," << endl;
if(kaxih!="") {
auto begin = begin_dict->diao_map[diao].begin(),
end = begin_dict->diao_map[diao].end();
while(begin!=end)
{
if((*begin)==kaxih) break;
++begin;
}
if(begin==end) {
//cout << "呒解释" << kaxih << ",加解释." << endl;
begin_dict->diao_map[diao].push_back(kaxih);
}
}
}
break;
}
//cout << "继续寻!" << endl;
++begin_dict;
}
if(begin_dict==end_dict)
{
//cout << "(5)呒音" << xin << yvn << diao << ",加音."<<endl;
dict[zy].push_back(addIn(xin,yvn,binlih,diao,kaxih));
}
}
return true;
}
bool Dictionary::remove(const string& zy)
{
if(dict.erase(zy)) return true;
return false;
}
bool Dictionary::remove(const string& zy, string xin, string yvn)
{
if(xin=="?") xin="";
if(yvn=="?") yvn="";
auto in_vec = dict.find(zy);
if(in_vec == dict.end() ) { return false; } //字典里没有这个字 及 音vec
auto begin = in_vec->second.begin();
auto end = in_vec->second.cend();
while(begin != end) //遍历 音vec
{
if(begin->xin == xin and begin->yvn == yvn) //找到所需的音
{
in_vec->second.erase(begin); //删除音
break; //只要找到就break,使得begin!=end
}
++begin;
}
if(begin==end) return false; //未找到
return true;
}
bool Dictionary::remove(const string& zy, string xin, string yvn, string diao)
{
if(xin=="?") xin="";
if(yvn=="?") yvn="";
if(diao=="?") diao="";
auto in_vec = dict.find(zy);
if(in_vec == dict.end() ) { //字典里有这个字的 音vec
cout << "Not Found: " << zy << endl;
return false;
}
auto begin = in_vec->second.begin();
auto end = in_vec->second.end();
while(begin!=end) //遍历 音vec
{
if(begin->xin == xin && begin->yvn == yvn) //找到所需的音
{
auto kaxih_vec = begin->diao_map.find(diao);
if(kaxih_vec != begin->diao_map.end() ) //找到diao及解释vec
{
begin->diao_map.erase(kaxih_vec); //调字典里删除调
break;
}
}
++begin;
}
if(begin==end) return false;
return true;
}
bool Dictionary::remove(const string& zy, string xin, string yvn, string diao, string kaxih)
{
if(xin=="?") xin="";
if(yvn=="?") yvn="";
if(diao=="?") diao="";
if(kaxih=="?") kaxih="";
auto in_vec = dict.find(zy);
if(in_vec == dict.end() ) { //字典里有这个字的 音vec
return false;
}
auto begin = in_vec->second.begin();
auto end = in_vec->second.end();
while(begin!=end) //遍历 音vec
{
if(begin->xin == xin && begin->yvn == yvn) //找到所需的音
{
auto kaxih_vec = begin->diao_map.find(diao);
if(kaxih_vec != begin->diao_map.end() ) //找到diao及解释vec
{
auto begin_kx = kaxih_vec->second.begin();
auto end_kx = kaxih_vec->second.begin();
while(begin_kx!=end_kx)
{
if(kaxih == (*begin_kx) ) {
kaxih_vec->second.erase(begin_kx);
break;
}
++begin_kx;
}
break;
}
}
++begin;
}
if(begin==end) return false;
return true;
}