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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
/*
* "123" "ABC" Τ褦ʼˤΤäƤʤ
* ʸФ礻ξƤθ
* 嵭¾ֹؤΥԤʤ
*
* Copyright (C) 2001-2005 TABATA Yusuke
* Copyright (C) 2004-2005 YOSHIDA Yuichi
*
*/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <anthy/anthy.h> /* for ANTHY_*_ENCODING */
#include <anthy/conf.h>
#include <anthy/xstr.h>
#include <anthy/xchar.h>
#include "dic_main.h"
#include "dic_ent.h"
/* ext entry */
static struct seq_ent unkseq_ent;/*̤ʸȤбʸȤ*/
static struct seq_ent num_ent;/*ʤ*/
static struct seq_ent sep_ent;/*ѥ졼ʤɡ*/
/* ext entrywtype*/
static wtype_t wt_num;
static xchar narrow_wide_tab[]= {WIDE_0, WIDE_1, WIDE_2,
WIDE_3, WIDE_4, WIDE_5,
WIDE_6, WIDE_7, WIDE_8, WIDE_9};
static int kj_num_tab[]={KJ_0, KJ_1, KJ_2, KJ_3, KJ_4,
KJ_5, KJ_6, KJ_7, KJ_8, KJ_9};
struct zipcode_line {
int nr;
xstr **strs;
};
/* ̾ɲä */
static void
pushback_place_name(struct zipcode_line *zl, char *pn)
{
if (pn[0] == '#') {
return ;
}
zl->strs = realloc(zl->strs, sizeof(xstr *) * (zl->nr + 1));
zl->strs[zl->nr] = anthy_cstr_to_xstr(pn, ANTHY_EUC_JP_ENCODING);
zl->nr++;
}
/* ֹ漭ѡƥڡڤФ */
static void
parse_zipcode_line(struct zipcode_line *zl, char *ln)
{
char buf[1000];
int i = 0;
while (*ln) {
buf[i] = *ln;
if (*ln == '\\') {
buf[i] = ln[1];
i ++;
if (ln[1]) {
ln ++;
}
} else if (*ln == ' ') {
buf[i] = 0;
i = 0;
pushback_place_name(zl, buf);
} else {
i ++;
}
/**/
ln ++;
}
buf[i] = 0;
pushback_place_name(zl, buf);
}
/* ֹ漭õ */
static void
search_zipcode_dict(struct zipcode_line *zl, xstr* xs)
{
FILE *fp;
char buf[1000];
int len;
xstr *temp;
char *index;
zl->nr = 0;
zl->strs = NULL;
fp = fopen(anthy_conf_get_str("ZIPDICT_EUC"), "r");
if (!fp) {
return ;
}
/* ȾѡѤۼ */
temp = anthy_xstr_wide_num_to_num(xs);
index = anthy_xstr_to_cstr(temp, 0);
len = strlen(index);
/* grep */
while (fgets(buf, 1000, fp)) {
/* 3ʸֹ椬7ʸֹƬ˥ޥåʤ褦 */
if (!strncmp(buf, index, len) && buf[len] == ' ') {
/* Ԥä */
buf[strlen(buf) - 1] = 0;
parse_zipcode_line(zl, &buf[len + 1]);
}
}
anthy_free_xstr(temp); /* ν */
free(index);
fclose(fp);
}
/* ֹ漭ξ */
static void
free_zipcode_line(struct zipcode_line *zl)
{
int i;
for (i = 0; i < zl->nr; i++) {
anthy_free_xstr(zl->strs[i]);
}
free(zl->strs);
}
static int
gen_zipcode(xstr* xs, xstr *dest, int nth)
{
struct zipcode_line zl;
/* ֹ漭̾ɤ */
search_zipcode_dict(&zl, xs);
/* */
if (zl.nr > nth) {
dest->len = zl.strs[nth]->len;
dest->str = anthy_xstr_dup_str(zl.strs[nth]);
free_zipcode_line(&zl);
return 0;
} else {
free_zipcode_line(&zl);
return -1;
}
}
/* ȾѤοѤο */
static xchar
narrow_num_to_wide_num(xchar xc)
{
if (xc > '9' || xc < '0') {
return WIDE_0;
}
return narrow_wide_tab[(int)(xc - '0')];
}
/* ѤοȾѤο */
static xchar
wide_num_to_narrow_num(xchar xc)
{
int i;
for (i = 0; i < 10; i++) {
if (xc == narrow_wide_tab[i]) {
return i + '0';
}
}
return '0';
}
/*
* Ѵ
*/
static xchar
get_kj_num(int n)
{
if (n > 9 || n < 1) {
return KJ_10;
}
return kj_num_tab[n];
}
/*
* 4ʬʸȤƤ
*/
static void
compose_num_component(xstr *xs, long long num)
{
int n[4],i;
int a[4] = { 0 , KJ_10, KJ_100, KJ_1000};
for (i = 0; i < 4; i++) {
n[i] = num-(num/10)*10;
num /= 10;
}
/* 10,100,1000ΰ */
for (i = 3; i > 0; i--) {
if (n[i] > 0) {
if (n[i] > 1) {
anthy_xstrappend(xs, get_kj_num(n[i]));
}
anthy_xstrappend(xs, a[i]);
}
}
/* 1ΰ */
if (n[0]) {
anthy_xstrappend(xs, get_kj_num(n[0]));
}
}
/** ʸ */
static int
gen_kanji_num(long long num, xstr *dest)
{
int i;
int n[10];
if (num < 1 || num >= 10000000000000000LL) {
return -1;
}
/* 4夺nˤĤ */
for (i = 0; i < 10; i ++) {
n[i] = num-(num/10000)*10000;
num = num/10000;
}
/**/
dest->len = 0;
dest->str = 0;
/* ΰ̤Ĥ */
if (n[3]) {
compose_num_component(dest, n[3]);
anthy_xstrappend(dest, KJ_1000000000000);
}
/* ΰ̤Ĥ */
if (n[2]) {
compose_num_component(dest, n[2]);
anthy_xstrappend(dest, KJ_100000000);
}
/* ΰ̤Ĥ */
if (n[1]) {
compose_num_component(dest, n[1]);
anthy_xstrappend(dest, KJ_10000);
}
/**/
compose_num_component(dest, n[0]);
return 0;
}
static int
get_nr_zipcode(xstr* xs)
{
struct zipcode_line zl;
int nr = 0;
if (xs->len != 3 && xs->len != 7) {
return 0;
}
/* ֹ漭̾ɤ */
search_zipcode_dict(&zl, xs);
nr = zl.nr;
free_zipcode_line(&zl);
return nr;
}
static int
get_nr_num_ents(long long num)
{
if (num > 0 && num < 10000000000000000LL) {
if (num > 999) {
/* ӥ(Τޤ)ӥ(Ⱦؤ)
3ڤ(ѡȾ) */
return 5;
} else {
/* ӥ(Τޤ)Ⱦؤ */
return 3;
}
} else {
/* ӥ(Τޤ)Ⱦؤ */
return 2;
}
}
/*
* ĤιΥȥ뤫
*/
int
anthy_get_nr_dic_ents_of_ext_ent(seq_ent_t se, xstr *xs)
{
if (se == &unkseq_ent) {
return 1;
}
if (anthy_get_xstr_type(xs) & (XCT_NUM|XCT_WIDENUM)) {
long long num = anthy_xstrtoll(xs);
return get_nr_num_ents(num) + get_nr_zipcode(xs);
}
return 0;
}
/* ʸȾѤ */
static void
toggle_wide_narrow(xstr *dest, xstr *src)
{
int f, i;
dest->len = src->len;
dest->str = anthy_xstr_dup_str(src);
f = anthy_get_xstr_type(src) & XCT_WIDENUM;
for (i = 0; i < dest->len; i++) {
if (f) {
dest->str[i] = wide_num_to_narrow_num(src->str[i]);
} else {
dest->str[i] = narrow_num_to_wide_num(src->str[i]);
}
}
}
/* 3˶ڤä */
static int
gen_separated_num(long long num, xstr *dest, int full)
{
int width = 0, dot_count;
long long tmp;
int i, pos;
if (num < 1000) {
return -1;
}
/* */
for (tmp = num; tmp != 0; tmp /= 10) {
width ++;
}
/* ο */
dot_count = (width - 1) / 3;
/* ǼΤɬפʸѰդ */
dest->len = dot_count + width;
dest->str = malloc(sizeof(xchar)*dest->len);
/* η夫˷Ƥ */
for (i = 0, pos = dest->len - 1; i < width; i++, pos --) {
int n = num % 10;
/* ޤɲ */
if (i > 0 && (i % 3) == 0) {
if (full) {
dest->str[pos] = WIDE_COMMA;
} else {
dest->str[pos] = ',';
}
pos --;
}
if (full) {
/* ѿ */
dest->str[pos] = narrow_wide_tab[n];
} else {
/* ASCII */
dest->str[pos] = 48 + n;
}
num /= 10;
}
return 0;
}
/*
* nthĤθФ
*/
int
anthy_get_nth_dic_ent_str_of_ext_ent(seq_ent_t se, xstr *xs,
int nth, xstr *dest)
{
dest->str = NULL; /* ʥꥢ¿ŲХν */
dest->len = 0;
if (nth == 0) {
/* ̵Ѵʸ */
dest->len = xs->len;
dest->str = anthy_xstr_dup_str(xs);
return 0;
}
if (se == &unkseq_ent) {
switch(nth) {
case 1:
/* ѡȾѤΥȥ */
return 0;
}
}
if (anthy_get_xstr_type(xs) & (XCT_NUM|XCT_WIDENUM)) {
long long num = anthy_xstrtoll(xs);
const int base_ents = get_nr_num_ents(num); /* ֹؤб */
/* ӥȾؤ */
switch(nth) {
case 1:
/* ȾѤ촹 */
toggle_wide_narrow(dest, xs);
return 0;
case 2:
/* */
if (!gen_kanji_num(num, dest)) {
return 0;
}
/* break̵ */
case 3:
/* 3Ƕڤä */
if (!gen_separated_num(num, dest, 0)) {
return 0;
}
/* break̵ */
case 4:
/* 3Ƕڤä() */
if (!gen_separated_num(num, dest, 1)) {
return 0;
}
/* break̵ */
default:
/* ֹ */
if (base_ents <= nth) { /* ֹؤб */
if (xs->len == 3 || xs->len == 7) {
if (!gen_zipcode(xs, dest, nth - base_ents)) { /* ֹؤб */
return 0;
}
}
}
break;
}
return -1;
}
return 0;
}
int
anthy_get_ext_seq_ent_indep(struct seq_ent *se)
{
if (se == &num_ent || se == &unkseq_ent) {
return 1;
}
return 0;
}
/* ѷ */
int
anthy_get_ext_seq_ent_ct(struct seq_ent *se, int pos, int ct)
{
if (anthy_get_ext_seq_ent_pos(se, pos) && ct == CT_NONE) {
/* ʻ줬äƤƤ̵Ѥξ
(ext_entϳѤʤ) */
return 10;
}
return 0;
}
/* ʻ */
int
anthy_get_ext_seq_ent_pos(struct seq_ent *se, int pos)
{
/* ext_ent̾Τ */
if (se == &num_ent && pos == POS_NOUN) {
return 10;
}
if ((se == &unkseq_ent) && pos == POS_NOUN) {
return 10;
}
return 0;
}
/*
* ˤΤäƤʤ
*/
seq_ent_t
anthy_get_ext_seq_ent_from_xstr(xstr *x, int is_reverse)
{
int t = anthy_get_xstr_type(x);
/* ΤߤǹƤ num_ent */
if (t & (XCT_NUM | XCT_WIDENUM)) {
return &num_ent;
}
/* ѿʤunkseq */
if (t & XCT_ASCII) {
return &unkseq_ent;
}
if (t & XCT_KATA) {
return &unkseq_ent;
}
if (!is_reverse) {
/* ѴϴϺʤ */
if (t & XCT_KANJI) {
return &unkseq_ent;
}
}
if (x->len == 1) {
/* ˤΤäƤʤ1ʸʤ饻ѥ졼 */
return &sep_ent;
}
return 0;
}
int
anthy_get_nth_dic_ent_wtype_of_ext_ent(xstr *xs, int nth,
wtype_t *wt)
{
int type;
(void)nth;
type = anthy_get_xstr_type(xs);
if (type & (XCT_NUM | XCT_WIDENUM)) {
*wt = wt_num;
return 0;
}
if (type & XCT_KATA) {
*wt = anthy_get_wtype(POS_NOUN, COS_NONE, SCOS_NONE, CC_NONE,
CT_NONE, WF_INDEP);
return 0;
}
return -1;
}
int
anthy_get_nth_dic_ent_freq_of_ext_ent(struct seq_ent *se, int nth)
{
(void)se;
(void)nth;
return 100;
}
int
anthy_get_ext_seq_ent_wtype(struct seq_ent *se, wtype_t w)
{
if (se == &num_ent) {
if (anthy_wtype_include(w, wt_num)) {
/* ξ */
return 10;
}
return 0;
}
if (anthy_wtype_get_pos(w) == POS_NOUN &&
anthy_wtype_get_cos(w) == COS_NONE &&
anthy_wtype_get_scos(w) == SCOS_NONE) {
/* ̾졢ʻʤʻ̵˥ޥå */
return 10;
}
return 0;
}
void
anthy_init_ext_ent(void)
{
/**/
unkseq_ent.seq_type = 0;
unkseq_ent.nr_dic_ents = 0;
num_ent.seq_type = 0;
num_ent.nr_dic_ents = 0;
sep_ent.seq_type = 0;
sep_ent.nr_dic_ents = 0;
/**/
wt_num = anthy_init_wtype_by_name("");
}
|