0
|
1 /*
|
|
2 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
3 * This file is part of FreeWnn.
|
|
4 *
|
|
5 * Copyright Kyoto University Research Institute for Mathematical Sciences
|
|
6 * 1987, 1988, 1989, 1990, 1991, 1992
|
|
7 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
8 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
|
|
9 * Copyright FreeWnn Project 1999, 2000, 2002, 2003
|
|
10 *
|
|
11 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
|
|
12 *
|
|
13 * This program is free software; you can redistribute it and/or modify
|
|
14 * it under the terms of the GNU General Public License as published by
|
|
15 * the Free Software Foundation; either version 2 of the License, or
|
|
16 * (at your option) any later version.
|
|
17 *
|
|
18 * This program is distributed in the hope that it will be useful,
|
|
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 * GNU General Public License for more details.
|
|
22 *
|
|
23 * You should have received a copy of the GNU General Public License
|
|
24 * along with this program; if not, write to the Free Software
|
|
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
26 */
|
|
27
|
|
28 #ifdef HAVE_CONFIG_H
|
|
29 # include <config.h>
|
|
30 #endif
|
|
31
|
|
32 #include <stdio.h>
|
|
33 #if STDC_HEADERS
|
|
34 # include <stdlib.h>
|
|
35 #else
|
|
36 # if HAVE_MALLOC_H
|
|
37 # include <malloc.h>
|
|
38 # endif
|
|
39 #endif /* STDC_HEADERS */
|
|
40
|
|
41 #include "commonhd.h"
|
|
42 #include "de_header.h"
|
|
43 #include "jdata.h"
|
|
44
|
|
45 #ifdef CONVERT_by_STROKE
|
|
46 /******* Create B_dic index *************************************/
|
|
47 static int delete_b_node (struct JT *jt, w_char *yomi, int level, int p_node);
|
|
48 static int creat_b_node (struct JT *jt, w_char *yomi, int level, int p_node);
|
|
49 static int bnode_alloc (struct JT *jt);
|
|
50 static int bnode_free (struct JT *jt, int k);
|
|
51
|
|
52 static int free_bnode = -1; /* Initially no free b_node */
|
|
53 static int b_cnt; /* Current member of b_node in b_nodes area */
|
|
54 /* Note, b_cnt < jt->bufsize_bnode */
|
|
55
|
|
56 /*----------------------------------------------------------------+
|
|
57 SYNOPSYS: Create b_index for b_dic.
|
|
58 RETURN VALUE: success: b_cnt (current b_node); failure: -1
|
|
59 +----------------------------------------------------------------*/
|
|
60 int
|
|
61 create_b_index (struct JT *jt)
|
|
62 {
|
|
63 int i;
|
|
64 int serial;
|
|
65 w_char *yomi; /* The inputed character string */
|
|
66
|
|
67 /* Make Space of b_nodes */
|
|
68 jt->bind = (struct b_node *) malloc (2 * jt->bufsize_ri1[0] * sizeof (struct b_node));
|
|
69
|
|
70 /* If the bind size is determined in atod, we use the
|
|
71 following statement instead of the above
|
|
72 */
|
|
73 if (jt->bind == NULL)
|
|
74 {
|
|
75 log_err ("error in creating head of b_index.");
|
|
76 return (-1);
|
|
77 }
|
|
78 /* Set the first one be a dummy b_node */
|
|
79 jt->bind[0].pter_next = -1;
|
|
80 jt->bind[0].pter_son = -1;
|
|
81 jt->bind[0].pter = -1;
|
|
82
|
|
83 /* Delete the following line when the bufsize of bnode is set at atod step */
|
|
84 jt->bufsize_bnode = 2 * jt->bufsize_ri1[0];
|
|
85
|
|
86 b_cnt = 0;
|
|
87
|
|
88 /** For each tuple in ri1[0] create b_nodes */
|
|
89 for (i = 0; i < jt->maxri1[0]; i++)
|
|
90 {
|
|
91 serial = (jt->ri1[0] + i)->pter;
|
|
92 yomi = KANJI_str (jt->ri2[serial].kanjipter + jt->kanji, 0);
|
|
93 b_index_add (jt, yomi, serial);
|
|
94 }
|
|
95 return (b_cnt);
|
|
96 }
|
|
97
|
|
98 /*----------------------------------------------------------------+
|
|
99 SYNOPSYS: Create a b_node for each character in a tuple.
|
|
100 RETURN VALUE: success: 0; failure: -1
|
|
101 +----------------------------------------------------------------*/
|
|
102 int
|
|
103 b_index_add (struct JT *jt, w_char *yomi, int serial)
|
|
104 {
|
|
105 int k;
|
|
106 int p_node; /* Current b_node in No. */
|
|
107
|
|
108 p_node = 0;
|
|
109 for (k = 0; k < Strlen (yomi); k++)
|
|
110 {
|
|
111 if ((p_node = creat_b_node (jt, yomi, k, p_node)) == -1)
|
|
112 {
|
|
113 log_err ("error in creating b_index.");
|
|
114 return (-1);
|
|
115 }
|
|
116 }
|
|
117 jt->bind[p_node].pter = serial;
|
|
118 return (0);
|
|
119 }
|
|
120
|
|
121 /*----------------------------------------------------------------+
|
|
122 SYNOPSYS:
|
|
123 +----------------------------------------------------------------*/
|
|
124 void
|
|
125 b_index_delete (struct JT *jt, int serial)
|
|
126 {
|
|
127 w_char *yomi;
|
|
128 yomi = KANJI_str (jt->ri2[serial].kanjipter + jt->kanji, 0);
|
|
129 delete_b_node (jt, yomi, 0, 0);
|
|
130 }
|
|
131
|
|
132 /*----------------------------------------------------------------+
|
|
133 SYNOPSYS:
|
|
134 RETURN VALUE: 0 Having no son
|
|
135 1 Having some sons
|
|
136 +----------------------------------------------------------------*/
|
|
137 static int
|
|
138 delete_b_node (struct JT *jt, w_char *yomi, int level, int p_node)
|
|
139 {
|
|
140 int tmp_node;
|
|
141 int buf_node1, buf_node2;
|
|
142 w_char *yo_kanji = NULL;
|
|
143
|
|
144 if (p_node == -1)
|
|
145 return (0);
|
|
146
|
|
147 buf_node1 = jt->bind[p_node].pter_son; /*Initialize two tmp nodes */
|
|
148 buf_node2 = jt->bind[p_node].pter_son;
|
|
149
|
|
150 /* search if the node exists already */
|
|
151 while (buf_node2 != -1)
|
|
152 {
|
|
153 tmp_node = buf_node2;
|
|
154 while (jt->bind[tmp_node].pter == -1)
|
|
155 {
|
|
156 tmp_node = jt->bind[tmp_node].pter_son;
|
|
157 }
|
|
158 yo_kanji = KANJI_str (jt->ri2[jt->bind[tmp_node].pter].kanjipter + jt->kanji, 0);
|
|
159 if (yomi[level] > yo_kanji[level])
|
|
160 {
|
|
161 buf_node1 = buf_node2;
|
|
162 buf_node2 = jt->bind[buf_node2].pter_next;
|
|
163 }
|
|
164 else
|
|
165 break;
|
|
166 }
|
|
167 if (yo_kanji == NULL || yomi[level] != yo_kanji[level])
|
|
168 {
|
|
169 /* Error case */
|
|
170 log_err ("error on b_index.");
|
|
171 return (-1);
|
|
172 }
|
|
173
|
|
174 if (level == (Strlen (yomi) - 1))
|
|
175 {
|
|
176 jt->bind[buf_node2].pter = -1; /* HERE HERE */
|
|
177 if (jt->bind[buf_node2].pter_son != -1) /* having son */
|
|
178 return (1);
|
|
179 }
|
|
180 if (level < (Strlen (yomi) - 1))
|
|
181 {
|
|
182 if ((delete_b_node (jt, yomi, level + 1, buf_node2) != 0) || (jt->bind[buf_node2].pter != -1))
|
|
183 return (1);
|
|
184 }
|
|
185 /* 0 case: (bnode_free) below */
|
|
186 if (buf_node1 == buf_node2)
|
|
187 { /* only one b_node in this level */
|
|
188 bnode_free (jt, buf_node2);
|
|
189 return (0);
|
|
190 }
|
|
191 else
|
|
192 {
|
|
193 jt->bind[buf_node1].pter_next = jt->bind[buf_node2].pter_next;
|
|
194 bnode_free (jt, buf_node2);
|
|
195 return (1);
|
|
196 }
|
|
197 }
|
|
198
|
|
199 /*----------------------------------------------------------------+
|
|
200 SYNOPSIS: Create a son of the parent node p_node, when it does
|
|
201 not exist.
|
|
202 PARAMETERS: jt: Header of the current dic.
|
|
203 yomi: Cureent character in this level.
|
|
204 level: Level number.
|
|
205 p_node: cureent b_node.
|
|
206 RETURN VALUE: new or existent node number whether created or existed.
|
|
207 failure: -1
|
|
208 +----------------------------------------------------------------*/
|
|
209 static int
|
|
210 creat_b_node (struct JT *jt, w_char *yomi, int level, int p_node)
|
|
211 {
|
|
212 int new_node;
|
|
213 int buf_node1, buf_node2;
|
|
214 int tmp_node;
|
|
215 w_char *yo_kanji = NULL;
|
|
216
|
|
217 buf_node1 = jt->bind[p_node].pter_son; /*Initialize two tmp nodes */
|
|
218 buf_node2 = jt->bind[p_node].pter_son;
|
|
219
|
|
220 /* search if the node exists already */
|
|
221 while (buf_node2 != -1)
|
|
222 {
|
|
223 tmp_node = buf_node2;
|
|
224 while (jt->bind[tmp_node].pter == -1)
|
|
225 {
|
|
226 tmp_node = jt->bind[tmp_node].pter_son;
|
|
227 }
|
|
228 yo_kanji = KANJI_str (jt->ri2[jt->bind[tmp_node].pter].kanjipter + jt->kanji, 0);
|
|
229
|
|
230 if (yomi[level] > yo_kanji[level])
|
|
231 {
|
|
232 buf_node1 = buf_node2;
|
|
233 buf_node2 = jt->bind[buf_node2].pter_next;
|
|
234 }
|
|
235 else
|
|
236 break;
|
|
237 }
|
|
238
|
|
239 if (buf_node1 == -1)
|
|
240 { /* the cur is the first */
|
|
241 if (buf_node2 == -1)
|
|
242 {
|
|
243 if ((new_node = bnode_alloc (jt)) == -1)
|
|
244 {
|
|
245 log_err ("error in reallocing area of b_index.");
|
|
246 return (-1);
|
|
247 }
|
|
248 jt->bind[p_node].pter_son = new_node;
|
|
249
|
|
250 jt->bind[new_node].pter_next = -1;
|
|
251 jt->bind[new_node].pter_son = -1;
|
|
252 jt->bind[new_node].pter = -1;
|
|
253 }
|
|
254 else
|
|
255 return (-1); /* Error case. Impossible case */
|
|
256 return (new_node);
|
|
257
|
|
258 }
|
|
259 else if (buf_node2 == -1)
|
|
260 { /* insert to last */
|
|
261 new_node = bnode_alloc (jt);
|
|
262 jt->bind[buf_node1].pter_next = new_node;
|
|
263
|
|
264 jt->bind[new_node].pter_next = -1;
|
|
265 jt->bind[new_node].pter_son = -1;
|
|
266 jt->bind[new_node].pter = -1;
|
|
267 return (new_node);
|
|
268
|
|
269 }
|
|
270 else if (yo_kanji == NULL)
|
|
271 {
|
|
272 return (-1);
|
|
273 }
|
|
274 else if (yomi[level] == yo_kanji[level]) /* exist already */
|
|
275 return (buf_node2);
|
|
276
|
|
277 /* insert in before tnp_node2 */
|
|
278 else if (yomi[level] < yo_kanji[level])
|
|
279 {
|
|
280 new_node = bnode_alloc (jt);
|
|
281 jt->bind[new_node].pter_next = buf_node2;
|
|
282 jt->bind[new_node].pter_son = -1;
|
|
283 jt->bind[new_node].pter = -1;
|
|
284
|
|
285
|
|
286 if (buf_node1 == buf_node2) /*insert to first */
|
|
287 jt->bind[p_node].pter_son = new_node;
|
|
288 else
|
|
289 jt->bind[buf_node1].pter_next = new_node; /*to middle */
|
|
290 return (new_node);
|
|
291 }
|
|
292 else
|
|
293 return (-1);
|
|
294 }
|
|
295
|
|
296
|
|
297 /*----------------------------------------------------------------+
|
|
298 SYNOPSYS: Get a b_node from free_bnode list if there is any.
|
|
299 Otherwise get a b_node sequencially by doing b_cnt++.
|
|
300 When b_cnt is greater than bufsize of b_node, rallocation
|
|
301 will be performed.
|
|
302 RETURN VALUE:
|
|
303 +----------------------------------------------------------------*/
|
|
304 static int
|
|
305 bnode_alloc (struct JT *jt)
|
|
306 {
|
|
307 int i;
|
|
308
|
|
309 if (free_bnode != -1)
|
|
310 { /* Use free b_nodes */
|
|
311 i = free_bnode;
|
|
312 free_bnode = jt->bind[free_bnode].pter_next;
|
|
313 return (i);
|
|
314 }
|
|
315 if (b_cnt++ >= jt->bufsize_bnode) /* Use new b_nodes */
|
1
|
316 if (rd_realloc_bind (jt) == 0) /* realloc jt->bind */
|
0
|
317 return (-1);
|
|
318 return (jt->bufsize_bnode = b_cnt); /* Not re-alloc */
|
|
319 }
|
|
320
|
|
321 /*----------------------------------------------------------------+
|
|
322 SYNOPSIS: Free b_node[k] from jt.
|
|
323 +----------------------------------------------------------------*/
|
|
324 static int
|
|
325 bnode_free (struct JT *jt, int k)
|
|
326 {
|
|
327 if (k <= 0 || k > jt->max_bnode)
|
|
328 {
|
|
329 log_err ("error: bnode_free()");
|
|
330 return (-1);
|
|
331 }
|
|
332 jt->bind[k].pter = -1; /* Initial this node */
|
|
333 jt->bind[k].pter_son = -1;
|
|
334 jt->bind[k].pter_next = -1;
|
|
335
|
|
336 if (k < jt->max_bnode)
|
|
337 {
|
|
338 jt->bind[k].pter_next = free_bnode;
|
|
339 free_bnode = k;
|
|
340 }
|
|
341 else
|
|
342 jt->max_bnode = --b_cnt; /* case of k==b_cnt */
|
|
343 return (0);
|
|
344 }
|
|
345 #endif /* CONVERT_by_STROKE */
|