| 1 |
/********************************************************************* |
| 2 |
* |
| 3 |
* KasumiDic.cxx |
| 4 |
* |
| 5 |
* Kasumi - a management tool for a private dictionary of anthy |
| 6 |
* |
| 7 |
* Copyright (C) 2004-2006 Takashi Nakamoto |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License |
| 11 |
* as published by the Free Software Foundation; either version 2 |
| 12 |
* of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 22 |
* MA 02110-1301, USA. |
| 23 |
* |
| 24 |
*********************************************************************/ |
| 25 |
|
| 26 |
#include "KasumiDic.hxx" |
| 27 |
#include "KasumiWord.hxx" |
| 28 |
#include "KasumiString.hxx" |
| 29 |
#include "KasumiException.hxx" |
| 30 |
#include "KasumiConfiguration.hxx" |
| 31 |
extern "C"{ // ad-hoc solution for a defect of Anthy |
| 32 |
#include <anthy/dicutil.h> |
| 33 |
} |
| 34 |
#include <anthy/anthy.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <cstdlib> |
| 37 |
#include <iostream> |
| 38 |
#include <fstream> |
| 39 |
#include <sstream> |
| 40 |
#include <string> |
| 41 |
#include <vector> |
| 42 |
#include <cassert> |
| 43 |
|
| 44 |
using namespace std; |
| 45 |
|
| 46 |
#define OptionOutput( Word, OptionName ) (string(OptionName) + " = " + (Word->getOption(OptionName) ? "y" : "n")) |
| 47 |
#define BUFFER_SIZE (255) |
| 48 |
|
| 49 |
KasumiDic::KasumiDic(KasumiConfiguration *conf) |
| 50 |
throw(KasumiException){ |
| 51 |
|
| 52 |
try{ |
| 53 |
load(conf); |
| 54 |
}catch(KasumiException e){ |
| 55 |
throw e; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
KasumiDic::~KasumiDic() |
| 60 |
{ |
| 61 |
list<KasumiWord*>::iterator p; |
| 62 |
|
| 63 |
while(!mWordList.empty()) |
| 64 |
{ |
| 65 |
p = mWordList.begin(); |
| 66 |
mWordList.pop_front(); |
| 67 |
delete (*p); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void KasumiDic::load(KasumiConfiguration *conf) |
| 72 |
throw(KasumiException){ |
| 73 |
|
| 74 |
const int FREQ_LBOUND = conf->getPropertyValueByInt("MinFrequency"); |
| 75 |
const int FREQ_UBOUND = conf->getPropertyValueByInt("MaxFrequency"); |
| 76 |
|
| 77 |
int anthy_version = atoi(anthy_get_version_string()); |
| 78 |
assert(anthy_version != 0); |
| 79 |
|
| 80 |
try{ |
| 81 |
if(anthy_priv_dic_select_first_entry() == -1){ |
| 82 |
// no word |
| 83 |
return; |
| 84 |
} |
| 85 |
else if(anthy_priv_dic_select_first_entry() == -3) |
| 86 |
{ |
| 87 |
if(anthy_version >= 7716) |
| 88 |
{ |
| 89 |
// do not throw exception in the case that this doesn't fail to |
| 90 |
// read the private dictionary but the dictionary containes |
| 91 |
// no word. This case happens if the version of anthy is less |
| 92 |
// than 7714. |
| 93 |
string message = string("Failed to read private dictionary. This problem might be a problem of Anthy.\n"); |
| 94 |
throw KasumiException(message, STDERR, KILL); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
char sound[BUFFER_SIZE], wt[BUFFER_SIZE], spelling[BUFFER_SIZE]; |
| 99 |
int freq; |
| 100 |
|
| 101 |
do{ |
| 102 |
if (anthy_priv_dic_get_index(sound, BUFFER_SIZE) && |
| 103 |
anthy_priv_dic_get_wtype(wt, BUFFER_SIZE) && |
| 104 |
anthy_priv_dic_get_word(spelling, BUFFER_SIZE)) { |
| 105 |
freq = anthy_priv_dic_get_freq(); |
| 106 |
|
| 107 |
// corret frequency value crossing the bounds |
| 108 |
if(FREQ_LBOUND > freq) |
| 109 |
freq = FREQ_LBOUND; |
| 110 |
if(FREQ_UBOUND < freq) |
| 111 |
freq = FREQ_UBOUND; |
| 112 |
|
| 113 |
KasumiWord *newWord = KasumiWord::createNewWord(conf); |
| 114 |
|
| 115 |
newWord->setSound(string(sound)); |
| 116 |
if (anthy_version < 7710 && spelling[0] == ' ') { |
| 117 |
// Measures against a defect of anthy. |
| 118 |
// "anthy_priv_dic_get_word()" function returns |
| 119 |
// a string whose first character is an unwated |
| 120 |
// white space. |
| 121 |
newWord->setSpelling(string(spelling+1)); |
| 122 |
} else |
| 123 |
newWord->setSpelling(string(spelling)); |
| 124 |
newWord->setFrequency(freq); |
| 125 |
newWord->setWordType(KasumiWordType::getWordTypeFromCannaTab(string(wt))); |
| 126 |
appendWord(newWord); |
| 127 |
} |
| 128 |
}while(anthy_priv_dic_select_next_entry() == 0); |
| 129 |
}catch(KasumiException e){ |
| 130 |
throw e; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void KasumiDic::appendWord(KasumiWord *word){ |
| 135 |
// check duplication |
| 136 |
list<KasumiWord*>::iterator p = mWordList.begin(); |
| 137 |
while(p != mWordList.end() ) |
| 138 |
{ |
| 139 |
if((*p)->getID() == word->getID()) |
| 140 |
return; // nothing to do |
| 141 |
p++; |
| 142 |
} |
| 143 |
|
| 144 |
word->registerEventListener(this); |
| 145 |
mWordList.push_back(word); |
| 146 |
|
| 147 |
// raise event |
| 148 |
for(size_t i=0;i<EventListeners.size();i++){ |
| 149 |
EventListeners[i]->appendedWord(word); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
void KasumiDic::removeWord(unsigned int id) |
| 154 |
{ |
| 155 |
int flag = 0; |
| 156 |
|
| 157 |
list<KasumiWord*>::iterator p = mWordList.begin(); |
| 158 |
while(p != mWordList.end() ) |
| 159 |
{ |
| 160 |
if((*p)->getID() == id) |
| 161 |
{ |
| 162 |
mWordList.erase(p); |
| 163 |
(*p)->removeEventListener(this); |
| 164 |
free(*p); |
| 165 |
flag = 1; |
| 166 |
break; |
| 167 |
} |
| 168 |
p++; |
| 169 |
} |
| 170 |
|
| 171 |
// raise event |
| 172 |
if(flag) |
| 173 |
for(size_t i=0;i<EventListeners.size();i++){ |
| 174 |
EventListeners[i]->removedWord(id); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void KasumiDic::store() |
| 179 |
throw(KasumiException) |
| 180 |
{ |
| 181 |
list<KasumiWord*>::iterator p = mWordList.begin(); |
| 182 |
|
| 183 |
anthy_priv_dic_delete(); |
| 184 |
|
| 185 |
while( p != mWordList.end() ) |
| 186 |
{ |
| 187 |
string spelling = (*p)->getSpelling(); |
| 188 |
string sound = (*p)->getSound(); |
| 189 |
string wt = (*p)->getWordType()->getCannaTab(); |
| 190 |
int freq = (*p)->getFrequency(); |
| 191 |
|
| 192 |
int ret = anthy_priv_dic_add_entry(sound.c_str(), |
| 193 |
spelling.c_str(), |
| 194 |
wt.c_str(), |
| 195 |
freq); |
| 196 |
|
| 197 |
if (ret == -1) |
| 198 |
{ |
| 199 |
throw KasumiException(string("Failed to register") + sound, |
| 200 |
ERR_DIALOG, |
| 201 |
KILL); |
| 202 |
} |
| 203 |
|
| 204 |
p++; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void KasumiDic::registerEventListener(KasumiDicEventListener *listener){ |
| 209 |
int i,size; |
| 210 |
|
| 211 |
size = EventListeners.size(); |
| 212 |
|
| 213 |
// if the listener have been already registered, nothing to do |
| 214 |
// assuring no duplication |
| 215 |
for(i=0;i<size;i++){ |
| 216 |
if(EventListeners[i] == listener){ |
| 217 |
return; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
EventListeners.push_back(listener); |
| 222 |
} |
| 223 |
|
| 224 |
void KasumiDic::removeEventListener(KasumiDicEventListener *listener){ |
| 225 |
vector<KasumiDicEventListener*>::iterator i; |
| 226 |
KasumiDicEventListener *p; |
| 227 |
|
| 228 |
for(i=EventListeners.begin();i!=EventListeners.end();i++){ |
| 229 |
p = *i; |
| 230 |
if(p == listener){ |
| 231 |
EventListeners.erase(i); |
| 232 |
return; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void KasumiDic::changedFrequency(KasumiWord *word) |
| 238 |
{ |
| 239 |
for(size_t i=0;i<EventListeners.size();i++){ |
| 240 |
EventListeners[i]->modifiedWord(word); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
void KasumiDic::changedSpelling(KasumiWord *word) |
| 245 |
{ |
| 246 |
for(size_t i=0;i<EventListeners.size();i++){ |
| 247 |
EventListeners[i]->modifiedWord(word); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void KasumiDic::changedSound(KasumiWord *word) |
| 252 |
{ |
| 253 |
for(size_t i=0;i<EventListeners.size();i++){ |
| 254 |
EventListeners[i]->modifiedWord(word); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
void KasumiDic::changedWordType(KasumiWord *word) |
| 259 |
{ |
| 260 |
for(size_t i=0;i<EventListeners.size();i++){ |
| 261 |
EventListeners[i]->modifiedWord(word); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
// for debug |
| 267 |
// |
| 268 |
// % g++ -g KasumiDic.cxx KasumiWord.cxx KasumiException.cxx KasumiConfiguration.cxx KasumiWordType.cxx KasumiString.cxx `pkg-config --libs --cflags anthy gtk+-2.0` |
| 269 |
void output(KasumiDic *dic) |
| 270 |
{ |
| 271 |
list<KasumiWord*>::iterator p = dic->firstWordIter(); |
| 272 |
while(p != dic->endWordIter()){ |
| 273 |
cout << (*p)->getID() << " " << (*p)->getSpelling() << endl; |
| 274 |
p++; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
int main(int argc, char *argv[]) |
| 279 |
{ |
| 280 |
anthy_dic_util_init(); |
| 281 |
|
| 282 |
KasumiWordType::initWordTypeList(); |
| 283 |
KasumiConfiguration *conf = new KasumiConfiguration(argc, argv); |
| 284 |
KasumiDic *dic = new KasumiDic(conf); |
| 285 |
KasumiWord *word = KasumiWord::createNewWord(conf); |
| 286 |
word->setSpellingByUTF8("テスト"); |
| 287 |
word->setSoundByUTF8("てすと"); |
| 288 |
dic->appendWord(word); |
| 289 |
output(dic); |
| 290 |
|
| 291 |
while(1); |
| 292 |
|
| 293 |
dic->removeWord(1); |
| 294 |
output(dic); |
| 295 |
} |
| 296 |
|
| 297 |
*/ |