int main(int argc, char *argv[]) { ps_decoder_t *ps; ps_nbest_t *nbest; cmd_ln_t *config; FILE *rawfh; char const *hyp; int32 score, n; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/en-us/en-us", "-lm", MODELDIR "/en-us/en-us.lm.bin", "-dict", MODELDIR "/en-us/cmudict-en-us.dict", "-fwdtree", "yes", "-fwdflat", "yes", "-bestpath", "yes", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); ps_decode_raw(ps, rawfh, -1); fclose(rawfh); hyp = ps_get_hyp(ps, &score); printf("BESTPATH: %s (%d)\n", hyp, score); for (n = 1, nbest = ps_nbest(ps); nbest && n < 10; nbest = ps_nbest_next(nbest), n++) { ps_seg_t *seg; hyp = ps_nbest_hyp(nbest, &score); printf("NBEST %d: %s (%d)\n", n, hyp, score); for (seg = ps_nbest_seg(nbest); seg; seg = ps_seg_next(seg)) { char const *word; int sf, ef; word = ps_seg_word(seg); ps_seg_frames(seg, &sf, &ef); printf("%s %d %d\n", word, sf, ef); } } if (nbest) ps_nbest_free(nbest); ps_free(ps); cmd_ln_free_r(config); return 0; }
static void acoustic_processor(context_t *ctx, srs_srec_utterance_t *utt, srs_srec_candidate_t *cands, srs_srec_candidate_t **sorted) { filter_buf_t *filtbuf; decoder_set_t *decset; decoder_t *dec; logmath_t *lmath; const char *uttid; const char *hyp; int32 score; double prob; ps_nbest_t *nb; ps_seg_t *seg; int32_t frlen; int32 start, end; size_t ncand; srs_srec_candidate_t *cand; srs_srec_token_t *tkn; int32_t length; if (!ctx || !(filtbuf = ctx->filtbuf) || !(decset = ctx->decset) || !(dec = decset->curdec)) return; frlen = filtbuf->frlen; lmath = ps_get_logmath(dec->ps); uttid = "<unknown>"; hyp = ps_get_hyp(dec->ps, &score, &uttid); prob = logmath_exp(lmath, score); length = 0; if (prob < 0.00000001) prob = 0.00000001; for (nb = ps_nbest(dec->ps, 0,-1, NULL,NULL), ncand = 0; nb != NULL; nb = ps_nbest_next(nb)) { if (ncand >= CANDIDATE_MAX-1) { break; ps_nbest_free(nb); } if ((seg = ps_nbest_seg(nb, &score))) { while (seg && strcmp(ps_seg_word(seg), "<s>")) seg = ps_seg_next(seg); if (!seg) continue; ps_seg_frames(seg, &start, &end); cand = cands + ncand; cand->score = logmath_exp(lmath, score) / prob; cand->ntoken = 0; length = 0; while ((seg = ps_seg_next(seg))) { if ((hyp = ps_seg_word(seg))) { if (!strcmp(hyp, "</s>") || cand->ntoken >= CANDIDATE_TOKEN_MAX) { ncand++; //memset(cand+1, 0, sizeof(srs_srec_candidate_t)); ps_seg_frames(seg, &start, &end); ps_seg_free(seg); //printf("hyp=</s> ncand=%d\n", ncand); length = (end + 1) * frlen; break; } else if (!strcmp(hyp, "<sil>")) { ps_seg_frames(seg, &start, &end); //printf("hyp=<sil> skip it\n"); } else { tkn = cand->tokens + cand->ntoken++; tkn->token = tknbase(hyp); ps_seg_frames(seg, &start, &end); tkn->start = start * frlen; tkn->end = (end + 1) * frlen; //printf("hyp=%s (%d, %d) tkn count %d\n", // tkn->token, tkn->start,tkn->end, cand->ntoken); } } } /* while seg */ if (!seg && cand->ntoken > 0) { ncand++; cand->score *= 0.9; /* some penalty */ //memset(cand+1, 0, sizeof(srs_srec_candidate_t)); } if (!length) { tkn = cand->tokens + (cand->ntoken - 1); length = tkn->end; } } } /* for nb */ memset(cand+1, 0, sizeof(srs_srec_candidate_t)); utt->id = uttid; utt->score = prob; //utt->length = length; utt->length = filtbuf->len; utt->ncand = candidate_sort(cands, sorted); utt->cands = sorted; }