-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKasumiDic.cxx
297 lines (253 loc) · 7.35 KB
/
KasumiDic.cxx
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
/*********************************************************************
*
* KasumiDic.cxx
*
* Kasumi - a management tool for a private dictionary of anthy
*
* Copyright (C) 2004-2006 Takashi Nakamoto
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*********************************************************************/
#include "KasumiDic.hxx"
#include "KasumiWord.hxx"
#include "KasumiString.hxx"
#include "KasumiException.hxx"
#include "KasumiConfiguration.hxx"
extern "C"{ // ad-hoc solution for a defect of Anthy
#include <anthy/dicutil.h>
}
#include <anthy/anthy.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
#define OptionOutput( Word, OptionName ) (string(OptionName) + " = " + (Word->getOption(OptionName) ? "y" : "n"))
#define BUFFER_SIZE (255)
KasumiDic::KasumiDic(KasumiConfiguration *conf)
throw(KasumiException){
try{
load(conf);
}catch(KasumiException e){
throw e;
}
}
KasumiDic::~KasumiDic()
{
list<KasumiWord*>::iterator p;
while(!mWordList.empty())
{
p = mWordList.begin();
mWordList.pop_front();
delete (*p);
}
}
void KasumiDic::load(KasumiConfiguration *conf)
throw(KasumiException){
const int FREQ_LBOUND = conf->getPropertyValueByInt("MinFrequency");
const int FREQ_UBOUND = conf->getPropertyValueByInt("MaxFrequency");
int anthy_version = atoi(anthy_get_version_string());
assert(anthy_version != 0);
try{
if(anthy_priv_dic_select_first_entry() == -1){
// no word
return;
}
else if(anthy_priv_dic_select_first_entry() == -3)
{
if(anthy_version >= 7716)
{
// do not throw exception in the case that this doesn't fail to
// read the private dictionary but the dictionary containes
// no word. This case happens if the version of anthy is less
// than 7714.
string message = string("Failed to read private dictionary. This problem might be a problem of Anthy.\n");
throw KasumiException(message, STDERR, KILL);
}
}
char sound[BUFFER_SIZE], wt[BUFFER_SIZE], spelling[BUFFER_SIZE];
int freq;
do{
if (anthy_priv_dic_get_index(sound, BUFFER_SIZE) &&
anthy_priv_dic_get_wtype(wt, BUFFER_SIZE) &&
anthy_priv_dic_get_word(spelling, BUFFER_SIZE)) {
freq = anthy_priv_dic_get_freq();
// corret frequency value crossing the bounds
if(FREQ_LBOUND > freq)
freq = FREQ_LBOUND;
if(FREQ_UBOUND < freq)
freq = FREQ_UBOUND;
KasumiWord *newWord = KasumiWord::createNewWord(conf);
newWord->setSound(string(sound));
if (anthy_version < 7710 && spelling[0] == ' ') {
// Measures against a defect of anthy.
// "anthy_priv_dic_get_word()" function returns
// a string whose first character is an unwated
// white space.
newWord->setSpelling(string(spelling+1));
} else
newWord->setSpelling(string(spelling));
newWord->setFrequency(freq);
newWord->setWordType(KasumiWordType::getWordTypeFromCannaTab(string(wt)));
appendWord(newWord);
}
}while(anthy_priv_dic_select_next_entry() == 0);
}catch(KasumiException e){
throw e;
}
}
void KasumiDic::appendWord(KasumiWord *word){
// check duplication
list<KasumiWord*>::iterator p = mWordList.begin();
while(p != mWordList.end() )
{
if((*p)->getID() == word->getID())
return; // nothing to do
p++;
}
word->registerEventListener(this);
mWordList.push_back(word);
// raise event
for(size_t i=0;i<EventListeners.size();i++){
EventListeners[i]->appendedWord(word);
}
}
void KasumiDic::removeWord(unsigned int id)
{
int flag = 0;
list<KasumiWord*>::iterator p = mWordList.begin();
while(p != mWordList.end() )
{
if((*p)->getID() == id)
{
mWordList.erase(p);
(*p)->removeEventListener(this);
free(*p);
flag = 1;
break;
}
p++;
}
// raise event
if(flag)
for(size_t i=0;i<EventListeners.size();i++){
EventListeners[i]->removedWord(id);
}
}
void KasumiDic::store()
throw(KasumiException)
{
list<KasumiWord*>::iterator p = mWordList.begin();
anthy_priv_dic_delete();
while( p != mWordList.end() )
{
string spelling = (*p)->getSpelling();
string sound = (*p)->getSound();
string wt = (*p)->getWordType()->getCannaTab();
int freq = (*p)->getFrequency();
int ret = anthy_priv_dic_add_entry(sound.c_str(),
spelling.c_str(),
wt.c_str(),
freq);
if (ret == -1)
{
throw KasumiException(string("Failed to register") + sound,
ERR_DIALOG,
KILL);
}
p++;
}
}
void KasumiDic::registerEventListener(KasumiDicEventListener *listener){
int i,size;
size = EventListeners.size();
// if the listener have been already registered, nothing to do
// assuring no duplication
for(i=0;i<size;i++){
if(EventListeners[i] == listener){
return;
}
}
EventListeners.push_back(listener);
}
void KasumiDic::removeEventListener(KasumiDicEventListener *listener){
vector<KasumiDicEventListener*>::iterator i;
KasumiDicEventListener *p;
for(i=EventListeners.begin();i!=EventListeners.end();i++){
p = *i;
if(p == listener){
EventListeners.erase(i);
return;
}
}
}
void KasumiDic::changedFrequency(KasumiWord *word)
{
for(size_t i=0;i<EventListeners.size();i++){
EventListeners[i]->modifiedWord(word);
}
}
void KasumiDic::changedSpelling(KasumiWord *word)
{
for(size_t i=0;i<EventListeners.size();i++){
EventListeners[i]->modifiedWord(word);
}
}
void KasumiDic::changedSound(KasumiWord *word)
{
for(size_t i=0;i<EventListeners.size();i++){
EventListeners[i]->modifiedWord(word);
}
}
void KasumiDic::changedWordType(KasumiWord *word)
{
for(size_t i=0;i<EventListeners.size();i++){
EventListeners[i]->modifiedWord(word);
}
}
/*
// for debug
//
// % g++ -g KasumiDic.cxx KasumiWord.cxx KasumiException.cxx KasumiConfiguration.cxx KasumiWordType.cxx KasumiString.cxx `pkg-config --libs --cflags anthy gtk+-2.0`
void output(KasumiDic *dic)
{
list<KasumiWord*>::iterator p = dic->firstWordIter();
while(p != dic->endWordIter()){
cout << (*p)->getID() << " " << (*p)->getSpelling() << endl;
p++;
}
}
int main(int argc, char *argv[])
{
anthy_dic_util_init();
KasumiWordType::initWordTypeList();
KasumiConfiguration *conf = new KasumiConfiguration(argc, argv);
KasumiDic *dic = new KasumiDic(conf);
KasumiWord *word = KasumiWord::createNewWord(conf);
word->setSpellingByUTF8("テスト");
word->setSoundByUTF8("てすと");
dic->appendWord(word);
output(dic);
while(1);
dic->removeWord(1);
output(dic);
}
*/