File: connect.c

package info (click to toggle)
chasen 2.4.5-46.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,616 kB
  • sloc: sh: 10,024; ansic: 5,983; cpp: 179; makefile: 111; perl: 10
file content (315 lines) | stat: -rw-r--r-- 7,647 bytes parent folder | download
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
/*
 * Copyright (c) 2003 Nara Institute of Science and Technology
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name Nara Institute of Science and Technology may not be used to
 *    endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Nara Institute of Science and Technology 
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE Nara Institute
 * of Science and Technology BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: connect.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
 */

#include "chadic.h"

#define CHA_LINEMAX 8192

static int tbl_num;		/* ϢɽΥ */
static int tbl_num_goi;		/* Ϣɽθäο */
static int i_num;		/* Ϣܹι */
static int j_num;		/* Ϣܹ */
static rensetu_pair_t *rensetu_tbl;
static connect_rule_t *connect_mtr;

/*
 * rensetu table
 */
static int
cmp_pair(rensetu_pair_t * pair1, rensetu_pair_t * pair2)
{
    int ret;

    /*
     * Ф surface form 
     */
    if (pair1->goi == NULL && pair2->goi != NULL)
	return 1;
    if (pair1->goi != NULL && pair2->goi == NULL)
	return -1;

    /*
     * ʻʬ POS 
     */
    if ((ret = pair1->hinsi - pair2->hinsi) != 0)
	return ret;

    /*
     * ѷ Conjugation type 
     */
    if ((ret = pair1->type - pair2->type) != 0)
	return ret;

    /*
     * Ф surface form 
     */
    if (pair1->goi != NULL && pair2->goi != NULL)
	if ((ret = strcmp(pair1->goi, pair2->goi)) != 0)
	    return ret;

    /*
     * ѷ Conjugation form 
     */
    if ((ret = pair1->form - pair2->form) != 0)
	return ret;

    return pair1->index - pair2->index;
}

static char *
cha_numtok(char *s, int *valp)
{
    int minus = 0;

    while (*s == ' ')
	s++;
    if (*s == '-') {
	minus = 1;
	s++;
    }
    if (*s < '0' || *s > '9')
	cha_exit_file(1, "illegal format");
    for (*valp = 0; *s >= '0' && *s <= '9';
	 *valp = *valp * 10 + *s++ - '0');
    while (*s == ' ')
	s++;

    if (minus)
	*valp = -*valp;

    return s;
}

void
cha_read_table(FILE * fp_out, int dir)
{
    FILE *fp;
    char *filepath;
    char buf[CHA_LINEMAX], *s;
    int i, val, cell_num;

    fp = cha_fopen_grammar(TABLE_FILE, "r", 1, dir, &filepath);

    if (fp_out != NULL)
	fprintf(fp_out, "parsing %s\n", filepath);

    Cha_lineno_error = ++Cha_lineno;
    fscanf(fp, "%d\n", &cell_num);
    rensetu_tbl =
	(rensetu_pair_t *) cha_malloc(sizeof(rensetu_pair_t) * cell_num);

    tbl_num = 0;
    for (i = 0; i < cell_num; i++) {
	Cha_lineno_error = ++Cha_lineno;
	if (fgets(buf, sizeof(buf), fp) == NULL)
	    cha_exit_file(1, "illegal format");
	Cha_lineno_error = ++Cha_lineno;
	if (fgets(s = buf, sizeof(buf), fp) == NULL)
	    cha_exit_file(1, "illegal format");
	s = cha_numtok(s, &val);
	rensetu_tbl[i].i_pos = val;
	s = cha_numtok(s, &val);
	rensetu_tbl[i].j_pos = val;
	if (!tbl_num && val < 0)
	    tbl_num = i;
	buf[strlen(buf) - 1] = '\0';
	if (*s >= '0' && *s <= '9') {
	    s = cha_numtok(s, &val);
	    rensetu_tbl[i].index = i;
	    rensetu_tbl[i].hinsi = val;
	    s = cha_numtok(s, &val);
	    rensetu_tbl[i].type = val;
	    s = cha_numtok(s, &val);
	    rensetu_tbl[i].form = val;
	    if (*s == '*') {
		rensetu_tbl[i].goi = NULL;
	    } else {
		rensetu_tbl[i].goi = cha_strdup(s);
		tbl_num_goi++;
	    }
	}
    }

    if (!tbl_num)
	tbl_num = cell_num;

    fclose(fp);
}

