1 /*
2  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
3  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
4  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
5  * All rights reserved
6  */
7 
8 /* next_word functions */
9 
10 #include "common.h"
11 #include "gen_next.h"
12 
13 extern WORD_INFO *winfo;
14 extern DFA_INFO *dfa;
15 
16 NEXTWORD **
nw_malloc()17 nw_malloc()
18 {
19   NEXTWORD **nw;
20   NEXTWORD *nwtmp;
21   int i;
22   int maxnw;
23 
24   maxnw = winfo->num * 2;	/* NOISE$B$rHt$P$9J,(B */
25   /* $BO"B3NN0h$rG[Ns$K3d$jEv$F$k(B */
26   nw = (NEXTWORD **)malloc(maxnw * sizeof(NEXTWORD *));
27   nwtmp = (NEXTWORD *)malloc(maxnw * sizeof(NEXTWORD));
28   nw[0] = nwtmp;
29   for (i=1;i<maxnw; i++) {
30     nw[i] = &(nwtmp[i]);
31   }
32   return nw;
33 }
34 
35 /* $BM=B,<!C18l3JG<NN0h$N2rJ|(B */
36 void
nw_free(NEXTWORD ** nw)37 nw_free(NEXTWORD **nw)
38 {
39   free(nw[0]);
40   free(nw);
41 }
42 
43 
44 
45 /* $B=i4|>uBV$+$iA+0\$7$&$kC18l=89g$rJV$9(B */
46 /* $BJV$jCM(B: $BC18l?t(B*/
47 /* NOISE: $B$3$3$K$OMh$J$$;EMM(B */
48 int
dfa_firstwords(NEXTWORD ** nw)49 dfa_firstwords(NEXTWORD **nw)
50 {
51   DFA_ARC *arc;
52   int i, cate, iw, ns;
53   int num = 0;
54 
55   for (i=0;i<dfa->state_num;i++) {
56     if ((dfa->st[i].status & INITIAL_S) != 0) { /* $B=i4|>uBV$+$i(B */
57       for (arc = dfa->st[i].arc; arc; arc = arc->next) {	/* $BA4$F$NA+0\(B */
58 	cate = arc->label;
59 	ns = arc->to_state;
60 	/* $BA+0\$KBP1~$9$k%+%F%4%jFb$NA4C18l$rE83+(B */
61 	for (iw=0;iw<dfa->term.wnum[cate];iw++) {
62 	  nw[num]->id = dfa->term.tw[cate][iw];
63 	  nw[num]->next_state = ns;
64 	  nw[num]->can_insert_sp = FALSE;
65 	  num++;
66 	}
67       }
68     }
69   }
70 
71   return num;
72 }
73 int
dfa_firstterms(NEXTWORD ** nw)74 dfa_firstterms(NEXTWORD **nw)
75 {
76   DFA_ARC *arc;
77   int i, cate, ns;
78   int num = 0;
79 
80   for (i=0;i<dfa->state_num;i++) {
81     if ((dfa->st[i].status & INITIAL_S) != 0) { /* $B=i4|>uBV$+$i(B */
82       for (arc = dfa->st[i].arc; arc; arc = arc->next) {	/* $BA4$F$NA+0\(B */
83 	cate = arc->label;
84 	ns = arc->to_state;
85 	/* $BA+0\$KBP1~$9$k%+%F%4%jFb$N(B1$BC18l$rE83+(B */
86 	if (dfa->term.wnum[cate] == 0) continue;
87 	nw[num]->id = dfa->term.tw[cate][0];
88 	nw[num]->next_state = ns;
89 	nw[num]->can_insert_sp = FALSE;
90 	num++;
91       }
92     }
93   }
94 
95   return num;
96 }
97 
98 /* $B<!$K@\B3$7F@$kC18l72$rJV$9(B */
99 /* $BJV$jCM(B:$BC18l?t(B */
100 /* NOISE: $B@h$^$G8+$F!$(Bcan_insert_sp=TRUE$B$GJV$9(B */
101 int
dfa_nextwords(NODE * hypo,NEXTWORD ** nw)102 dfa_nextwords(NODE *hypo, NEXTWORD **nw)
103 {
104   DFA_ARC *arc, *arc2;
105   int iw,cate,ns,cate2,ns2;
106   int num = 0;
107 
108   for (arc = dfa->st[hypo->state].arc; arc; arc = arc->next) {
109     cate = arc->label;
110     ns = arc->to_state;
111     if (dfa->is_sp[cate]) {
112       /* $B@h$^$G8+$k!#<+J,$OE83+$7$J$$(B */
113       for (arc2 = dfa->st[ns].arc; arc2; arc2 = arc2->next) {
114 	cate2 = arc2->label;
115 	ns2 = arc2->to_state;
116 	for (iw=0;iw<dfa->term.wnum[cate2];iw++) {
117 	  nw[num]->id = dfa->term.tw[cate2][iw];
118 	  nw[num]->next_state = ns2;
119 	  nw[num]->can_insert_sp = TRUE;
120 	  num++;
121 	}
122       }
123     } else {
124       /* $BA+0\$KBP1~$9$k%+%F%4%jFb$NA4C18l$rE83+(B */
125       for (iw=0;iw<dfa->term.wnum[cate];iw++) {
126 	nw[num]->id = dfa->term.tw[cate][iw];
127 	nw[num]->next_state = ns;
128 	nw[num]->can_insert_sp = FALSE;
129 	num++;
130       }
131     }
132   }
133   return num;
134 }
135 int
dfa_nextterms(NODE * hypo,NEXTWORD ** nw)136 dfa_nextterms(NODE *hypo, NEXTWORD **nw)
137 {
138   DFA_ARC *arc, *arc2;
139   int cate,ns,cate2,ns2;
140   int num = 0;
141 
142   for (arc = dfa->st[hypo->state].arc; arc; arc = arc->next) {
143     cate = arc->label;
144     ns = arc->to_state;
145     if (dfa->is_sp[cate]) {
146       /* $B@h$^$G8+$k!#<+J,$OE83+$7$J$$(B */
147       for (arc2 = dfa->st[ns].arc; arc2; arc2 = arc2->next) {
148 	cate2 = arc2->label;
149 	ns2 = arc2->to_state;
150 	if (dfa->term.wnum[cate2] == 0) continue;
151 	nw[num]->id = dfa->term.tw[cate2][0];
152 	nw[num]->next_state = ns2;
153 	nw[num]->can_insert_sp = TRUE;
154 	num++;
155       }
156     } else {
157       /* $BA+0\$KBP1~$9$k%+%F%4%jFb$NA4C18l$rE83+(B */
158       if (dfa->term.wnum[cate] == 0) continue;
159       nw[num]->id = dfa->term.tw[cate][0];
160       nw[num]->next_state = ns;
161       nw[num]->can_insert_sp = FALSE;
162       num++;
163     }
164   }
165   return num;
166 }
167 
168 /* $B2>@b$,J8$H$7$F<uM}2DG=$G$"$k$+$I$&$+$rJV$9(B */
169 /* NOISE: $B$3$3$K$O$3$J$$;EMM(B */
170 boolean
dfa_acceptable(NODE * hypo)171 dfa_acceptable(NODE *hypo)
172 {
173   /* $B<uM}>uBV$J$i(B */
174   if (dfa->st[hypo->state].status & ACCEPT_S) {
175     return TRUE;
176   } else {
177     return FALSE;
178   }
179 }
180