📄 fsg_search.c
字号:
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- *//* ==================================================================== * Copyright (c) 1999-2004 Carnegie Mellon University. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ==================================================================== * *//* * fsg_search.c -- Search structures for FSM decoding. * * ********************************************** * CMU ARPA Speech Project * * Copyright (c) 2004 Carnegie Mellon University. * ALL RIGHTS RESERVED. * ********************************************** * * HISTORY * * 18-Feb-2004 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon * Started. */#include <stdio.h>#include <string.h>#include <assert.h>#include <err.h>#include <ckd_alloc.h>#include <cmd_ln.h>#include "search.h"#include "phone.h"#include "fsg_search.h"#include "senscr.h"#include "kb.h"#include "fbs.h"#include "dict.h"#include "log.h"#define FSG_SEARCH_IDLE 0#define FSG_SEARCH_BUSY 1/* Turn this on for detailed debugging dump */#define __FSG_DBG__ 0#define __FSG_DBG_CHAN__ 0fsg_search_t *fsg_search_init(word_fsg_t * fsg){ fsg_search_t *search; float32 lw; int32 pip, wip; search = (fsg_search_t *) ckd_calloc(1, sizeof(fsg_search_t)); search->fsg = fsg; if (fsg) { search->fsglist = glist_add_ptr(NULL, (void *) fsg); search->lextree = fsg_lextree_init(fsg); } else { search->fsglist = NULL; search->lextree = NULL; } /* Intialize the search history object */ search->history = fsg_history_init(fsg); /* Initialize the active lists */ search->pnode_active = NULL; search->pnode_active_next = NULL; search->frame = -1; search->hyp = NULL; search->state = FSG_SEARCH_IDLE; /* Get search pruning parameters */ search_get_logbeams(&(search->beam_orig), &(search->pbeam_orig), &(search->wbeam_orig)); search->beam_factor = 1.0f; search->beam = search->beam_orig; search->pbeam = search->pbeam_orig; search->wbeam = search->wbeam_orig; /* LM related weights/penalties */ lw = cmd_ln_float32("-lw"); pip = (int32) (LOG(cmd_ln_float32("-pip")) * lw); wip = (int32) (LOG(cmd_ln_float32("-wip")) * lw); E_INFO("FSG(beam: %d, pbeam: %d, wbeam: %d; wip: %d, pip: %d)\n", search->beam_orig, search->pbeam_orig, search->wbeam_orig, wip, pip); return search;}word_fsg_t *fsg_search_fsgname_to_fsg(fsg_search_t * search, char *name){ gnode_t *gn; word_fsg_t *fsg; for (gn = search->fsglist; gn; gn = gnode_next(gn)) { fsg = (word_fsg_t *) gnode_ptr(gn); if (strcmp(name, word_fsg_name(fsg)) == 0) return fsg; } return NULL;}booleanfsg_search_add_fsg(fsg_search_t * search, word_fsg_t * fsg){ word_fsg_t *oldfsg; /* Check to make sure search is in a quiescent state */ if (search->state != FSG_SEARCH_IDLE) { E_ERROR("Attempt to switch FSG inside an utterance\n"); return FALSE; } /* Make sure no existing FSG has the same name as the given one */ oldfsg = fsg_search_fsgname_to_fsg(search, word_fsg_name(fsg)); if (oldfsg) { E_ERROR("FSG name '%s' already exists\n", word_fsg_name(fsg)); return FALSE; } search->fsglist = glist_add_ptr(search->fsglist, (void *) fsg); return TRUE;}booleanfsg_search_del_fsg(fsg_search_t * search, word_fsg_t * fsg){ gnode_t *gn, *prev, *next; word_fsg_t *oldfsg; /* Check to make sure search is in a quiescent state */ if (search->state != FSG_SEARCH_IDLE) { E_ERROR("Attempt to switch FSG inside an utterance\n"); return FALSE; } /* Search fsglist for the given fsg */ prev = NULL; for (gn = search->fsglist; gn; gn = next) { oldfsg = (word_fsg_t *) gnode_ptr(gn); next = gnode_next(gn); if (oldfsg == fsg) { /* Found the FSG to be deleted; remove it from fsglist */ if (prev) prev->next = next; else search->fsglist = next; myfree((char *) gn, sizeof(gnode_t)); /* If this was the currently active FSG, also delete other stuff */ if (search->fsg == fsg) { fsg_lextree_free(search->lextree); search->lextree = NULL; fsg_history_set_fsg(search->history, NULL); search->fsg = NULL; } E_INFO("Deleting FSG '%s'\n", word_fsg_name(fsg)); word_fsg_free(fsg); return TRUE; } else prev = gn; } E_WARN("FSG '%s' to be deleted not found\n", word_fsg_name(fsg)); return TRUE;}booleanfsg_search_del_fsg_byname(fsg_search_t * search, char *name){ word_fsg_t *fsg; fsg = fsg_search_fsgname_to_fsg(search, name); if (!fsg) { E_WARN("FSG name '%s' to be deleted not found\n", name); return TRUE; } else return fsg_search_del_fsg(search, fsg);}booleanfsg_search_set_current_fsg(fsg_search_t * search, char *name){ word_fsg_t *fsg; /* Check to make sure search is in a quiescent state */ if (search->state != FSG_SEARCH_IDLE) { E_ERROR("Attempt to switch FSG inside an utterance\n"); return FALSE; } fsg = fsg_search_fsgname_to_fsg(search, name); if (!fsg) { E_ERROR("FSG '%s' not known; cannot make it current\n", name); return FALSE; } /* Free the old lextree */ if (search->lextree) fsg_lextree_free(search->lextree); /* Allocate new lextree for the given FSG */ search->lextree = fsg_lextree_init(fsg); /* Inform the history module of the new fsg */ fsg_history_set_fsg(search->history, fsg); search->fsg = fsg; return TRUE;}voidfsg_search_free(fsg_search_t * search){ E_FATAL("NOT IMPLEMENTED\n");}voidfsg_search_sen_active(fsg_search_t * search){ gnode_t *gn; fsg_pnode_t *pnode; CHAN_T *hmm; sen_active_clear(); for (gn = search->pnode_active; gn; gn = gnode_next(gn)) { pnode = (fsg_pnode_t *) gnode_ptr(gn); hmm = fsg_pnode_hmmptr(pnode); assert(hmm->active == search->frame); hmm_sen_active(hmm); } sen_active_flags2list(); search->n_sen_eval += n_senone_active;}/* * Evaluate all the active HMMs. * (Executed once per frame.) */voidfsg_search_hmm_eval(fsg_search_t * search){ gnode_t *gn; fsg_pnode_t *pnode; CHAN_T *hmm; int32 bestscore; int32 n; bestscore = (int32) 0x80000000; if (!search->pnode_active) { E_ERROR("Frame %d: No active HMM!!\n", search->frame); return; } for (n = 0, gn = search->pnode_active; gn; gn = gnode_next(gn), n++) { pnode = (fsg_pnode_t *) gnode_ptr(gn); hmm = fsg_pnode_hmmptr(pnode); assert(hmm->active == search->frame);#if __FSG_DBG__ E_INFO("pnode(%08x) active @frm %5d\n", (int32) pnode, search->frame);#if __FSG_DBG_CHAN__ chan_dump(hmm, search->frame, stdout);#endif#endif chan_v_eval(hmm);#if __FSG_DBG_CHAN__ E_INFO("pnode(%08x) after eval @frm %5d\n", (int32) pnode, search->frame); chan_dump(hmm, search->frame, stdout);#endif if (bestscore < hmm->bestscore) bestscore = hmm->bestscore; }#if __FSG_DBG__ E_INFO("[%5d] %6d HMM; bestscr: %11d\n", search->frame, n, bestscore);#endif search->n_hmm_eval += n; /* Adjust beams if #active HMMs larger than absolute threshold */ if (n > cmd_ln_int32("-maxhmmpf")) { /* * Too many HMMs active; reduce the beam factor applied to the default * beams, but not if the factor is already at a floor (0.1). */ if (search->beam_factor > 0.1) { /* Hack!! Hardwired constant 0.1 */ search->beam_factor *= 0.9f; /* Hack!! Hardwired constant 0.9 */ search->beam = (int32) (search->beam_orig * search->beam_factor); search->pbeam = (int32) (search->pbeam_orig * search->beam_factor); search->wbeam = (int32) (search->wbeam_orig * search->beam_factor); } } else { search->beam_factor = 1.0f; search->beam = search->beam_orig; search->pbeam = search->pbeam_orig; search->wbeam = search->wbeam_orig; } if (n > fsg_lextree_n_pnode(search->lextree)) E_FATAL("PANIC! Frame %d: #HMM evaluated(%d) > #PNodes(%d)\n", search->frame, n, fsg_lextree_n_pnode(search->lextree)); search->bestscore = bestscore;}static voidfsg_search_pnode_trans(fsg_search_t * search, fsg_pnode_t * pnode){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -