43 #include <sphinxbase/err.h> 44 #include <sphinxbase/ckd_alloc.h> 45 #include <sphinxbase/strfuncs.h> 46 #include <sphinxbase/pio.h> 47 #include <sphinxbase/cmd_ln.h> 50 #include "kws_search.h" 53 #define hmm_is_active(hmm) ((hmm)->frame > 0) 54 #define kws_nth_hmm(keyphrase,n) (&((keyphrase)->hmms[n])) 86 itor->
base.
word = detection->keyphrase;
87 itor->
base.
sf = detection->sf;
88 itor->
base.
ef = detection->ef;
99 gnode_t *detect_head = gnode_next(itor->
detection);
101 detect_head = gnode_next(detect_head);
124 gnode_t *detect_head = kwss->
detections->detect_list;
127 detect_head = gnode_next(detect_head);
132 itor = (
kws_seg_t *)ckd_calloc(1,
sizeof(*itor));
133 itor->
base.
vt = &kws_segfuncs;
165 for (i = 0; i < kwss->
n_pl; i++)
169 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
171 for (i = 0; i < keyphrase->n_hmms; i++) {
172 if (hmm_is_active(kws_nth_hmm(keyphrase, i)))
183 kws_search_hmm_eval(
kws_search_t * kwss, int16
const *senscr)
192 for (i = 0; i < kwss->
n_pl; ++i) {
201 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
203 for (i = 0; i < keyphrase->n_hmms; i++) {
204 hmm_t *hmm = kws_nth_hmm(keyphrase, i);
206 if (hmm_is_active(hmm)) {
230 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
232 for (i = 0; i < keyphrase->n_hmms; i++) {
233 hmm_t *hmm = kws_nth_hmm(keyphrase, i);
234 if (hmm_is_active(hmm) && hmm_bestscore(hmm) < thresh)
247 hmm_t *pl_best_hmm = NULL;
253 for (i = 0; i < kwss->
n_pl; i++)
255 best_out_score = hmm_out_score(&kwss->
pl_hmms[i]);
256 pl_best_hmm = &kwss->
pl_hmms[i];
264 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
268 if (keyphrase->n_hmms < 1)
271 last_hmm = kws_nth_hmm(keyphrase, keyphrase->n_hmms - 1);
273 if (hmm_is_active(last_hmm)
276 if (hmm_out_score(last_hmm) - hmm_out_score(pl_best_hmm)
277 >= keyphrase->threshold) {
279 int32 prob = hmm_out_score(last_hmm) - hmm_out_score(pl_best_hmm) - KWS_MAX;
280 kws_detections_add(kwss->
detections, keyphrase->word,
281 hmm_out_history(last_hmm),
283 hmm_out_score(last_hmm));
289 for (i = 0; i < kwss->
n_pl; i++) {
291 hmm_in_score(&kwss->
pl_hmms[i])) {
293 hmm_out_score(pl_best_hmm) + kwss->
plp,
294 hmm_out_history(pl_best_hmm), kwss->
frame + 1);
299 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
302 if (keyphrase->n_hmms < 1)
305 for (i = keyphrase->n_hmms - 1; i > 0; i--) {
306 hmm_t *pred_hmm = kws_nth_hmm(keyphrase, i - 1);
307 hmm_t *hmm = kws_nth_hmm(keyphrase, i);
309 if (hmm_is_active(pred_hmm)) {
310 if (!hmm_is_active(hmm)
314 hmm_out_history(pred_hmm), kwss->
frame + 1);
320 hmm_in_score(kws_nth_hmm(keyphrase, 0)))
321 hmm_enter(kws_nth_hmm(keyphrase, 0), hmm_out_score(pl_best_hmm),
327 kws_search_read_list(
kws_search_t *kwss,
const char* keyfile)
333 if ((list_file = fopen(keyfile,
"r")) == NULL) {
334 E_ERROR_SYSTEM(
"Failed to open keyphrase file '%s'", keyfile);
341 for (li = lineiter_start_clean(list_file); li; li = lineiter_next(li)) {
351 end = strlen(line) - 1;
353 if (line[end] ==
'/') {
354 while (line[begin] !=
'/' && begin > 0)
358 keyphrase->threshold = (int32) logmath_log(kwss->base.
acmod->
lmath, atof_c(line + begin + 1))
364 keyphrase->word = ckd_salloc(line);
374 kws_search_init(
const char *name,
375 const char *keyphrase,
381 ps_search_init(ps_search_base(kwss), &kws_funcs, PS_SEARCH_TYPE_KWS, name, config, acmod, dict,
387 (int32) logmath_log(acmod->
lmath,
388 cmd_ln_float64_r(config,
392 (int32) logmath_log(acmod->
lmath,
393 cmd_ln_float32_r(config,
398 (int32) logmath_log(acmod->
lmath,
399 cmd_ln_float64_r(config,
400 "-kws_threshold")) >>
403 kwss->
delay = (int32) cmd_ln_int32_r(config,
"-kws_delay");
405 E_INFO(
"KWS(beam: %d, plp: %d, default threshold %d, delay %d)\n",
409 if (kws_search_read_list(kwss, keyfile) < 0) {
410 E_ERROR(
"Failed to create kws search\n");
411 kws_search_free(ps_search_base(kwss));
417 k->word = ckd_salloc(keyphrase);
422 if (kws_search_reinit(ps_search_base(kwss),
423 ps_search_dict(kwss),
424 ps_search_dict2pid(kwss)) < 0) {
425 ps_search_free(ps_search_base(kwss));
429 ptmr_init(&kwss->
perf);
431 return ps_search_base(kwss);
443 n_speech = (double)kwss->n_tot_frame
444 / cmd_ln_int32_r(ps_search_config(kwss),
"-frate");
446 E_INFO(
"TOTAL kws %.2f CPU %.3f xRT\n",
447 kwss->
perf.t_tot_cpu,
448 kwss->
perf.t_tot_cpu / n_speech);
449 E_INFO(
"TOTAL kws %.2f wall %.3f xRT\n",
450 kwss->
perf.t_tot_elapsed,
451 kwss->
perf.t_tot_elapsed / n_speech);
460 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
462 ckd_free(keyphrase->hmms);
463 ckd_free(keyphrase->word);
475 int32 wid, pronlen, in_dict;
476 int32 n_hmms, n_wrds;
481 int32 silcipid = bin_mdef_silphone(mdef);
499 for (i = 0; i < kwss->
n_pl; ++i)
506 for (i = 0; i < kwss->
n_pl; ++i) {
509 bin_mdef_pid2ssid(search->
acmod->
mdef, i),
510 bin_mdef_pid2tmatid(search->
acmod->
mdef, i));
513 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
517 tmp_keyphrase = (
char *) ckd_salloc(keyphrase->word);
518 n_wrds = str2words(tmp_keyphrase, NULL, 0);
519 wrdptr = (
char **) ckd_calloc(n_wrds,
sizeof(*wrdptr));
520 str2words(tmp_keyphrase, wrdptr, n_wrds);
525 for (i = 0; i < n_wrds; i++) {
528 E_ERROR(
"Word '%s' in phrase '%s' is missing in the dictionary\n", wrdptr[i], keyphrase->word);
532 pronlen = dict_pronlen(dict, wid);
538 ckd_free(tmp_keyphrase);
544 ckd_free(keyphrase->hmms);
545 keyphrase->hmms = (
hmm_t *) ckd_calloc(n_hmms,
sizeof(
hmm_t));
546 keyphrase->n_hmms = n_hmms;
550 for (i = 0; i < n_wrds; i++) {
552 pronlen = dict_pronlen(dict, wid);
553 for (p = 0; p < pronlen; p++) {
558 pronlen > 1 ?
dict_pron(dict, wid, 1) : silcipid;
559 ssid = dict2pid_ldiph_lc(d2p, ci, rc, silcipid);
561 else if (p == pronlen - 1) {
565 int j = rssid->
cimap[silcipid];
566 ssid = rssid->
ssid[j];
572 tmatid = bin_mdef_pid2tmatid(mdef, ci);
580 ckd_free(tmp_keyphrase);
599 for (i = 0; i < kwss->
n_pl; ++i) {
605 ptmr_reset(&kwss->
perf);
606 ptmr_start(&kwss->
perf);
612 kws_search_step(
ps_search_t * search,
int frame_idx)
620 kws_search_sen_active(kwss);
626 kws_search_hmm_eval(kwss, senscr);
629 kws_search_hmm_prune(kwss);
632 kws_search_trans(kwss);
646 kwss->n_tot_frame += kwss->
frame;
649 ptmr_stop(&kwss->
perf);
651 cf = ps_search_acmod(kwss)->output_frame;
653 double n_speech = (double) (cf + 1)
654 / cmd_ln_int32_r(ps_search_config(kwss),
"-frate");
655 E_INFO(
"kws %.2f CPU %.3f xRT\n",
656 kwss->
perf.t_cpu, kwss->
perf.t_cpu / n_speech);
657 E_INFO(
"kws %.2f wall %.3f xRT\n",
658 kwss->
perf.t_elapsed, kwss->
perf.t_elapsed / n_speech);
665 kws_search_hyp(
ps_search_t * search, int32 * out_score)
689 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn))
693 line = (
char *)ckd_calloc(len,
sizeof(*line));
694 for (gn = kwss->
keyphrases; gn; gn = gnode_next(gn)) {
696 memcpy(&line[c], str, strlen(str));
Internal implementation of PocketSphinx decoder.
frame_idx_t last_frame
Last frame to raise the detection.
Base structure for search module.
void hmm_init(hmm_context_t *ctx, hmm_t *hmm, int mpx, int ssid, int tmatid)
Populate a previously-allocated HMM structure, allocating internal data.
POCKETSPHINX_EXPORT s3wid_t dict_wordid(dict_t *d, const char *word)
Return word id for given word string if present.
glist_t keyphrases
Keyphrases to spot.
void ps_search_base_reinit(ps_search_t *search, dict_t *dict, dict2pid_t *d2p)
Re-initialize base structure with new dictionary.
acmod_t * acmod
Acoustic model.
An individual HMM among the HMM search space.
gnode_t * detection
Keyphrase detection correspondent to segment.
uint8 *** tp
The transition matrices; kept in the same scale as acoustic scores; tp[tmatid][from-state][to-state]...
int32 plp
Phone loop probability.
ps_segfuncs_t * vt
V-table of seg methods.
int32 def_threshold
default threshold for p(hyp)/p(altern) ratio
logmath_t * lmath
Log-math computation.
uint16 ** sseq
Unique senone sequences (2D array built at load time)
void hmm_deinit(hmm_t *hmm)
Free an HMM structure, releasing internal data (but not the HMM structure itself).
int32 lscr
Language model score.
void acmod_activate_hmm(acmod_t *acmod, hmm_t *hmm)
Activate senones associated with an HMM.
#define BAD_S3WID
Dictionary word id.
Segmentation "iterator" for KWS history.
int32 bestscore
For beam pruning.
int32 prob
Log posterior probability.
char const * word
Word string (pointer into dictionary hash)
ps_search_t * search
Search object from whence this came.
void ps_search_init(ps_search_t *search, ps_searchfuncs_t *vt, const char *type, const char *name, cmd_ln_t *config, acmod_t *acmod, dict_t *dict, dict2pid_t *d2p)
Initialize base structure.
hmm_t * pl_hmms
Phone loop hmms - hmms of CI phones.
int32 hmm_vit_eval(hmm_t *hmm)
Viterbi evaluation of given HMM.
#define dict2pid_rssid(d, ci, lc)
Access macros; not designed for arbitrary use.
hmm_context_t * hmmctx
HMM context.
uint8 compallsen
Compute all senones?
int32 delay
Delay to wait for best detection score.
hmm_context_t * hmm_context_init(int32 n_emit_state, uint8 **const *tp, int16 const *senscore, uint16 *const *sseq)
Create an HMM context.
void ps_search_base_free(ps_search_t *search)
Free search.
#define WORST_SCORE
Large "bad" score.
tmat_t * tmat
Transition matrices.
int32 ascr
Acoustic score.
void hmm_enter(hmm_t *h, int32 score, int32 histid, int frame)
Enter an HMM with the given path score and history ID.
void acmod_clear_active(acmod_t *acmod)
Clear set of active senones.
ps_seg_t base
Base structure.
#define hmm_context_set_senscore(ctx, senscr)
Change the senone score array for a context.
#define SENSCR_SHIFT
Shift count for senone scores.
a structure for a dictionary.
Word graph structure used in bestpath/nbest search.
s3ssid_t dict2pid_internal(dict2pid_t *d2p, int32 wid, int pos)
Return the senone sequence ID for the given word position.
void hmm_clear(hmm_t *h)
Reset the states of the HMM to the invalid condition.
cross word triphone model structure
char * hyp_str
Current hypothesis string.
frame_idx_t frame
Frame index.
#define BETTER_THAN
Is one score better than another?
int32 n_pl
Number of CI phones.
void hmm_context_free(hmm_context_t *ctx)
Free an HMM context.
bin_mdef_t * mdef
Model definition.
Implementation of KWS search structure.
kws_detections_t * detections
Keyword spotting history.
V-table for search algorithm.
ptmr_t perf
Performance counter.
Base structure for hypothesis segmentation iterator.
s3cipid_t * cimap
Index into ssid[] above for each ci phone.
#define dict_pron(d, w, p)
The CI phones of the word w at position p.
Acoustic model structure.
float32 lwf
Language weight factor (for second-pass searches)
Building composite triphone (as well as word internal triphones) with the dictionary.
s3ssid_t * ssid
Senone Sequence ID list for all context ciphones.
frame_idx_t sf
Start frame.
int16 const * acmod_score(acmod_t *acmod, int *inout_frame_idx)
Score one frame of data.