1 /*
2  *  $Id: atorev.c,v 1.7 2004/07/19 18:24:26 hiroo Exp $
3  */
4 
5 /*
6  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7  * This file is part of FreeWnn.
8  *
9  * Copyright Kyoto University Research Institute for Mathematical Sciences
10  *                 1987, 1988, 1989, 1990, 1991, 1992
11  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
12  * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
13  * Copyright FreeWnn Project 1999, 2000, 2002
14  *
15  * Maintainer:  FreeWnn Project   <freewnn@tomo.gr.jp>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30  */
31 
32 /*
33  * UJIS ���������Ѵ���ǽ�������Ѵ�����ץ���ࡣ
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39 
40 #include <stdio.h>
41 #include <ctype.h>
42 #if STDC_HEADERS
43 #  include <stdlib.h>
44 #else
45 #  if HAVE_MALLOC_H
46 #    include <malloc.h>
47 #  endif
48 #endif /* STDC_HEADERS */
49 
50 #include "commonhd.h"
51 #include "jslib.h"
52 #include "jh.h"
53 #include "jdata.h"
54 #include "wnn_string.h"
55 
56 extern int kanjicount;
57 extern struct JT jt;
58 
59 static void r_alloc (), make_ri2 (), make_ri1 (), set_rpter1 ();
60 extern int sort_func_je ();
61 extern void sort_if_not_sorted (), uniq_je (), output_dic_data (), sort_kanji (), upd_kanjicount ();
62 
63 struct je **ptmp;
64 
65 void
create_rev_dict(void)66 create_rev_dict (void)
67 {
68   r_alloc ();
69   sort_if_not_sorted ();
70   uniq_je (sort_func_je);
71   output_dic_data ();
72   make_ri2 ();
73   make_ri1 (D_YOMI);
74   set_rpter1 (D_YOMI);
75   sort_kanji ();
76   make_ri1 (D_KANJI);
77   set_rpter1 (D_KANJI);
78 }
79 
80 static void
r_alloc(void)81 r_alloc (void)
82 {
83   if ((jt.ri2 = (struct rind2 *) malloc ((jt.maxserial) * sizeof (struct rind2))) == NULL
84       || (jt.ri1[D_YOMI] = (struct rind1 *) malloc (jt.maxserial * sizeof (struct rind1))) == NULL
85       || (jt.ri1[D_KANJI] = (struct rind1 *) malloc (jt.maxserial * sizeof (struct rind1))) == NULL
86       || (ptmp = (struct je **) malloc (jt.maxserial * sizeof (struct je *))) == NULL)
87     {
88       fprintf (stderr, "Malloc Failed\n");
89       exit (1);
90     }
91 }
92 
93 static void
make_ri2(void)94 make_ri2 (void)
95 {
96   int s;
97 
98   for (s = 0; s < jt.maxserial; s++)
99     {
100       jeary[s]->serial = s;
101       jt.ri2[s].kanjipter = kanjicount;
102       upd_kanjicount (s);
103     }
104 }
105 
106 static void
make_ri1(int which)107 make_ri1 (int which)
108 {
109   w_char *yomi;
110   w_char dummy = 0;             /* 2 byte yomi */
111   w_char *pyomi;                /* maeno tangono yomi */
112   int s, t;
113 
114   yomi = &dummy;
115 
116   for (t = 0, s = 0; s < jt.maxserial; s++)
117     {
118       pyomi = yomi;
119       yomi = (which == D_YOMI) ? jeary[s]->yomi : jeary[s]->kan;
120 
121       if (wnn_Strcmp (yomi, pyomi))
122         {
123           (jt.ri1[which])[t].pter = jeary[s]->serial;
124           if (s)
125             (jt.ri2[jeary[s - 1]->serial].next)[which] = RD_ENDPTR;
126           ptmp[t] = jeary[s];
127           t++;
128         }
129       else
130         {
131           if (s)
132             (jt.ri2[jeary[s - 1]->serial].next)[which] = jeary[s]->serial;
133         }
134     }
135   if (s > 0)
136     {
137       (jt.ri2[jeary[s - 1]->serial].next)[which] = RD_ENDPTR;
138     }
139   jt.maxri1[which] = t;
140 }
141 
142 static void
set_rpter1(int which)143 set_rpter1 (int which)
144 {
145   int k;
146   int len;
147   w_char *oyomi, *nyomi;
148   /* May be a little slow, but simple! */
149   int lasts[LENGTHYOMI];        /* pter_to */
150 
151   for (k = 0; k < LENGTHYOMI; k++)
152     {
153       lasts[k] = -1;
154     }
155 
156   for (k = 0; k < jt.maxri1[which]; k++)
157     {
158       nyomi = (which == D_YOMI) ? ptmp[k]->yomi : ptmp[k]->kan;
159       len = wnn_Strlen (nyomi);
160       lasts[len] = k;
161       for (len--; len; len--)
162         {
163           if (lasts[len] >= 0)
164             {
165               oyomi = (which == D_YOMI) ? ptmp[lasts[len]]->yomi : ptmp[lasts[len]]->kan;
166               if (wnn_Substr (oyomi, nyomi))
167                 {
168                   (jt.ri1[which])[k].pter1 = lasts[len];
169                   break;
170                 }
171             }
172         }
173       if (len == 0)
174         (jt.ri1[which])[k].pter1 = RD_ENDPTR;
175     }
176 }
177 
178 void
output_ri(FILE * ofpter)179 output_ri (FILE* ofpter)
180 {
181   fwrite (jt.ri1[D_YOMI], sizeof (struct rind1), jt.maxri1[D_YOMI], ofpter);
182   fwrite (jt.ri1[D_KANJI], sizeof (struct rind1), jt.maxri1[D_KANJI], ofpter);
183   fwrite (jt.ri2, sizeof (struct rind2), jt.maxri2, ofpter);
184 }
185