/**************************************************************************/ /* File: grammar.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 class that outlines the language model structure used in the speech recognizer ========================================================================*/ /*------------------------------------------------------------------------ system and ISIP include files ------------------------------------------------------------------------*/ #include "grammar.h" /*------------------------------------------------------------------------ forward declaration of classes ------------------------------------------------------------------------*/ class Linked_List; class Gram_State; class Trans_State; class Grammar; /*======================================================================== Grammar function definitions for the grammar class ========================================================================*/ /*--------------------------- constructors and destructor ---------------------------*/ // default constructor // Grammar :: Grammar () { // create the basis for the states linked list // states_ptr = new Gram_State (); } // destructor // Grammar :: ~Grammar () { // free all memory // delete states_ptr; } // get the states pointer // Gram_State *Grammar :: get_states_ptr_cc () { return (states_ptr); } Gram_State *Grammar :: get_start_state_cc () { Gram_State *last_state_ptr; last_state_ptr = states_ptr->get_next_state_cc (); while (last_state_ptr->get_next_state_cc () != NULL) last_state_ptr = last_state_ptr->get_next_state_cc (); return (last_state_ptr); } /*--------------------- input / output method ---------------------*/ // read the grammar in from a data file // void Grammar :: read_grammar_cc (FILE *gram_file_ptr) { // dummy variables // String data_string = new char[10]; String temp_string = ""; float_8 prob_trans = 0; Gram_State *curr_state = NULL; Gram_State *tran_state = NULL; // read data strings from the input file and assign suitably // while (fscanf (gram_file_ptr, "%s", data_string) != EOF) { // check if it is a current state // if (data_string[0] == '$') { temp_string = data_string + 1; // create a new state // curr_state = states_ptr->add_state_cc (temp_string); // if state already exists make it the current state // if (curr_state == NULL) curr_state = states_ptr->find_state_cc (temp_string); } // now fill in all possible transitions // if (strcmp (data_string, "-->") == 0) { // read the transition-to state // fscanf (gram_file_ptr, "%s", data_string); // increment pointer to remove the identifier // temp_string = data_string + 1; // create new state if it doesn't exist already // tran_state = states_ptr->add_state_cc (temp_string); // if it already exists make it the trans_state // if (tran_state == NULL) tran_state = states_ptr->find_state_cc (temp_string); // now read the score of this transition // fscanf (gram_file_ptr, "%s", data_string); prob_trans = atof (data_string); // add this to the transition linked list from the current state // curr_state->add_transition_state_cc (tran_state, prob_trans); } } } // add a new state to the grammar // Gram_State *Grammar :: add_state_cc (String state_name) { // create a new state and add it to state list // return (states_ptr->add_state_cc (state_name)); } // get the score of a specified transition // float_8 Grammar :: get_trans_score_cc (String from_state_name, String to_state_name) { // get to the from-state // Gram_State *curr_state = states_ptr->find_state_cc (from_state_name); // get the score of the transition and return it // return (curr_state->get_transition_score_cc (to_state_name)); } // print the grammar to output file // void Grammar :: print_grammar_cc (FILE *outFile) { // dummy variables // String state_name; Gram_State *index_ptr, *last_state_ptr; Trans_State *trans_ptr, *last_trans_ptr; last_state_ptr = states_ptr->get_next_state_cc (); while (last_state_ptr->get_next_state_cc () != NULL) last_state_ptr = last_state_ptr->get_next_state_cc (); // loop through the states // for (index_ptr = last_state_ptr; index_ptr->get_prev_state_cc () != NULL; index_ptr = index_ptr->get_prev_state_cc ()) { // write the state symbol // state_name = index_ptr->get_state_name_cc (); fprintf (outFile, "\n$%s", state_name); last_trans_ptr = index_ptr->get_transition_cc (); // go through the transitions if there are any // if (last_trans_ptr != NULL) { while (last_trans_ptr->get_next_transition_cc () != NULL) last_trans_ptr = last_trans_ptr->get_next_transition_cc (); // now write all possible transitions // for (trans_ptr = last_trans_ptr; trans_ptr->get_prev_transition_cc () != NULL; trans_ptr = (Trans_State *) trans_ptr->get_prev_transition_cc ()) { // find the destination state // state_name = trans_ptr->get_state_name_cc (); // write the state name and the score // fprintf (outFile, "\t\t --> "); fprintf (outFile, " %s ", state_name); fprintf (outFile, "\t %f\n", trans_ptr->get_transition_score_cc ()); } } } } /*------ void Grammar :: print_grammar_cc (FILE *outFile) { // dummy variables // String state_name; Gram_State *index_ptr; Trans_State *trans_ptr; // loop through the states // for (index_ptr = states_ptr->get_next_state_cc (); index_ptr != NULL; index_ptr = index_ptr->get_next_state_cc ()) { // write the state symbol // state_name = index_ptr->get_state_name_cc (); fprintf (outFile, "\n$%s", state_name); // now write all possible transitions // for (trans_ptr = index_ptr->get_transition_cc (); trans_ptr != NULL; trans_ptr = (Trans_State *) trans_ptr->get_next_transition_cc ()) { // find the destination state // state_name = trans_ptr->get_state_name_cc (); // write the state name and the score // fprintf (outFile, "\t\t --> "); fprintf (outFile, " %s ", state_name); fprintf (outFile, "\t %f\n", trans_ptr->get_transition_score_cc ()); } } } --------*/