2 * Copyright (c) 2003 Nara Institute of Science and Technology
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * $Id: grammar.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
36 hinsi_t Cha_hinsi[HINSI_MAX];
42 make_hinsi(chasen_cell_t * cell, int mother, int idx)
49 cha_exit_file(1, "too many (over %d) parts of speech", HINSI_MAX);
54 depth = Cha_hinsi[mother].depth + 1;
55 path = cha_malloc(sizeof(short) * (depth + 1));
56 memcpy(path, Cha_hinsi[mother].path, sizeof(short) * depth);
57 path[depth - 1] = idx;
59 Cha_hinsi[idx].depth = depth;
60 Cha_hinsi[idx].path = path;
63 * hinsi name and katsuyou
65 name = cha_s_atom(cha_car(cell));
67 printf("%2d:%*s%s\n", depth, depth * 2, "", name);
71 * ÉÊ»ì¤ÎÆó½ÅÅÐÏ¿¤Î¥Á¥§¥Ã¥¯ ¤¢¤Þ¤ê¤¤ì¤¤¤ÊÊýË¡¤Ç¤Ï¤Ê¤¤
73 for (i = 0; Cha_hinsi[mother].daughter[i + 1]; i++) {
74 if (!strcmp(Cha_hinsi[Cha_hinsi[mother].daughter[i]].name, name))
75 cha_exit_file(1, "hinsi `%s' is already defined", name);
78 s = name + strlen(name) - 1;
79 if (Cha_hinsi[mother].kt == 1 || *s == '%') {
80 Cha_hinsi[idx].kt = 1;
86 cha_exit_file(1, "an empty string for hinsi name");
88 Cha_hinsi[idx].name = cha_strdup(name);
90 cha_s_print(stdout, cha_car(cell));
91 printf("[%d,%d,%s]\n", mother, idx, name);
97 static short daughter0 = 0;
98 Cha_hinsi[idx++].daughter = &daughter0;
104 * ÉÊ»ì¤ÎÆó½ÅÅÐÏ¿¤Î¥Á¥§¥Ã¥¯¤Î¤¿¤á°ì»þŪ¤Ë daughter ¤òÂåÆþ
106 Cha_hinsi[idx].daughter = daughter;
107 for (; !nullp(cell); cell = cha_cdr(cell)) {
108 daughter[ndaughter++] = d;
109 daughter[ndaughter] = 0;
110 d = make_hinsi(cha_car(cell), idx, d);
112 daughter[ndaughter++] = 0;
113 Cha_hinsi[idx].daughter = cha_malloc(sizeof(short) * ndaughter);
114 memcpy(Cha_hinsi[idx].daughter, daughter,
115 sizeof(short) * ndaughter);
126 cha_read_class(FILE * fp)
128 static short path0 = 0;
129 chasen_cell_t *cell1;
136 Cha_hinsi[0].path = &path0;
137 Cha_hinsi[0].depth = 0;
139 Cha_hinsi[0].name = CHA_LIT(STR_BOS_EOS);
144 * ÉÊ»ì¤ÎÆó½ÅÅÐÏ¿¤Î¥Á¥§¥Ã¥¯¤Î¤¿¤á°ì»þŪ¤Ë daughter ¤òÂåÆþ
146 Cha_hinsi[0].daughter = daughter;
147 while (!cha_s_feof(fp)) {
148 if (!nullp(cell1 = cha_s_read(fp))) {
149 daughter[ndaughter++] = idx;
150 daughter[ndaughter] = 0;
151 idx = make_hinsi(cell1, 0, idx);
155 daughter[ndaughter++] = 0;
156 Cha_hinsi[0].daughter = cha_malloc(sizeof(short) * ndaughter);
157 memcpy(Cha_hinsi[0].daughter, daughter, sizeof(short) * ndaughter);
162 Cha_hinsi[idx].name = NULL;
166 * cha_match_nhinsi - cell¤Îwildcardɽ¸½¤¬hinsi¤È¥Þ¥Ã¥Á¤·¤Æ¤¤¤ë¤«¤É¤¦¤«
169 cha_match_nhinsi(chasen_cell_t * cell, int hinsi)
174 for (path = Cha_hinsi[hinsi].path; !nullp(cell);
175 path++, cell = cha_cdr(cell)) {
176 name = cha_s_atom(cha_car(cell));
179 * cell ¤ÎÊý¤¬Ä¹¤¤¤È¤¤Ï¡¢ºÇ¸å¤ÎϢ³¤·¤¿ "*" ¤Ï̵»ë¤¹¤ë
180 * ¤³¤ì¤Ë¤è¤ê cell:(Éû»ì *) ¤È hinsi:Éû»ì ¤¬¥Þ¥Ã¥Á¤¹¤ë
181 * chasenrc ¤ÎÉʻ쥳¥¹¥È¤Î»ØÄê¤Ç (Éû»ì *) ¤Ê¤É¤¬»È¤ï¤ì¤Æ¤¤¤ë
182 * connect.cha ¤Ç¤â»È¤ï¤ì¤ë²ÄǽÀ¤¬¤¢¤ë
184 if (strcmp(name, "*"))
187 * ¤³¤ì°Ê¹ß¤Ï *path ¤ÎÃͤ¬ 0 ¤Ë¤Ê¤ë¤è¤¦¤Ë¤¹¤ë
191 if (strcmp(name, "*") && strcmp(name, Cha_hinsi[*path].name))
196 * cell ¤¬ hinsi ¤è¤ê¤âÁƤ¤Ê¬Îà¤Ê¤é¥Þ¥Ã¥Á
202 * cha_read_grammar - read GRAMMAR_FILE and set Class[][]
205 * dir - 0: read from current directory
206 * 1: read from grammar directory
207 * 2: read from current directory or grammar directory
210 cha_read_grammar(FILE * fp_out, int ret, int dir)
215 fp = cha_fopen_grammar(GRAMMAR_FILE, "r", ret, dir, &filepath);
217 fprintf(fp_out, "parsing %s\n", filepath);