static int
find_table(lexicon_t * mrph, rensetu_pair_t * pair)
{
    int ret;

    /*
     * ʻʬ POS 
     */
    if ((ret = mrph->pos - pair->hinsi) != 0)
	return ret;
    /*
     * ѷ Conjugation type 
     */
    if ((ret = mrph->inf_type - pair->type) != 0)
	return ret;

    /*
     * Ф surface form 
     */
    if (pair->goi && (ret = strcmp(mrph->headword, pair->goi)))
	return ret;

    /*
     * ѸʤСѷ1֤ȥޥå
     */
    if (mrph->inf_type)
	return 1 - pair->form;
    return 0;
}

/* if an error occurs, this function returns 0, else returns 1 */
int
cha_check_table(lexicon_t * mrph)
{
    rensetu_pair_t *ret;

    if (rensetu_tbl[0].hinsi == 0)
	qsort(rensetu_tbl, tbl_num, sizeof(rensetu_pair_t),
	      (int (*)(const void *, const void *)) cmp_pair);

    ret = (rensetu_pair_t *)
	bsearch(mrph, rensetu_tbl, tbl_num_goi,
		sizeof(rensetu_pair_t), (int (*)(const void *, const void *)) find_table);
    if (ret) {
	mrph->con_tbl = ret->index;
	return 1;
    }

    ret = (rensetu_pair_t *)
	bsearch(mrph, rensetu_tbl + tbl_num_goi, tbl_num - tbl_num_goi,
		sizeof(rensetu_pair_t), (int (*)(const void *, const void *)) find_table);
    if (ret) {
	mrph->con_tbl = ret->index;
	return 1; /* if no error, return 1 */
    }

    /*
     * error
     */
    cha_exit_file(-1, "no morpheme in connection table\n");
    return 0;
}

int
cha_check_table_for_undef(int hinsi)
{
    int i;

    for (i = 0; i < tbl_num; i++)
	if (rensetu_tbl[i].hinsi == hinsi)
	    if (!rensetu_tbl[i].goi)
		return i;

    return -1;
}

/*
 * rensetu matrix
 */
void
cha_read_matrix(FILE * fp_out)
{
    FILE *fp;
    char *filepath;
    int i, j, cost, next;
    char buf[CHA_LINEMAX], *s;

    fp = cha_fopen_grammar(MATRIX_FILE, "r", 1, 1, &filepath);

    if (fp_out != NULL)
	fprintf(fp_out, "parsing %s", filepath);

    Cha_lineno_error = ++Cha_lineno;
    fscanf(fp, "%d %d\n", &i_num, &j_num);
    connect_mtr = (connect_rule_t *)
	cha_malloc(sizeof(connect_rule_t) * i_num * j_num);

    next = 0;
    for (i = 0; i < i_num; i++) {
	Cha_lineno_error = ++Cha_lineno;
	if (fgets(s = buf, sizeof(buf), fp) == NULL)
	    cha_exit_file(1, "illegal format");
	for (j = 0; j < j_num;) {
	    int nval;
	    if (*s == 'o') {
		s = cha_numtok(s + 1, &nval);
		next = cost = 0;
	    } else {
		s = cha_numtok(s, &next);
		if (*s++ != ',')
		    cha_exit_file(1, "illegal format");
		s = cha_numtok(s, &cost);
		if (*s == 'x')
		    s = cha_numtok(s + 1, &nval);
		else
		    nval = 1;
	    }
	    while (nval-- > 0) {
		connect_mtr[i * j_num + j].next = next;
		connect_mtr[i * j_num + j].cost = cost;
		j++;
	    }
	}
    }
    fclose(fp);
}

int
cha_check_automaton(int state, int con, int undef_con_cost, int *costp)
{
    connect_rule_t *cr;
#if 0
    printf("[%d:%d:%d]\n", state, con, rensetu_tbl[con].j_pos);
    fflush(stdout);
#endif
    cr = &connect_mtr[state * j_num + rensetu_tbl[con].j_pos];
    *costp = cr->cost;
    if (*costp == 0)
	*costp = undef_con_cost;
    else
	(*costp)--;

#ifdef DEBUG
    printf("[state:%d,con:%d,newcon:%d] ", state, con, cr->next + con);
#endif

    return rensetu_tbl[cr->next + con].i_pos;
}