OSDN Git Service

initial import
[chasen-legacy/chasen.git] / lib / init.c
1 /*
2  * Copyright (c) 2003 Nara Institute of Science and Technology
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name Nara Institute of Science and Technology may not be used to
15  *    endorse or promote products derived from this software without
16  *    specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Nara Institute of Science and Technology 
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
21  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE Nara Institute
22  * of Science and Technology BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $Id: init.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
31  */
32
33 #include "chalib.h"
34 #include "dartsdic.h"
35 #include "literal.h"
36 #include "tokenizer.h"
37
38 /*
39  * .chasenrc default values 
40  */
41 #define POS_COST_DEFAULT        1
42 #define RENSETSU_WEIGHT_DEFAULT 1
43 #define KEITAISO_WEIGHT_DEFAULT 1
44 #define COST_WIDTH_DEFAULT      0
45 #define UNDEF_WORD_DEFAULT      10000
46
47 int Cha_con_cost_weight = RENSETSU_WEIGHT_DEFAULT * MRPH_DEFAULT_WEIGHT;
48 int Cha_con_cost_undef = 0;
49 int Cha_mrph_cost_weight = KEITAISO_WEIGHT_DEFAULT;
50
51 anno_info Cha_anno_info[UNDEF_HINSI_MAX];
52 undef_info Cha_undef_info[UNDEF_HINSI_MAX];
53 int Cha_undef_info_num = 0;
54 int Cha_output_iscompound = 1;
55
56 char *Cha_bos_string = "";
57 char *Cha_eos_string = "EOS\n";
58
59 chasen_tok_t *Cha_tokenizer;
60
61 static void
62 read_class_cost(chasen_cell_t * cell)
63 {
64     int hinsi, cost;
65
66     for (; !nullp(cell); cell = cha_cdr(cell)) {
67         chasen_cell_t *cell1 = cha_car(cha_car(cell));
68         chasen_cell_t *cell2 = cha_cdr(cha_car(cell));
69         char *s = cha_s_atom(cha_car(cell1));
70         if (cha_litmatch(s, 3, STR_UNKNOWN_WORD,
71                          STR_UNKNOWN_WORD1, STR_UNKNOWN_WORD2)) {
72             int i;
73             for (i = 0; i < UNDEF_HINSI_MAX && !nullp(cell2);
74                  i++, cell2 = cha_cdr(cell2)) {
75                 chasen_cell_t *cell3 = cha_car(cell2);
76                 if (atomp(cell3)) {
77                     Cha_undef_info[i].cost = atoi(cha_s_atom(cell3));
78                     Cha_undef_info[i].cost_step = 0;
79                 } else {
80                     Cha_undef_info[i].cost =
81                         atoi(cha_s_atom(cha_car(cell3)));
82                     Cha_undef_info[i].cost_step =
83                         atoi(cha_s_atom(cha_car(cha_cdr(cell3))));
84                 }
85             }
86             if (Cha_undef_info_num == 0 || Cha_undef_info_num > i)
87                 Cha_undef_info_num = i;
88         } else if (!strcmp(s, "*")) {
89             cost = atoi(cha_s_atom(cha_car(cell2)));
90             for (hinsi = 1; Cha_hinsi[hinsi].name; hinsi++)
91                 if (Cha_hinsi[hinsi].cost == 0)
92                     Cha_hinsi[hinsi].cost = cost;
93         } else {
94             int match = 0;
95             cost = atoi(cha_s_atom(cha_car(cell2)));
96             for (hinsi = 1; Cha_hinsi[hinsi].name; hinsi++) {
97                 if (cha_match_nhinsi(cell1, hinsi)) {
98                     Cha_hinsi[hinsi].cost = cost;
99                     match = 1;
100                 }
101             }
102             if (!match)
103                 cha_exit_file(1, "invalid hinsi name `%s'\n",
104                               cha_s_tostr(cell1));
105         }
106     }
107
108     /*
109      * default 
110      */
111     for (hinsi = 1; Cha_hinsi[hinsi].name; hinsi++)
112         if (Cha_hinsi[hinsi].cost == 0)
113             Cha_hinsi[hinsi].cost = POS_COST_DEFAULT;
114
115     /*
116      * Ê¸Æ¬ Ê¸Ëö 
117      */
118     Cha_hinsi[0].cost = 0;
119 }
120
121 static void
122 read_composition(chasen_cell_t * cell)
123 {
124     int composit, pos;
125     chasen_cell_t *cell2, *cell3;
126
127     for (; !nullp(cell); cell = cha_cdr(cell)) {
128         cell2 = cha_car(cell);
129         composit = cha_get_nhinsi_id(cha_car(cell2));
130         if (!nullp(cha_cdr(cell2)))
131             cell2 = cha_cdr(cell2);
132         for (; !nullp(cell2); cell2 = cha_cdr(cell2)) {
133             cell3 = cha_car(cell2);
134             for (pos = 1; Cha_hinsi[pos].name; pos++)
135                 if (cha_match_nhinsi(cell3, pos))
136                     Cha_hinsi[pos].composit = composit;
137         }
138     }
139 }
140
141 static void
142 eval_chasenrc_sexp(chasen_cell_t * cell)
143 {
144     char *cell1_str;
145     chasen_cell_t *cell2;
146
147     cell1_str = cha_s_atom(cha_car(cell));
148     cell2 = cha_car(cha_cdr(cell));
149     if (Cha_errno)
150         return;
151
152     /*
153      * ¼­½ñ¥Õ¥¡¥¤¥ë 
154      */
155     if (!strcmp(cell1_str, CHA_LIT(STR_DA_FILE)))
156         cha_read_dadic(cha_cdr(cell));
157     /*
158      * ¶õÇòÉÊ»ì(space pos) 
159      */
160     else if (cha_litmatch(cell1_str, 1, STR_SPACE_POS)) {
161         Cha_anno_info[0].hinsi = cha_get_nhinsi_id(cell2);
162     }
163     /*
164      * Ãí¼á(annotation) 
165      */
166     else if (cha_litmatch(cell1_str, 1, STR_ANNOTATION)) {
167         int i;
168         for (i = 1, cell2 = cha_cdr(cell);
169              i < UNDEF_HINSI_MAX && !nullp(cell2);
170              i++, cell2 = cha_cdr(cell2)) {
171             chasen_cell_t *cell3 = cha_car(cell2);
172             chasen_cell_t *cell4;
173             /*
174              * str1, len1 
175              */
176             Cha_anno_info[i].str1 = cha_s_atom(cha_car(cha_car(cell3)));
177             Cha_anno_info[i].len1 = strlen(Cha_anno_info[i].str1);
178             cell4 = cha_car(cha_cdr(cha_car(cell3)));
179             /*
180              * str2, len2 
181              */
182             Cha_anno_info[i].str2 = nullp(cell4) ? "" : cha_s_atom(cell4);
183             Cha_anno_info[i].len2 = strlen(Cha_anno_info[i].str2);
184             /*
185              * hinsi 
186              */
187             cell4 = cha_car(cha_cdr(cell3));
188             if (!nullp(cell4)) {
189                 if (atomp(cell4)) {
190                     /*
191                      * format string 
192                      */
193                     Cha_anno_info[i].format = cha_s_atom(cell4);
194                 } else {
195                     /*
196                      * pos 
197                      */
198                     Cha_anno_info[i].hinsi = cha_get_nhinsi_id(cell4);
199                 }
200             }
201         }
202     }
203     /*
204      * Ì¤ÃθìÉÊ»ì 
205      */
206     else if (cha_litmatch(cell1_str, 2,
207                           STR_UNKNOWN_POS1, STR_UNKNOWN_POS2)) {
208         int i;
209         cell2 = cha_cdr(cell);
210         for (i = 0; i < UNDEF_HINSI_MAX && !nullp(cell2);
211              i++, cell2 = cha_cdr(cell2)) {
212             Cha_undef_info[i].hinsi = cha_get_nhinsi_id(cha_car(cell2));
213         }
214         if (Cha_undef_info_num == 0 || Cha_undef_info_num > i)
215             Cha_undef_info_num = i;
216     }
217     /*
218      * Ï¢ÀÜ¥³¥¹¥È½Å¤ß 
219      */
220     else if (cha_litmatch(cell1_str, 1, STR_CONN_WEIGHT))
221         Cha_con_cost_weight =
222             atoi(cha_s_atom(cell2)) * MRPH_DEFAULT_WEIGHT;
223     /*
224      * ·ÁÂÖÁÇ¥³¥¹¥È½Å¤ß 
225      */
226     else if (cha_litmatch(cell1_str, 1, STR_MRPH_WEIGHT))
227         Cha_mrph_cost_weight = atoi(cha_s_atom(cell2));
228     /*
229      * ¥³¥¹¥ÈÉý 
230      */
231     else if (cha_litmatch(cell1_str, 1, STR_COST_WIDTH))
232         cha_set_cost_width(atoi(cha_s_atom(cell2)));
233     /*
234      * Éʻ쥳¥¹¥È 
235      */
236     else if (cha_litmatch(cell1_str, 1, STR_POS_COST))
237         read_class_cost(cha_cdr(cell));
238     /*
239      * Ì¤ÄêµÁÏ¢ÀÜ¥³¥¹¥È 
240      */
241     else if (cha_litmatch(cell1_str, 1, STR_DEF_CONN_COST))
242         Cha_con_cost_undef = (int) atoi(cha_s_atom(cell2));
243     /*
244      * Ï¢·ëÉÊ»ì 
245      */
246     else if (cha_litmatch(cell1_str, 1, STR_COMPOSIT_POS))
247         read_composition(cha_cdr(cell));
248     /*
249      * Ê£¹ç¸ì 
250      */
251     else if (cha_litmatch(cell1_str, 1, STR_OUTPUT_COMPOUND))
252         Cha_output_iscompound =
253             cha_litmatch(cha_s_atom(cell2), 1, STR_SEG) ? 0 : 1;
254     /*
255      * ½ÐÎÏ¥Õ¥©¡¼¥Þ¥Ã¥È 
256      */
257     else if (cha_litmatch(cell1_str, 1, STR_OUTPUT_FORMAT))
258         cha_set_opt_form(cha_s_atom(cell2));
259     /*
260      * ¸À¸ì 
261      */
262     else if (cha_litmatch(cell1_str, 1, STR_LANG))
263         cha_set_language(cha_s_atom(cell2));
264     /*
265      * BOSʸ»úÎó 
266      */
267     else if (cha_litmatch(cell1_str, 1, STR_BOS_STR))
268         Cha_bos_string = cha_s_atom(cell2);
269     /*
270      * EOSʸ»úÎó 
271      */
272     else if (cha_litmatch(cell1_str, 1, STR_EOS_STR))
273         Cha_eos_string = cha_s_atom(cell2);
274     /*
275      * ¶èÀÚ¤êʸ»ú 
276      */
277     else if (cha_litmatch(cell1_str, 1, STR_DELIMITER))
278         cha_set_jfgets_delimiter(cha_s_atom(cell2));
279 }
280
281 /*
282  * cha_read_rcfile_fp()
283  */
284 void
285 cha_read_rcfile_fp(FILE * fp)
286 {
287     chasen_cell_t *cell;
288
289     while (!cha_s_feof(fp)) {
290         cell = cha_s_read(fp);
291         if (!Cha_errno)
292             eval_chasenrc_sexp(cell);
293     }
294 }
295
296 static void
297 read_chasenrc(void)
298 {
299     FILE *fp;
300     char *rcpath;
301
302     rcpath = cha_get_rcpath();
303
304     fp = cha_fopen(rcpath, "r", 1);
305     cha_read_rcfile_fp(fp);
306     fclose(fp);
307
308     /*
309      * required options 
310      */
311     if (!Cha_undef_info[0].hinsi)
312         cha_exit(1, "%s: UNKNOWN_POS/michigo-hinsi is not specified",
313                  cha_get_rcpath());
314
315     if (!Da_ndicfile)
316         cha_exit(1, "%s: dictionary is not specified",
317                  cha_get_rcpath());
318 }
319
320 /*
321  * cha_init - ChaSen's initialization
322  */
323 void
324 cha_init(void)
325 {
326     int i;
327
328     /*
329      * cost width 
330      */
331     cha_set_cost_width(COST_WIDTH_DEFAULT);
332
333     if (cha_literal[0][2] == NULL)
334         cha_set_encode("");
335
336     cha_read_grammar_dir();
337     cha_read_grammar(NULL, 1, 1);
338
339     read_chasenrc();
340
341     cha_read_katuyou(NULL, 1);
342     cha_read_table(NULL, 1);
343     cha_read_matrix(NULL);
344
345     for (i = 0; i < Cha_undef_info_num; i++)
346         Cha_undef_info[i].con_tbl =
347             cha_check_table_for_undef(Cha_undef_info[i].hinsi);
348
349     /*
350      * initialize the tokenizer
351      */
352     Cha_tokenizer = cha_tok_new(Cha_lang, Cha_encode);
353     cha_tok_set_annotation(Cha_tokenizer, Cha_anno_info);
354
355     Cha_mrph_block = cha_block_new(sizeof(mrph_t), MRPH_NUM);
356 }