Julius 4.2
libjulius/src/gramlist.c
説明を見る。
00001 
00034 /*
00035  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00036  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00037  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00038  * All rights reserved
00039  */
00040 
00041 #include <julius/julius.h>
00042 
00065 void
00066 multigram_add_gramlist(char *dfafile, char *dictfile, JCONF_LM *j, int lmvar)
00067 {
00068   GRAMLIST *new;
00069 
00070   new = (GRAMLIST *)mymalloc(sizeof(GRAMLIST));
00071   new->dfafile = new->dictfile = NULL;
00072   if (dfafile) new->dfafile = strcpy((char *)mymalloc(strlen(dfafile)+1), dfafile);
00073   if (dictfile) new->dictfile = strcpy((char *)mymalloc(strlen(dictfile)+1), dictfile);
00074   switch(lmvar) {
00075   case LM_DFA_GRAMMAR:
00076     new->next = j->gramlist_root;
00077     j->gramlist_root = new;
00078     break;
00079   case LM_DFA_WORD:
00080     new->next = j->wordlist_root;
00081     j->wordlist_root = new;
00082     break;
00083   }
00084 }
00085 
00102 void
00103 multigram_remove_gramlist(JCONF_LM *j)
00104 {
00105   GRAMLIST *g;
00106   GRAMLIST *tmp;
00107 
00108   g = j->gramlist_root;
00109   while (g) {
00110     tmp = g->next;
00111     if (g->dfafile) free(g->dfafile);
00112     if (g->dictfile) free(g->dictfile);
00113     free(g);
00114     g = tmp;
00115   }
00116   j->gramlist_root = NULL;
00117 
00118   g = j->wordlist_root;
00119   while (g) {
00120     tmp = g->next;
00121     if (g->dfafile) free(g->dfafile);
00122     if (g->dictfile) free(g->dictfile);
00123     free(g);
00124     g = tmp;
00125   }
00126   j->wordlist_root = NULL;
00127 }
00128 
00162 boolean
00163 multigram_add_prefix_list(char *prefix_list, char *cwd, JCONF_LM *j, int lmvar)
00164 {
00165   char buf[MAXGRAMNAMELEN], *p, *q;
00166   char buf2_d[MAXGRAMNAMELEN], *buf_d;
00167   char buf2_v[MAXGRAMNAMELEN], *buf_v;
00168   boolean ok_p, ok_p_total;
00169 
00170   if (prefix_list == NULL) return TRUE;
00171   
00172   p = &(prefix_list[0]);
00173 
00174   ok_p_total = TRUE;
00175   
00176   while(*p != '\0') {
00177     /* extract one prefix to buf[] */
00178     q = p;
00179     while(*p != '\0' && *p != ',') {
00180       buf[p-q] = *p;
00181       p++;
00182     }
00183     buf[p-q] = '\0';
00184 
00185     switch(lmvar) {
00186     case LM_DFA_GRAMMAR:
00187       /* register the new grammar to the grammar list to be read later */
00188       /* making file names from the prefix */
00189       ok_p = TRUE;
00190       strcpy(buf2_d, buf);
00191       strcat(buf2_d, ".dfa");
00192       buf_d = filepath(buf2_d, cwd);
00193       if (!checkpath(buf_d)) {
00194         jlog("ERROR: gramlist: cannot read dfa file \"%s\"\n", buf_d);
00195         ok_p = FALSE;
00196       }
00197       strcpy(buf2_v, buf);
00198       strcat(buf2_v, ".dict");
00199       buf_v = filepath(buf2_v, cwd);
00200       if (!checkpath(buf_v)) {
00201         jlog("ERROR: gramlist: cannot read dict file \"%s\"\n", buf_v);
00202         ok_p = FALSE;
00203       }
00204       if (ok_p == TRUE) {
00205         multigram_add_gramlist(buf_d, buf_v, j, lmvar);
00206       } else {
00207         ok_p_total = FALSE;
00208       }
00209       break;
00210     case LM_DFA_WORD:
00211       /* register the new word list to the list */
00212       /* treat the file name as a full file path (not prefix) */
00213       buf_v = filepath(buf, cwd);
00214       if (!checkpath(buf_v)) {
00215         jlog("ERROR: gramlist: cannot read wordlist file \"%s\"\n", buf_v);
00216         ok_p_total = FALSE;
00217       } else {
00218         multigram_add_gramlist(NULL, buf_v, j, lmvar);
00219       }
00220       break;
00221     } 
00222 
00223     /* move to next */
00224     if (*p == ',') p++;
00225   }
00226 
00227   return ok_p_total;
00228 }
00229 
00264 boolean
00265 multigram_add_prefix_filelist(char *listfile, JCONF_LM *j, int lmvar)
00266 {
00267   FILE *fp;
00268   char buf[MAXGRAMNAMELEN], *p, *src_bgn, *src_end, *dst;
00269   char *cdir;
00270   char buf2_d[MAXGRAMNAMELEN], *buf_d;
00271   char buf2_v[MAXGRAMNAMELEN], *buf_v;
00272   boolean ok_p, ok_p_total;
00273 
00274   if (listfile == NULL) return FALSE;
00275   if ((fp = fopen(listfile, "r")) == NULL) {
00276     jlog("ERROR: gramlist: failed to open grammar list file %s\n", listfile);
00277     return FALSE;
00278   }
00279 
00280   /* convert relative paths as relative to this list file */
00281   cdir = strcpy((char *)mymalloc(strlen(listfile)+1), listfile);
00282   get_dirname(cdir);
00283 
00284   ok_p_total = TRUE;
00285 
00286   while(getl_fp(buf, MAXGRAMNAMELEN, fp) != NULL) {
00287     /* remove comment */
00288     p = &(buf[0]);
00289     while(*p != '\0') {
00290       if (*p == '#') {
00291         *p = '\0';
00292         break;
00293       }
00294       p++;
00295     }
00296     if (buf[0] == '\0') continue;
00297     
00298     /* trim head/tail blanks */
00299     p = (&buf[0]);
00300     while(*p == ' ' || *p == '\t' || *p == '\r') p++;
00301     if (*p == '\0') continue;
00302     src_bgn = p;
00303     p = (&buf[strlen(buf) - 1]);
00304     while((*p == ' ' || *p == '\t' || *p == '\r') && p > src_bgn) p--;
00305     src_end = p;
00306     dst = (&buf[0]);
00307     p = src_bgn;
00308     while(p <= src_end) *dst++ = *p++;
00309     *dst = '\0';
00310     if (buf[0] == '\0') continue;
00311 
00312 
00313     switch(lmvar) {
00314     case LM_DFA_GRAMMAR:
00315       /* register the new grammar to the grammar list to be read later */
00316       ok_p = TRUE;
00317       strcpy(buf2_d, buf);
00318       strcat(buf2_d, ".dfa");
00319       buf_d = filepath(buf2_d, cdir);
00320       if (!checkpath(buf_d)) {
00321         jlog("ERROR: gramlist: cannot read dfa file \"%s\"\n", buf_d);
00322         ok_p = FALSE;
00323       }
00324       strcpy(buf2_v, buf);
00325       strcat(buf2_v, ".dict");
00326       buf_v = filepath(buf2_v, cdir);
00327       if (!checkpath(buf_v)) {
00328         jlog("ERROR: gramlist: cannot read dict file \"%s\"\n", buf_v);
00329         ok_p = FALSE;
00330       }
00331       if (ok_p == TRUE) {
00332         multigram_add_gramlist(buf_d, buf_v, j, lmvar);
00333       } else {
00334         ok_p_total = FALSE;
00335       }
00336       break;
00337     case LM_DFA_WORD:
00338       /* register the new word list to the list */
00339       /* treat the file name as a full file path (not prefix) */
00340       buf_v = filepath(buf, cdir);
00341       if (!checkpath(buf_v)) {
00342         jlog("ERROR: gramlist: cannot read wordlist file \"%s\"\n", buf_v);
00343         ok_p_total = FALSE;
00344       } else {
00345         multigram_add_gramlist(NULL, buf_v, j, lmvar);
00346       }
00347       break;
00348     }
00349 
00350   }
00351 
00352   free(cdir);
00353   
00354   fclose(fp);
00355 
00356   return ok_p_total;
00357 }
00358 
00359 /* end of file */