/**************************************************************************/ /* File: gram_state.cc */ /**************************************************************************/ /* Project: Continuous Speech Recognition Search Algorithms */ /* Author: Neeraj Deshmukh, Aravind Ganapathiraju */ /* Class: EE 8993 Spring 1996 Date: March 27, 1996 */ /**************************************************************************/ /*======================================================================== This file defines the grammar state and state transitions classes that are derived from the generic linked list class that is used in the speech recognizer ========================================================================*/ /*------------------------------------------------------------------------ system and ISIP include files ------------------------------------------------------------------------*/ #include "gram_state.h" /*------------------------------------------------------------------------ forward declaration of classes ------------------------------------------------------------------------*/ class Linked_List; class Gram_State; class Trans_State; /*======================================================================== Gram_State function definitions for the grammar state class derived from the generic linked list class ========================================================================*/ /*--------------------------- constructors and destructor ---------------------------*/ // default constructor // Gram_State :: Gram_State (String out_name = "", Trans_State *trans_ptr = NULL) : Linked_List (trans_ptr) { // create members of the object // out_symbol_d = new char[strlen (out_name) + 1]; strcpy (out_symbol_d, out_name); // if the element pointer is still not set // if ((Trans_State *) element_ptr == NULL) element_ptr = new Trans_State (); } // destructor // Gram_State :: ~Gram_State () { // free all memory // delete out_symbol_d; } /*---------------------- input / output methods ----------------------*/ // get the name of this state // String Gram_State :: get_state_name_cc () { return (out_symbol_d); } void Gram_State :: set_state_name_cc (String out_name) { // delete existing string if any // delete out_symbol_d; out_symbol_d = NULL; // now copy the new name onto // out_symbol_d = new char[strlen (out_name) + 1]; strcpy (out_symbol_d, out_name); } double Gram_State :: get_transition_score_cc (String trans_state_name) { // get to the transition to the specified string and return score // return (((Trans_State *)element_ptr)->get_transition_score_cc (trans_state_name)); } void Gram_State :: add_transition_state_cc (Gram_State *trans_state, float_8 score) { // add this state to the linked list of transitions from the current state // ((Trans_State *)element_ptr)->add_transition_state_cc (trans_state, score); } Trans_State *Gram_State :: get_transition_cc () { // return the transition pointer for this state // return (((Trans_State*) element_ptr)->get_next_transition_cc ()); } /*------------------------- state linked list methods -------------------------*/ // get the next state in the sequence // Gram_State *Gram_State :: get_next_state_cc () { // return the next link in the list // return ((Gram_State *)next_link_ptr); } // get the previous state in the sequence // Gram_State *Gram_State :: get_prev_state_cc () { // return the next link in the list // return ((Gram_State *)prev_link_ptr); } // add a new state to the state sequence // Gram_State *Gram_State :: add_state_cc (String out_name, Trans_State *trans_ptr = NULL) { // check if this state already exists in list, do not duplicate it // if (find_state_cc (out_name) != NULL) return (NULL); // create a new link for the given state // Gram_State *new_state_ptr = new Gram_State (out_name, trans_ptr); // add this at the head of the list // new_state_ptr->next_link_ptr = next_link_ptr; new_state_ptr->prev_link_ptr = this; if (next_link_ptr != NULL) ((Gram_State *) next_link_ptr)->prev_link_ptr = new_state_ptr; next_link_ptr = new_state_ptr; // return the head of the linked list // return (new_state_ptr); } // return the state with the given symbol // Gram_State *Gram_State :: find_state_cc (String out_name) { // traverse the linked list looking for matching element // for (Gram_State *index_ptr = this; index_ptr != NULL; index_ptr = (Gram_State*)(index_ptr->next_link_ptr)) { // check for matching element // if (strcmp (index_ptr->out_symbol_d, out_name) == 0) { // return link if found match // return (index_ptr); } } // else return NULL // return (NULL); } /*======================================================================== Trans_State function definitions for the grammar state transition class derived from the generic linked list class ========================================================================*/ /*--------------------------- constructors and destructor ---------------------------*/ // default constructor // Trans_State :: Trans_State (Gram_State *state_ptr = NULL, float_8 score = 0) : Linked_List (state_ptr) { // create elements of the object // trans_score_d = score; } // destructor // Trans_State :: ~Trans_State () {} /*---------------------- input / output methods ----------------------*/ // get the name of the state this transition is towards // String Trans_State :: get_state_name_cc () { // return the name of the to_state // return (((Gram_State *) element_ptr)->get_state_name_cc ()); } // get the score of the transition to a given state // float_8 Trans_State :: get_transition_score_cc (String trans_state_name = NULL) { // if no string is specified then it is this transition // if (trans_state_name == NULL) return (trans_score_d); // else find the corresponding transition // return (find_transition_cc (trans_state_name)->trans_score_d); } // get the next transition in sequence // Trans_State *Trans_State :: get_next_transition_cc () { // return the next link in the list // return ((Trans_State *)next_link_ptr); } // get the previous transition in sequence // Trans_State *Trans_State :: get_prev_transition_cc () { // return the next link in the list // return ((Trans_State *) prev_link_ptr); } // find the transition ending at the specified state // Trans_State *Trans_State :: find_transition_cc (String trans_state_name) { // traverse the linked list looking for matching element // for (Trans_State *index_ptr = (Trans_State *)next_link_ptr; index_ptr != NULL; index_ptr = (Trans_State *)(index_ptr->next_link_ptr)) { // check for matching element // if (strcmp (((Gram_State *)(index_ptr->element_ptr))->get_state_name_cc (), trans_state_name) == 0) { // return link if found match // return (index_ptr); } } // else return NULL // return (NULL); } // add a transition to list // Trans_State *Trans_State :: add_transition_state_cc (Gram_State *state_ptr, float_8 score) { // check if this transition already exists // if (find_transition_cc (state_ptr->get_state_name_cc ()) != NULL) return (NULL); // create a new link for the specified transition // Trans_State *new_trans_ptr = new Trans_State (state_ptr, score); // add this at the head of the list // new_trans_ptr->next_link_ptr = next_link_ptr; new_trans_ptr->prev_link_ptr = this; if (next_link_ptr != NULL) ((Trans_State *)next_link_ptr)->prev_link_ptr = new_trans_ptr; next_link_ptr = new_trans_ptr; // return the head of the linked list // return (new_trans_ptr); }