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 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
|
/*
* makemat.c - make table file and matrix file
*
* last modified by A.Kitauchi <akira-k@is.aist-nara.ac.jp>, Nov. 1996
*
*/
#include "chadic.h"
#define CTYPE_MAX 500
#define REN_TBL_MAX 3000
#define RENSETU_CELL_MAX (8192*4)
#ifdef VGRAM
#define HINSI_ID_MAX USHRT_MAX
#else
#define HINSI_ID_MAX UCHAR_MAX
#endif
typedef struct _kankei_t {
#ifdef VGRAM
unsigned short hinsi;
#else
unsigned char hinsi;
unsigned char bunrui;
#endif
unsigned char type;
} kankei_t;
typedef struct _rensetu_pair2_t {
short i_pos;
short j_pos;
#ifdef VGRAM
cell_t *hinsi;
#else
unsigned char hinsi;
unsigned char bunrui;
#endif
unsigned char type;
char *form;
char *goi;
} rensetu_pair2_t;
typedef struct _rensetu_cell_t {
short tbl;
short prev;
short has_rule;
} rensetu_cell_t;
static kankei_t kankei_tbl[CTYPE_MAX];
static int tbl_num;
static rensetu_pair_t rensetu_tbl[RENSETU_CELL_MAX];
static int i_num;
static int j_num;
static connect_rule_t **connect_mtr1;
static connect_rule_t **connect_mtr2;
#ifdef KOCHA
typedef unsigned short rensetu_mtr_t;
#else
typedef unsigned char rensetu_mtr_t;
#endif
static rensetu_mtr_t **rensetu_mtr1;
static rensetu_mtr_t **rensetu_mtr2;
/***********************************************************************
* read_kankei - read chasen's kankei file
***********************************************************************/
static void read_kankei()
{
FILE *fp;
cell_t *cell1, *cell2;
int j = 0;
int hinsi, bunrui, type;
char *filepath;
/* read only from current directory */
#ifdef VGRAM
fp = cha_fopen(CTYPE_FILE, "r", 1);
#else
fp = cha_fopen2(CTYPE_FILE, JM_CTYPE_FILE, "r", 1);
#endif
fprintf(stderr, "parsing %s\n", CTYPE_FILE);
while (! s_feof(fp)) {
cell1 = s_read(fp);
#ifdef VGRAM
hinsi = get_nhinsi_id(car(cell1));
#else
hinsi = get_hinsi_id(s_atom(car(car(cell1))));
if (nullp(cell2 = car(cdr(car(cell1)))) ||
!strcmp(s_atom(cell2), "*"))
bunrui = 0;
else
bunrui = get_bunrui_id(s_atom(cell2), hinsi);
#endif
cell1 = car(cdr(cell1));
while (!nullp(cell2 = car(cell1))) {
type = get_type_id(s_atom(cell2));
kankei_tbl[j].hinsi = hinsi;
#ifndef VGRAM
kankei_tbl[j].bunrui = bunrui;
#endif
kankei_tbl[j].type = type;
/*
* bug fixed by kurohashi on 92/03/16
*/
if (++j >= CTYPE_MAX)
cha_exit(1, "not enough size for CTYPE_MAX");
cell1 = cdr(cell1);
}
kankei_tbl[j].hinsi = HINSI_ID_MAX;
}
}
/***********************************************************************
* get_pair1
***********************************************************************/
static void get_pair1(cell, pair)
cell_t *cell;
rensetu_pair_t *pair;
{
cell_t *cell_p;
pair->hinsi = 0;
#ifndef VGRAM
pair->bunrui = 0;
#endif
pair->type = 0;
pair->form = 0;
pair->goi = NULL;
if (nullp(cell_p = car(cell)))
return;
#ifdef VGRAM
pair->hinsi = get_nhinsi_id(cell_p);
#else
pair->hinsi = get_hinsi_id(s_atom(cell_p));
if (nullp(cell_p = car(cell = cdr(cell))))
return;
pair->bunrui = get_bunrui_id(s_atom(cell_p), pair->hinsi);
#endif
if (nullp(cell_p = car(cell = cdr(cell))))
return;
pair->type = get_type_id(s_atom(cell_p));
if (nullp(cell_p = car(cell = cdr(cell))))
return;
pair->form = get_form_id(s_atom(cell_p), pair->type);
if (nullp(cell_p = car(cell = cdr(cell))))
return;
pair->goi = cha_strdup(s_atom(cell_p));
}
/***********************************************************************
* get_pair2
***********************************************************************/
static void get_pair2(cell, pair)
cell_t *cell;
rensetu_pair2_t *pair;
{
cell_t *cell_p;
char *s;
#ifdef VGRAM
pair->hinsi = NULL;
#else
pair->hinsi = 0;
pair->bunrui = 0;
#endif
pair->type = 0;
pair->form = NULL;
pair->goi = NULL;
if (nullp(cell_p = car(cell)))
return;
#ifdef VGRAM
s = s_atom(car(cell_p));
if (strcmp(JSTR_BUNTO, s) == 0 || strcmp(JSTR_BUNMATSU, s) == 0) {
pair->hinsi = NULL;
return;
}
pair->hinsi = cell_p;
#else
s = s_atom(cell_p);
if (strcmp(JSTR_BUNTO, s) == 0 || strcmp(JSTR_BUNMATSU, s) == 0) {
pair->hinsi = HINSI_ID_MAX;
return;
}
pair->hinsi = get_hinsi_id(s);
if (nullp(cell_p = car(cell = cdr(cell))))
return;
pair->bunrui = get_bunrui_id(s_atom(cell_p), pair->hinsi);
#endif
if (nullp(cell_p = car(cell = cdr(cell))))
return;
pair->type = get_type_id(s_atom(cell_p));
if (nullp(cell_p = car(cell = cdr(cell))))
return;
if (strcmp(s = s_atom(cell_p), "*"))
pair->form = cha_strdup(s);
if (nullp(cell_p = car(cell = cdr(cell))))
return;
pair->goi = cha_strdup(s_atom(cell_p));
}
/***********************************************************************
* match_pair1
***********************************************************************/
static int match_pair1(pair1, pair2)
rensetu_pair_t *pair1, *pair2;
{
if (pair1->hinsi == pair2->hinsi &&
#ifndef VGRAM
pair1->bunrui == pair2->bunrui &&
#endif
pair1->type == pair2->type &&
(!pair2->form || pair1->form == pair2->form) &&
!strcmp(pair1->goi, pair2->goi))
return 1;
return 0;
}
/***********************************************************************
* match_pair2
***********************************************************************/
static int match_pair2(pair, tbl)
rensetu_pair2_t *pair;
rensetu_pair_t *tbl;
{
/* ʸƬ, ʸ */
#ifdef VGRAM
if (pair->hinsi == NULL)
return tbl->hinsi == 0;
#else
if (pair->hinsi == HINSI_ID_MAX)
return tbl->hinsi == 0;
#endif
if (
#ifdef VGRAM
match_nhinsi(pair->hinsi, (int)tbl->hinsi) &&
#else
(!pair->hinsi || pair->hinsi == tbl->hinsi) &&
(!pair->bunrui || pair->bunrui == tbl->bunrui) &&
#endif
(!pair->type || pair->type == tbl->type) &&
(!pair->form ||
(tbl->form && !strcmp(pair->form, Form[tbl->type][tbl->form].name))) &&
(!pair->goi || (tbl->goi && !strcmp(pair->goi, tbl->goi))))
return 1;
return 0;
}
/***********************************************************************
* make_rensetu_tbl1 - register hinsi with goi
***********************************************************************/
void make_rensetu_tbl1(cell1, cnt)
cell_t *cell1;
int *cnt;
{
int i;
rensetu_pair_t r_pair;
cell_t *cell11;
for (; !nullp(cell11 = car(cell1)); cell1 = cdr(cell1)) {
#ifdef VGRAM
if (nullp(car(cdr(cdr(cdr(cell11))))))
continue;
#else
if (nullp(car(cdr(cdr(cdr(cdr(cell11)))))))
continue;
#endif
get_pair1(cell11, &r_pair);
for (i = 1; i < *cnt; i++)
if (match_pair1(&rensetu_tbl[i], &r_pair))
break;
if (i < *cnt)
continue;
if (r_pair.type) {
for (i = 1; Form[r_pair.type][i].name != NULL; i++) {
rensetu_tbl[*cnt].hinsi = r_pair.hinsi;
#ifndef VGRAM
rensetu_tbl[*cnt].bunrui = r_pair.bunrui;
#endif
rensetu_tbl[*cnt].type = r_pair.type;
rensetu_tbl[*cnt].form = i;
rensetu_tbl[*cnt].goi = r_pair.goi;
if (++*cnt >= REN_TBL_MAX)
cha_exit(1, "not enough size for table");
}
} else {
rensetu_tbl[*cnt].hinsi = r_pair.hinsi;
#ifndef VGRAM
rensetu_tbl[*cnt].bunrui = r_pair.bunrui;
#endif
rensetu_tbl[*cnt].type = r_pair.type;
rensetu_tbl[*cnt].form = r_pair.form;
rensetu_tbl[*cnt].goi = r_pair.goi;
if (++*cnt >= REN_TBL_MAX)
cha_exit(1, "not enough size for table");
}
}
}
/***********************************************************************
* make_rensetu_tbl2 - register hinsi
***********************************************************************/
static void make_rensetu_tbl2(hinsi, bunrui, cnt)
int hinsi, bunrui, *cnt;
{
int i, j;
if (
#ifdef VGRAM
Hinsi[hinsi].kt == 1
#else
Class[hinsi][bunrui].kt
#endif
) {
/* Ѥ */
for (i = 0; kankei_tbl[i].hinsi != HINSI_ID_MAX; i++) {
if (kankei_tbl[i].hinsi == hinsi
#ifndef VGRAM
&& kankei_tbl[i].bunrui == bunrui
#endif
) {
for (j = 1; Form[kankei_tbl[i].type][j].name != NULL; j++) {
rensetu_tbl[*cnt].hinsi = hinsi;
#ifndef VGRAM
rensetu_tbl[*cnt].bunrui = bunrui;
#endif
rensetu_tbl[*cnt].type = kankei_tbl[i].type;
rensetu_tbl[*cnt].form = j;
rensetu_tbl[*cnt].goi = NULL;
if (++*cnt >= REN_TBL_MAX)
cha_exit(1, "not enough size for table");
}
}
}
#ifdef KOCHA2
} else if (Hinsi[hinsi].kt == 2) {
extern char *Gomatsu[];
for (i = 0; Gomatsu[i]; i++, (*cnt)++) {
rensetu_tbl[*cnt].hinsi = hinsi;
rensetu_tbl[*cnt].type = get_type_id(Gomatsu[i]);
rensetu_tbl[*cnt].form = 0;
rensetu_tbl[*cnt].goi = NULL;
}
if (*cnt >= REN_TBL_MAX)
cha_exit(1, "not enough size for table");
#endif
} else {
/* Ѥʤ */
rensetu_tbl[*cnt].hinsi = hinsi;
#ifndef VGRAM
rensetu_tbl[*cnt].bunrui = bunrui;
#endif
rensetu_tbl[*cnt].type = 0;
rensetu_tbl[*cnt].form = 0;
rensetu_tbl[*cnt].goi = NULL;
/* bug fixed by kurohashi on 92/03/16 */
if (++*cnt >= REN_TBL_MAX)
cha_exit(1, "not enough size for table");
}
}
/***********************************************************************
* make_rensetu_tbl - register hinsi into table
***********************************************************************/
static int make_rensetu_tbl(fp)
FILE *fp;
{
cell_t *cell;
int i, j, lines;
int tbl_count = 1; /* 0 ʸƬʸ */
/* äꤷƤΤơ֥Ͽ */
for (lines = 0; !s_feof(fp); lines++) {
#ifdef VGRAM
for (cell = car(s_read(fp)); !nullp(cell); cell = cdr(cell))
make_rensetu_tbl1(car(cell), &tbl_count);
#else
cell = s_read(fp);
make_rensetu_tbl1(car(cell), &tbl_count);
make_rensetu_tbl1(car(cdr(cell)), &tbl_count);
#endif
}
/* ѤŸƥơ֥Ͽ */
#ifdef VGRAM
for (i = 1; Hinsi[i].name; i++)
/* second argument is dummy for compatibility */
make_rensetu_tbl2(i, 0, &tbl_count);
#else
for (i = 1; Class[i][0].id; i++)
for (j = 0; Class[i][j].id; j++)
make_rensetu_tbl2(i, j, &tbl_count);
#endif
tbl_num = tbl_count;
/* print for check */
fprintf(stderr, "table size: %d\n", tbl_num);
return lines;
}
/***********************************************************************
* variables and functions for rensetu_cell
***********************************************************************/
static rensetu_cell_t rensetu_cell[RENSETU_CELL_MAX];
static int cell_num;
static int new_cell1[RENSETU_CELL_MAX], new_cell2[RENSETU_CELL_MAX];
static int new_cell1_num, new_cell2_num;
static int search_rensetu_cell(tbl, prev)
int tbl, prev;
{
int i;
// if (prev < 0)
// return tbl;
for (i = 0; i < cell_num; i++)
if (rensetu_cell[i].tbl == tbl)
if (rensetu_cell[i].prev == prev)
return i;
return -1;
}
/* c2 c1 suffix ɤ */
static int match_rensetu_cell_suffix(c1, c2)
int c1, c2;
{
int n;
for (n = 0; c2 >= 0; n++) {
if (rensetu_cell[c1].tbl != rensetu_cell[c2].tbl)
return 0;
c1 = rensetu_cell[c1].prev;
c2 = rensetu_cell[c2].prev;
}
return n;
}
static void match_rensetu_cell_tbl(tbl, cells)
int tbl, *cells;
{
int *c, i;
c = cells;
*c++ = tbl;
for (i = tbl_num; i < cell_num; i++)
if (tbl == rensetu_cell[i].tbl)
*c++ = i;
*c = -1;
}
/***********************************************************************
* add_connect_rule
***********************************************************************/
static void add_connect_rule(in, prev, cost, is_last, in_cells, cur_cells)
int in, prev, cost, is_last, *in_cells, *cur_cells;
{
int cur, next, *curp, *cellp;
int suffix_len, suffix_len_max;
next = 0; /* to avoid warning */
match_rensetu_cell_tbl(rensetu_cell[prev].tbl, cur_cells);
/* cell 椫 cur() */
for (curp = cur_cells; (cur = *curp) >= 0; curp++) {
/* prev cur suffix ˤʤäƤ ok */
if (!match_rensetu_cell_suffix(cur, prev))
continue;
/* ǸʻǤʤϵ§ʤ */
if (!is_last && connect_mtr1[cur][in].cost)
continue;
suffix_len_max = 0;
/* cell 椫 next() */
for (cellp = in_cells; *cellp >= 0; cellp++) {
/* cur+in suffix ΤǤĹΤõ */
suffix_len = match_rensetu_cell_suffix(cur, rensetu_cell[*cellp].prev) + 1;
if (suffix_len_max < suffix_len) {
suffix_len_max = suffix_len;
next = *cellp;
}
}
#ifdef DEBUG
if (suffix_len_max>1) {
printf("suffix_len:%d,prev:%d,cur:%d,in:%d,next:%d,cost:%d\n",
suffix_len_max,prev,cur,in,next,cost);
}
#endif
/* §ɲ */
if (suffix_len_max) {
connect_mtr1[cur][in].next = next - in;
#ifdef VGRAM
connect_mtr1[cur][in].cost = cost < 0 ? 0 : cost + 1;
#else
connect_mtr1[cur][in].cost = cost;
#endif
}
}
}
#ifdef VGRAM
/***********************************************************************
* read_rensetu
***********************************************************************/
static void read_rensetu(fp, lines)
FILE *fp;
int lines;
{
cell_t **rule;
int *rule_len;
rensetu_pair2_t pair;
cell_t *cell, *cell1;
int rule_len_max, rlen;
int prev, in, c1, ln, linenum, linecnt;
int cost, is_last;
int *in_cells, *cur_cells;
connect_rule_t *ptr;
rule = (cell_t **)cha_malloc(sizeof(cell_t *) * lines);
rule_len = (int *)cha_malloc(sizeof(int) * lines);
/* rensetu_cell ν */
if (cell_num >= RENSETU_CELL_MAX)
cha_exit(1, "not enough size for cell");
for (cell_num = 0; cell_num < tbl_num; cell_num++) {
#if 0
rensetu_cell[cell_num] = cha_malloc_rensetu_cell();
#endif
rensetu_cell[cell_num].tbl = cell_num;
rensetu_cell[cell_num].prev = -1;
}
rule_len_max = 0;
for (ln = 0; !s_feof(fp); ln++) {
rule[ln] = s_read(fp);
#if 0
if (ln>5800)
printf("\n%d: %s\n", ln, s_tostr(rule[ln]));fflush(stdout);
#endif
/* ǤŧĤ */
rule_len[ln] = s_length(car(rule[ln]));
if (rule_len[ln] < 2)
cha_exit_file(1, "too few morphemes");
if (rule_len_max < rule_len[ln])
rule_len_max = rule_len[ln];
/* new_cell2: ʻϿ rensetu_cell */
new_cell2[0] = -1; /* ʸƬʸ */
new_cell2_num = 1;
/* §ΥΡɤ */
/* cell: ʻ췲Υꥹ */
for (cell = car(rule[ln]); !nullp(cdr(cell)); cell = cdr(cell)) {
/* new_cell2 new_cell1 ˥ԡ */
memcpy(new_cell1, new_cell2, sizeof(int) * new_cell2_num);
new_cell1_num = new_cell2_num;
new_cell2_num = 0;
/* cell1: ʻ췲 */
for (cell1 = car(cell); !nullp(cell1); cell1 = cdr(cell1)) {
int tbl;
#if 0
if (ln>5800)
printf("[ %s ]",s_tostr(car(cell1)));
#endif
/* pair: 磻ɥɤĤʻ */
get_pair2(car(cell1), &pair);
/* pair tbl(ʻ11)Фƽ */
for (tbl = 0; tbl < tbl_num; tbl++) {
if (!match_pair2(&pair, &rensetu_tbl[tbl]))
continue;
#if 0
if (ln>5800)
printf("<%d>",tbl);
#endif
/* c1, prev: 1ʻϿ줿cell */
for (c1 = 0; c1 < new_cell1_num; c1++) {
int prev = new_cell1[c1], cellno;
#if 0
if (ln>5800)
printf("{%d}",prev);
#endif
if ((cellno = search_rensetu_cell(tbl, prev)) < 0) {
cellno = cell_num;
if (++cell_num >= RENSETU_CELL_MAX)
cha_exit_file(1, "not enough size for cell");
rensetu_cell[cellno].tbl = tbl;
rensetu_cell[cellno].prev = prev;
#if 0
if ((cell_num % 100) == 0)
{ printf("ln:%d,cell:%d ", ln, cell_num); fflush(stdout); }
#endif
#ifdef DEBUG
printf("cellno:%d,tbl:%d,prev:%d\n",cellno,tbl,prev);
#endif
}
new_cell2[new_cell2_num++] = cellno;
}
}
}
}
}
fprintf(stderr, "number of states: %d\n", cell_num);
ptr = (connect_rule_t *)cha_malloc(sizeof(connect_rule_t) * cell_num * tbl_num);
memset(ptr, 0, sizeof(connect_rule_t) * cell_num * tbl_num);
connect_mtr1 = (connect_rule_t **)cha_malloc(sizeof(connect_rule_t *) * cell_num);
for (c1 = 0; c1 < cell_num; c1++)
connect_mtr1[c1] = ptr + c1 * tbl_num;
ptr = (connect_rule_t *)cha_malloc(sizeof(connect_rule_t) * cell_num * tbl_num);
connect_mtr2 = (connect_rule_t **)cha_malloc(sizeof(connect_rule_t *) * cell_num);
for (c1 = 0; c1 < cell_num; c1++)
connect_mtr2[c1] = ptr + c1 * tbl_num;
in_cells = cha_malloc(sizeof(int) * cell_num);
cur_cells = cha_malloc(sizeof(int) * cell_num);
linenum = ln;
linecnt = 0;
printf("lines: %d\n", linenum);
/* û§ν˽ */
for (rlen = 2; rlen <= rule_len_max; rlen++) {
printf(rlen == 2 ? "bi%s" : rlen == 3 ? "tri%s" : "%d%s",
"-gram: ", rlen);
for (ln = 0; ln < linenum; ln++) {
if (rule_len[ln] != rlen)
continue;
LineNoForError = LineNo = ln + 1;
#ifdef DEBUG
printf("Line: %d/%d\n",ln+1,linenum);
#endif
if ((++linecnt % 500) == 0) {
fputc('.', stdout);
if ((linecnt % 20000) == 0)
printf(" %d\n", linecnt);
fflush(stdout);
}
cell = car(cdr(rule[ln]));
cost = nullp(cell) ? DEFAULT_C_WEIGHT : atoi(s_atom(cell));
is_last = 0;
/* new_cell2: ʻϿ rensetu_cell */
new_cell2[0] = -1; /* ʸƬʸ */
new_cell2_num = 1;
/* cell: ʻ췲Υꥹ */
for (cell = car(rule[ln]); !is_last; cell = cdr(cell)) {
is_last = nullp(cdr(cell));
/* new_cell2 new_cell1 ˥ԡ */
memcpy(new_cell1, new_cell2, sizeof(int) * new_cell2_num);
new_cell1_num = new_cell2_num;
new_cell2_num = 0;
/* cell1: ʻ췲 */
for (cell1 = car(cell); !nullp(cell1); cell1 = cdr(cell1)) {
/* pair: 磻ɥɤĤʻ */
get_pair2(car(cell1), &pair);
/* pair in(ʻ11)Фƽ */
for (in = 0; in < tbl_num; in++) {
if (!match_pair2(&pair, &rensetu_tbl[in]))
continue;
match_rensetu_cell_tbl(in, in_cells);
/* c1, prev: 1ʻϿ줿cell */
for (c1 = 0; c1 < new_cell1_num; c1++) {
prev = new_cell1[c1];
if (!is_last) {
int cellno = search_rensetu_cell(in, prev);
new_cell2[new_cell2_num++] = cellno;
}
/* §ɲ */
add_connect_rule(in, prev, cost, is_last,
in_cells, cur_cells);
}
}
}
}
}
printf(" %d\n", linecnt);
}
}
#else /* !VGRAM */
/* modified by T.Utsuro for weight of rensetu matrix */
static void fill_matrix(pair1, pair2, c_weight)
rensetu_pair2_t *pair1, *pair2;
int c_weight;
{
int i, j;
int *tmp_tbl;
int flag = 0;
tmp_tbl = (int *)cha_malloc(sizeof(int) * tbl_num);
for (j = 0; j < tbl_num; j++) {
if (match_pair2(pair2, &rensetu_tbl[j]))
tmp_tbl[j] = 1;
else
tmp_tbl[j] = 0;
}
for (i = 0; i < tbl_num; i++) {
if (!match_pair2(pair1, &rensetu_tbl[i]))
continue;
for (j = 0; j < tbl_num; j++) {
if (tmp_tbl[j]) { /* ˤ롼νŤߤͥ */
#ifdef KOCHA
if (c_weight >= 100)
rensetu_mtr1[i][j] =
rensetu_mtr1[i][j] % 100 + c_weight;
else
rensetu_mtr1[i][j] =
rensetu_mtr1[i][j] - rensetu_mtr1[i][j] % 100 + c_weight;
#ifdef _KOCHA_DEBUG
printf("[c:%d,rm:%d]\n",c_weight,rensetu_mtr1[i][j]);
#endif
#else
rensetu_mtr1[i][j] = c_weight;
#endif
flag = 1;
}
}
}
free(tmp_tbl);
if (!flag) {
cha_exit_file(-1, "invalid katsuyou form `%s' or `%s'",
pair1->form ? pair1->form : "(null)",
pair2->form ? pair2->form : "(null)");
}
}
/***********************************************************************
* read_rensetu
***********************************************************************/
static void read_rensetu(fp, lines)
FILE *fp;
int lines; /* dummy for compatibility with VGRAM */
{
int i, j;
cell_t *cell, *cell1, *cell11, *cell2, *cell22, *cell2_stock;
rensetu_pair2_t pair1, pair2;
int c_weight;
rensetu_mtr_t *ptr;
ptr = (rensetu_mtr_t *)cha_malloc(sizeof(rensetu_mtr_t) * tbl_num * tbl_num);
memset(ptr, 0, sizeof(rensetu_mtr_t) * tbl_num * tbl_num);
rensetu_mtr1 = (rensetu_mtr_t **)cha_malloc(sizeof(rensetu_mtr_t *) * tbl_num);
for (i = 0; i < tbl_num; i++)
rensetu_mtr1[i] = ptr + i * tbl_num;
ptr = (rensetu_mtr_t *)cha_malloc(sizeof(rensetu_mtr_t) * tbl_num * tbl_num);
rensetu_mtr2 = (rensetu_mtr_t **)cha_malloc(sizeof(rensetu_mtr_t *) * tbl_num);
for (i = 0; i < tbl_num; i++)
rensetu_mtr2[i] = ptr + i * tbl_num;
while (! s_feof(fp)) {
cell = s_read(fp);
cell1 = car(cell);
cell2 = car(cdr(cell));
/* added by T.Utsuro for weight of rensetu matrix */
if (nullp(cdr(cdr(cell))))
c_weight = DEFAULT_C_WEIGHT;
else
c_weight = atoi(s_atom(car(cdr(cdr(cell)))));
cell2_stock = cell2;
while (!nullp(cell11 = car(cell1))) {
get_pair2(cell11, &pair1);
cell2 = cell2_stock;
while (!nullp(cell22 = car(cell2))) {
get_pair2(cell22, &pair2);
fill_matrix(&pair1, &pair2, c_weight);
cell2 = cdr(cell2);
}
cell1 = cdr(cell1);
}
}
}
#endif /* !VGRAM */
static int compare_vector1(k, j, num)
int k, j, num;
{
int i;
#ifdef VGRAM
for (i = 0; i < num; i++)
if (connect_mtr2[i][k].next != connect_mtr1[i][j].next ||
connect_mtr2[i][k].cost != connect_mtr1[i][j].cost)
return 0;
#else
for (i = 0; i < num; i++)
if (rensetu_mtr2[i][k] != rensetu_mtr1[i][j])
return 0;
#endif
return 1;
}
static void copy_vector1(j, j_n, num)
int j, j_n, num;
{
int i;
#ifdef VGRAM
for (i = 0; i < num; i++) {
connect_mtr2[i][j_n].next = connect_mtr1[i][j].next;
connect_mtr2[i][j_n].cost = connect_mtr1[i][j].cost;
}
#else
for (i = 0; i < num; i++)
rensetu_mtr2[i][j_n] = rensetu_mtr1[i][j];
#endif
}
static int compare_vector2(k, i, num)
int k, i, num;
{
int j;
#ifdef VGRAM
for (j = 0; j < num; j++)
if (connect_mtr2[i][j].next != connect_mtr1[k][j].next ||
connect_mtr2[i][j].cost != connect_mtr1[k][j].cost)
return 0;
#else
for (j = 0; j < num; j++)
if (rensetu_mtr2[i][j] != rensetu_mtr1[k][j])
return 0;
#endif
return 1;
}
static void copy_vector2(i, i_n, num)
int i, i_n, num;
{
int j;
#ifdef VGRAM
for (j = 0; j < num; j++) {
connect_mtr1[i_n][j].next = connect_mtr2[i][j].next;
connect_mtr1[i_n][j].cost = connect_mtr2[i][j].cost;
}
#else
for (j = 0; j < num; j++)
rensetu_mtr1[i_n][j] = rensetu_mtr2[i][j];
#endif
}
/***********************************************************************
* condense_matrix
***********************************************************************/
static void condense_matrix()
{
int i, j, k;
int i_n = 0;
int j_n = 0;
#ifndef VGRAM
cell_num = tbl_num;
#endif
fprintf(stderr, "matrix size: %dx%d", cell_num, tbl_num);
for (i = 0; i < cell_num; i++) {
rensetu_tbl[i].i_pos = i;
rensetu_tbl[i].j_pos = i;
}
for (j = 0; j < tbl_num; j++) {
for (k = 0; k < j_n; k++) {
if (compare_vector1(k, j, cell_num)) {
rensetu_tbl[j].j_pos = k;
break;
}
}
if (rensetu_tbl[j].j_pos == j) {
copy_vector1(j, j_n, cell_num);
rensetu_tbl[j].j_pos = j_n++;
}
}
for (i = 0; i < cell_num; i++) {
for (k = 0; k < i_n; k++) {
if (compare_vector2(k, i, j_n)) {
rensetu_tbl[i].i_pos = k;
break;
}
}
if (rensetu_tbl[i].i_pos == i) {
copy_vector2(i, i_n, j_n);
rensetu_tbl[i].i_pos = i_n++;
}
}
i_num = i_n;
j_num = j_n;
/* print for check */
fprintf(stderr, " -> %dx%d\n", i_num, j_num);
}
/***********************************************************************
* write_trble, write_matrix
***********************************************************************/
static void write_table()
{
FILE *fp;
rensetu_pair_t *tbl;
int i;
fp = cha_fopen(TABLE_FILE, "w", 1);
fprintf(fp, "%d\n", cell_num);
for (i = 0, tbl = &rensetu_tbl[0]; i < tbl_num; i++, tbl++) {
#ifdef VGRAM
/* comment */
fprintf(fp, "%s %s %s %s\n",
Hinsi[tbl->hinsi].name ?
Hinsi[tbl->hinsi].name : "(null)",
tbl->type ? Type[tbl->type].name : "",
tbl->form ? Form[tbl->type][tbl->form].name : "",
tbl->goi ? tbl->goi : "");
/* data */
fprintf(fp, "%d %d %d %d %d %s\n",
tbl->i_pos, tbl->j_pos, tbl->hinsi,
tbl->type, tbl->form,
tbl->goi ? tbl->goi : "*");
#else
/* comment */
fprintf(fp, "%s %s %s %s\n",
Class[tbl->hinsi][tbl->bunrui].id ?
Class[tbl->hinsi][tbl->bunrui].id : "(null) ",
tbl->type ? Type[tbl->type].name : "",
tbl->form ? Form[tbl->type][tbl->form].name : "",
tbl->goi ? tbl->goi : "");
/* data */
fprintf(fp, "%d %d %d %d %d %d %s\n",
tbl->i_pos, tbl->j_pos, tbl->hinsi, tbl->bunrui,
tbl->type, tbl->form,
tbl->goi ? tbl->goi : "*");
#endif
}
#ifdef VGRAM
for (; i < cell_num; i++, tbl++)
fprintf(fp, ";\n%d -1 0 0 0 *\n", tbl->i_pos);
#endif
fclose(fp);
}
static void write_matrix()
{
FILE *fp;
int i, j, nzero;
fp = cha_fopen(MATRIX_FILE, "w", 1);
fprintf(fp, "%d %d\n", i_num, j_num);
nzero = 0;
for (i = 0; i < i_num; i++) {
for (j = 0; j < j_num; j++) {
#ifdef VGRAM
if (!connect_mtr1[i][j].next && !connect_mtr1[i][j].cost)
nzero++;
else {
if (nzero) {
fprintf(fp, "o%d ", nzero);
nzero = 0;
}
fprintf(fp, "%d,%d ", (int)connect_mtr1[i][j].next,
(int)connect_mtr1[i][j].cost);
}
#else
fprintf(fp, "%d ", (int)rensetu_mtr1[i][j]);
#endif
}
#ifdef VGRAM
if (nzero) {
fprintf(fp, "o%d ", nzero);
nzero = 0;
}
#endif
fprintf(fp, "\n");
}
fclose(fp);
}
/***********************************************************************
* main
***********************************************************************/
int main(argc, argv)
int argc;
char **argv;
{
FILE *fpc;
int lines;
if (argv[1] && !strcmp(argv[1], "-i")) {
#ifdef VGRAM
fputs("cha\n", stdout);
#else
fputs("juman\n", stdout);
#endif
exit(0);
}
set_progpath(argv[0]);
/* .chasenrc ɤ߹ɬפʤ */
/* ʸˡѡطե */
read_grammar(stderr, 0, 0);
read_katuyou(stderr, 0);
read_kankei();
/* Ϣܵ§եΥץ */
#ifdef VGRAM
fpc = cha_fopen(CONNTMP_FILE, "r", 1);
#else
fpc = cha_fopen2(CONNTMP_FILE, JM_CONNTMP_FILE, "r", 1);
#endif
/* Ϣܵ§եν */
fprintf(stderr, "parsing %s\n", CONNTMP_FILE);
set_skip_char('#');
lines = make_rensetu_tbl(fpc);
rewind(fpc);
LineNo = 0;
read_rensetu(fpc, lines);
fclose(fpc);
/* Ϣܹΰ */
condense_matrix();
write_table();
write_matrix();
return 0;
}
|