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(keyword,n) (&((keyword)->hmms[n]))
80 itor->
base.
word = detection->keyphrase;
81 itor->
base.
sf = detection->sf;
82 itor->
base.
ef = detection->ef;
93 gnode_t *detect_head = gnode_next(itor->
detection);
95 detect_head = gnode_next(detect_head);
114 kws_search_seg_iter(
ps_search_t * search, int32 * out_score)
118 gnode_t *detect_head = kwss->
detections->detect_list;
121 detect_head = gnode_next(detect_head);
129 itor = (
kws_seg_t *)ckd_calloc(1,
sizeof(*itor));
130 itor->
base.
vt = &kws_segfuncs;
163 dict = ps_search_dict(kwss);
165 for (keyword_iter = 0; keyword_iter < kwss->
n_keyphrases; keyword_iter++) {
166 tmp_keyphrase = (
char *) ckd_salloc(kwss->
keyphrases[keyword_iter].word);
167 nwrds = str2words(tmp_keyphrase, NULL, 0);
168 wrdptr = (
char **) ckd_calloc(nwrds,
sizeof(*wrdptr));
169 str2words(tmp_keyphrase, wrdptr, nwrds);
170 for (i = 0; i < nwrds; i++) {
173 E_ERROR(
"The word '%s' is missing in the dictionary\n",
180 ckd_free(tmp_keyphrase);
194 for (i = 0; i < kwss->
n_pl; i++)
198 for (keyword_iter = 0; keyword_iter < kwss->
n_keyphrases; keyword_iter++) {
200 for (i = 0; i < keyword->n_hmms; i++) {
201 if (hmm_is_active(kws_nth_hmm(keyword, i)))
212 kws_search_hmm_eval(
kws_search_t * kwss, int16
const *senscr)
214 int32 i, keyword_iter;
220 for (i = 0; i < kwss->
n_pl; ++i) {
229 for (keyword_iter = 0; keyword_iter < kwss->
n_keyphrases; keyword_iter++) {
231 for (i = 0; i < keyword->n_hmms; i++) {
232 hmm_t *hmm = kws_nth_hmm(keyword, i);
234 if (hmm_is_active(hmm)) {
253 int32 thresh, i, keyword_iter;
257 for (keyword_iter = 0; keyword_iter < kwss->
n_keyphrases; keyword_iter++) {
259 for (i = 0; i < keyword->n_hmms; i++) {
260 hmm_t *hmm = kws_nth_hmm(keyword, i);
261 if (hmm_is_active(hmm) && hmm_bestscore(hmm) < thresh)
274 hmm_t *pl_best_hmm = NULL;
279 for (i = 0; i < kwss->
n_pl; i++)
281 best_out_score = hmm_out_score(&kwss->
pl_hmms[i]);
282 pl_best_hmm = &kwss->
pl_hmms[i];
290 for (keyword_iter = 0; keyword_iter < kwss->
n_keyphrases; keyword_iter++) {
295 last_hmm = kws_nth_hmm(keyword, keyword->n_hmms - 1);
296 if (hmm_is_active(last_hmm)
299 if (hmm_out_score(last_hmm) - hmm_out_score(pl_best_hmm)
300 >= keyword->threshold) {
302 int32 prob = hmm_out_score(last_hmm) - hmm_out_score(pl_best_hmm);
303 kws_detections_add(kwss->
detections, keyword->word,
304 hmm_out_history(last_hmm),
306 hmm_out_score(last_hmm));
312 for (i = 0; i < kwss->
n_pl; i++) {
314 hmm_in_score(&kwss->
pl_hmms[i])) {
316 hmm_out_score(pl_best_hmm) + kwss->
plp,
317 hmm_out_history(pl_best_hmm), kwss->
frame + 1);
322 for (keyword_iter = 0; keyword_iter < kwss->
n_keyphrases; keyword_iter++) {
324 for (i = keyword->n_hmms - 1; i > 0; i--) {
325 hmm_t *pred_hmm = kws_nth_hmm(keyword, i - 1);
326 hmm_t *hmm = kws_nth_hmm(keyword, i);
328 if (hmm_is_active(pred_hmm)) {
329 if (!hmm_is_active(hmm)
333 hmm_out_history(pred_hmm), kwss->
frame + 1);
339 hmm_in_score(kws_nth_hmm(keyword, 0)))
340 hmm_enter(kws_nth_hmm(keyword, 0), hmm_out_score(pl_best_hmm),
346 kws_search_read_list(
kws_search_t *kwss,
const char* keyfile)
353 if ((list_file = fopen(keyfile,
"r")) == NULL) {
354 E_ERROR_SYSTEM(
"Failed to open keyword file '%s'", keyfile);
360 for (li = lineiter_start(list_file); li; li = lineiter_next(li))
365 fseek(list_file, 0L, SEEK_SET);
368 for (li = lineiter_start(list_file), i=0; li; li = lineiter_next(li), i++) {
371 line = string_trim(li->buf, STRING_BOTH);
372 end = strlen(line) - 1;
375 if (line[end] ==
'/') {
376 while (line[begin] !=
'/' && begin > 0)
380 kwss->
keyphrases[i].threshold = (int32) logmath_log(kwss->base.
acmod->
lmath, atof_c(line + begin + 1))
391 kws_search_init(
const char *name,
392 const char *keyphrase,
398 ps_search_init(ps_search_base(kwss), &kws_funcs, PS_SEARCH_TYPE_KWS, name, config, acmod, dict,
404 (int32) logmath_log(acmod->
lmath,
405 cmd_ln_float64_r(config,
409 (int32) logmath_log(acmod->
lmath,
410 cmd_ln_float32_r(config,
415 (int32) logmath_log(acmod->
lmath,
416 cmd_ln_float64_r(config,
417 "-kws_threshold")) >>
420 kwss->
delay = (int32) cmd_ln_int32_r(config,
"-kws_delay");
422 E_INFO(
"KWS(beam: %d, plp: %d, default threshold %d, delay %d)\n",
426 if (kws_search_read_list(kwss, keyfile) < 0) {
427 E_ERROR(
"Failed to create kws search\n");
428 kws_search_free(ps_search_base(kwss));
435 kwss->
keyphrases[0].word = ckd_salloc(keyphrase);
439 if (!kws_search_check_dict(kwss)) {
440 kws_search_free(ps_search_base(kwss));
445 if (kws_search_reinit(ps_search_base(kwss),
446 ps_search_dict(kwss),
447 ps_search_dict2pid(kwss)) < 0) {
448 ps_search_free(ps_search_base(kwss));
452 return ps_search_base(kwss);
482 int32 n_hmms, n_wrds;
484 int i, j, p, keyword_iter;
487 int32 silcipid = bin_mdef_silphone(mdef);
504 for (i = 0; i < kwss->
n_pl; ++i)
511 for (i = 0; i < kwss->
n_pl; ++i) {
514 bin_mdef_pid2ssid(search->
acmod->
mdef, i),
515 bin_mdef_pid2tmatid(search->
acmod->
mdef, i));
518 for (keyword_iter = 0; keyword_iter < kwss->
n_keyphrases; keyword_iter++) {
522 tmp_keyphrase = (
char *) ckd_salloc(keyword->word);
523 n_wrds = str2words(tmp_keyphrase, NULL, 0);
524 wrdptr = (
char **) ckd_calloc(n_wrds,
sizeof(*wrdptr));
525 str2words(tmp_keyphrase, wrdptr, n_wrds);
529 for (i = 0; i < n_wrds; i++) {
531 pronlen = dict_pronlen(dict, wid);
537 ckd_free(keyword->hmms);
538 keyword->hmms = (
hmm_t *) ckd_calloc(n_hmms,
sizeof(
hmm_t));
539 keyword->n_hmms = n_hmms;
543 for (i = 0; i < n_wrds; i++) {
545 pronlen = dict_pronlen(dict, wid);
546 for (p = 0; p < pronlen; p++) {
551 pronlen > 1 ?
dict_pron(dict, wid, 1) : silcipid;
552 ssid = dict2pid_ldiph_lc(d2p, ci, rc, silcipid);
554 else if (p == pronlen - 1) {
558 int j = rssid->
cimap[silcipid];
559 ssid = rssid->
ssid[j];
565 tmatid = bin_mdef_pid2tmatid(mdef, ci);
573 ckd_free(tmp_keyphrase);
590 for (i = 0; i < kwss->
n_pl; ++i) {
599 kws_search_step(
ps_search_t * search,
int frame_idx)
607 kws_search_sen_active(kwss);
613 kws_search_hmm_eval(kwss, senscr);
616 kws_search_hmm_prune(kwss);
619 kws_search_trans(kwss);
633 kws_search_hyp(
ps_search_t * search, int32 * out_score,
634 int32 * out_is_final)
662 line = (
char *)ckd_calloc(len,
sizeof(*line));
665 memcpy(&line[c], keyword_str, strlen(keyword_str));
666 c += strlen(keyword_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.
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
Keyword 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.
kws_keyword_t * keyphrases
Keyphrases to spot.
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.
int n_keyphrases
Keyphrases amount.
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.
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.