00001
00018
00019
00020
00021
00022
00023
00024
00025 #include "app.h"
00026
00031 #define TEXTWIDTH 70
00032 #define MAXBUFLEN 4096
00033
00034 static char fname[MAXBUFLEN];
00035 static FILE *fp = NULL;
00036
00037 void
00038 outfile_set_fname(char *input_filename)
00039 {
00040 char *p;
00041
00042 strncpy(fname, input_filename, MAXBUFLEN);
00043 if ((p = strrchr(fname, '.')) != NULL) {
00044 *p = '\0';
00045 }
00046 strcat(fname, OUTPUT_FILE_SUFFIX);
00047 }
00048
00049 static void
00050 outfile_open(Recog *recog, void *dummy)
00051 {
00052 if ((fp = fopen(fname, "w")) == NULL) {
00053 fprintf(stderr, "output_rec: failed to open \"%s\", result not saved\n", fname);
00054 return;
00055 }
00056 }
00057
00058 static void
00059 outfile_close(Recog *recog, void *dummy)
00060 {
00061 if (fp != NULL) {
00062 fclose(fp);
00063 fprintf(stderr, "result written to \"%s\"\n", fname);
00064 }
00065 fp = NULL;
00066 }
00067
00068 static void
00069 outfile_sentence(Recog *recog, void *dummy)
00070 {
00071 RecogProcess *r;
00072 Sentence *s;
00073 WORD_INFO *winfo;
00074 WORD_ID *seq;
00075 int seqnum;
00076 int n, num;
00077 int i, j;
00078 boolean multi;
00079 static char phbuf[MAX_HMMNAME_LEN];
00080
00081 if (recog->process_list->next != NULL) multi = TRUE;
00082 else multi = FALSE;
00083
00084 for(r=recog->process_list;r;r=r->next) {
00085 if (! r->live) continue;
00086 if (multi) fprintf(fp, "[#%d %s]\n", r->config->id, r->config->name);
00087
00088 if (r->result.status < 0) {
00089 switch(r->result.status) {
00090 case J_RESULT_STATUS_REJECT_POWER:
00091 fprintf(fp, "<input rejected by power>\n");
00092 break;
00093 case J_RESULT_STATUS_TERMINATE:
00094 fprintf(fp, "<input teminated by request>\n");
00095 break;
00096 case J_RESULT_STATUS_ONLY_SILENCE:
00097 fprintf(fp, "<input rejected by decoder (silence input result)>\n");
00098 break;
00099 case J_RESULT_STATUS_REJECT_GMM:
00100 fprintf(fp, "<input rejected by GMM>\n");
00101 break;
00102 case J_RESULT_STATUS_REJECT_SHORT:
00103 fprintf(fp, "<input rejected by short input>\n");
00104 break;
00105 case J_RESULT_STATUS_FAIL:
00106 fprintf(fp, "<search failed>\n");
00107 break;
00108 }
00109 continue;
00110 }
00111
00112 winfo = r->lm->winfo;
00113 num = r->result.sentnum;
00114
00115 for(n=0;n<num;n++) {
00116 s = &(r->result.sent[n]);
00117 seq = s->word;
00118 seqnum = s->word_num;
00119
00120 fprintf(fp, "sentence%d:", n+1);
00121 for (i=0;i<seqnum;i++) {
00122 fprintf(fp, " %s", winfo->woutput[seq[i]]);
00123 }
00124 fprintf(fp, "\n");
00125
00126 fprintf(fp, "wseq%d:", n+1);
00127 for (i=0;i<seqnum;i++) {
00128 fprintf(fp, " %s", winfo->wname[seq[i]]);
00129 }
00130 fprintf(fp, "\n");
00131
00132 fprintf(fp, "phseq%d:", n+1);
00133 for (i=0;i<seqnum;i++) {
00134 if (i > 0) fprintf(fp, " |");
00135 for (j=0;j<winfo->wlen[seq[i]];j++) {
00136 center_name(winfo->wseq[seq[i]][j]->name, phbuf);
00137 fprintf(fp, " %s", phbuf);
00138 }
00139 }
00140 fprintf(fp, "\n");
00141
00142 #ifdef CONFIDENCE_MEASURE
00143 fprintf(fp, "cmscore%d:", n+1);
00144 for (i=0;i<seqnum;i++) {
00145 fprintf(fp, " %5.3f", s->confidence[i]);
00146 }
00147 fprintf(fp, "\n");
00148 #endif
00149 fprintf(fp, "score%d: %f", n+1, s->score);
00150 if (r->lmtype == LM_PROB) {
00151 fprintf(fp, " (AM: %f LM: %f)", s->score_am, s->score_lm);
00152 }
00153 fprintf(fp, "\n");
00154 if (r->lmtype == LM_DFA) {
00155 if (multigram_get_all_num(r->lm) > 1) {
00156 fprintf(fp, "grammar%d: %d\n", n+1, s->gram_id);
00157 }
00158 }
00159
00160 if (s->align.filled) {
00161 HMM_Logical *p;
00162 int i;
00163
00164 fprintf(fp, "=== begin forced alignment ===\n");
00165 fprintf(fp, " id: from to n_score unit\n");
00166 fprintf(fp, " ----------------------------------------\n");
00167 for(i=0;i<s->align.num;i++) {
00168 fprintf(fp, "[%4d %4d] %f ", s->align.begin_frame[i], s->align.end_frame[i], s->align.avgscore[i]);
00169 switch(s->align.unittype) {
00170 case PER_WORD:
00171 fprintf(fp, "%s\t[%s]\n", winfo->wname[s->align.w[i]], winfo->woutput[s->align.w[i]]);
00172 break;
00173 case PER_PHONEME:
00174 p = s->align.ph[i];
00175 if (p->is_pseudo) {
00176 fprintf(fp, "{%s}\n", p->name);
00177 } else if (strmatch(p->name, p->body.defined->name)) {
00178 fprintf(fp, "%s\n", p->name);
00179 } else {
00180 fprintf(fp, "%s[%s]\n", p->name, p->body.defined->name);
00181 }
00182 break;
00183 case PER_STATE:
00184 p = s->align.ph[i];
00185 if (p->is_pseudo) {
00186 fprintf(fp, "{%s}", p->name);
00187 } else if (strmatch(p->name, p->body.defined->name)) {
00188 fprintf(fp, "%s", p->name);
00189 } else {
00190 fprintf(fp, "%s[%s]", p->name, p->body.defined->name);
00191 }
00192 if (r->am->hmminfo->multipath) {
00193 if (s->align.is_iwsp[i]) {
00194 fprintf(fp, " #%d (sp)\n", s->align.loc[i]);
00195 } else {
00196 fprintf(fp, " #%d\n", s->align.loc[i]);
00197 }
00198 } else {
00199 fprintf(fp, " #%d\n", s->align.loc[i]);
00200 }
00201 break;
00202 }
00203 }
00204
00205 fprintf(fp, "re-computed AM score: %f\n", s->align.allscore);
00206
00207 fprintf(fp, "=== end forced alignment ===\n");
00208 }
00209 }
00210 }
00211
00212 }
00213
00214 static void
00215 outfile_gmm(Recog *recog, void *dummy)
00216 {
00217 HTK_HMM_Data *d;
00218 GMMCalc *gc;
00219 int i;
00220
00221 gc = recog->gc;
00222
00223 fprintf(fp, "--- GMM result begin ---\n");
00224 i = 0;
00225 for(d=recog->gmm->start;d;d=d->next) {
00226 fprintf(fp, " [%8s: total=%f avg=%f]\n", d->name, gc->gmm_score[i], gc->gmm_score[i] / (float)gc->framecount);
00227 i++;
00228 }
00229 fprintf(fp, " max = \"%s\"", gc->max_d->name);
00230 #ifdef CONFIDENCE_MEASURE
00231 fprintf(fp, " (CM: %f)", gc->gmm_max_cm);
00232 #endif
00233 fprintf(fp, "\n");
00234 fprintf(fp, "--- GMM result end ---\n");
00235 }
00236
00237 static void
00238 outfile_graph(Recog *recog, void *dummy)
00239 {
00240 WordGraph *wg;
00241 int tw1, tw2, i;
00242 WORD_INFO *winfo;
00243 RecogProcess *r;
00244 boolean multi;
00245
00246 if (recog->process_list->next != NULL) multi = TRUE;
00247 else multi = FALSE;
00248
00249 for(r=recog->process_list;r;r=r->next) {
00250 if (! r->live) continue;
00251 if (r->result.wg == NULL) continue;
00252 if (multi) fprintf(fp, "[#%d %s]\n", r->config->id, r->config->name);
00253
00254 winfo = r->lm->winfo;
00255
00256
00257 wordgraph_dump(fp, r->result.wg, winfo);
00258
00259 fprintf(fp, "-------------------------- begin wordgraph show -------------------------\n");
00260 for(wg=r->result.wg;wg;wg=wg->next) {
00261 tw1 = (TEXTWIDTH * wg->lefttime) / r->peseqlen;
00262 tw2 = (TEXTWIDTH * wg->righttime) / r->peseqlen;
00263 fprintf(fp, "%4d:", wg->id);
00264 for(i=0;i<tw1;i++) fprintf(fp, " ");
00265 fprintf(fp, " %s\n", winfo->woutput[wg->wid]);
00266 fprintf(fp, "%4d:", wg->lefttime);
00267 for(i=0;i<tw1;i++) fprintf(fp, " ");
00268 fprintf(fp, "|");
00269 for(i=tw1+1;i<tw2;i++) fprintf(fp, "-");
00270 fprintf(fp, "|\n");
00271 }
00272 fprintf(fp, "-------------------------- end wordgraph show ---------------------------\n");
00273 }
00274 }
00275
00276 static void
00277 outfile_confnet(Recog *recog, void *dummy)
00278 {
00279 CN_CLUSTER *c;
00280 int i;
00281 RecogProcess *r;
00282 boolean multi;
00283
00284 if (recog->process_list->next != NULL) multi = TRUE;
00285 else multi = FALSE;
00286
00287 for(r=recog->process_list;r;r=r->next) {
00288 if (! r->live) continue;
00289 if (r->result.confnet == NULL) continue;
00290 if (multi) fprintf(fp, "[#%d %s]\n", r->config->id, r->config->name);
00291
00292 fprintf(fp, "---- begin confusion network ---\n");
00293 for(c=r->result.confnet;c;c=c->next) {
00294 for(i=0;i<c->wordsnum;i++) {
00295 fprintf(fp, "(%s:%.3f)", (c->words[i] == WORD_INVALID) ? "-" : r->lm->winfo->woutput[c->words[i]], c->pp[i]);
00296 if (i == 0) fprintf(fp, " ");
00297 }
00298 fprintf(fp, "\n");
00299 }
00300 fprintf(fp, "---- end confusion network ---\n");
00301 }
00302 }
00303
00304
00305 void
00306 setup_output_file(Recog *recog, void *data)
00307 {
00308 callback_add(recog, CALLBACK_EVENT_RECOGNITION_BEGIN, outfile_open, data);
00309 callback_add(recog, CALLBACK_EVENT_RECOGNITION_END, outfile_close, data);
00310 callback_add(recog, CALLBACK_RESULT, outfile_sentence, data);
00311 callback_add(recog, CALLBACK_RESULT_GMM, outfile_gmm, data);
00312 callback_add(recog, CALLBACK_RESULT_GRAPH, outfile_graph, data);
00313 callback_add(recog, CALLBACK_RESULT_CONFNET, outfile_confnet, data);
00314 }
00315