/************************************************************************/

//Please read these revised comments about how the prototypes of

//     the three function to implement in model.cpp for Programming #1

/************************************************************************/

 

/************************************************************************/

//Calculate and return the probability of charGenerated actually typed

//given charOfTheState as the underlying cognitive state.

/************************************************************************/

double prCharGivenCharOfState(char charGenerated, char charOfTheState)

{   // KEYBOARD STATE

    // CharGenerated = What we actually touched (typed)

    // CharOfTheState = What we want to type in our mind (our cognitive state)

 

       //**************************************************

       //Replace the following with your own implementation

       //**************************************************

       return 0;

}

 

 

/************************************************************************/

//Calculate for each cognitive state excluding the special states I and F,

//     the probability of that cognitive state being the first cognitive state

//     after the transition out of the special state I.

//Note that the value of the parameter sizeOfTable should be

//     exactly the number characters in the word.

//Store these prbabilities in the prTable array.

/************************************************************************/

void getPrTableForPossibleInitialStates(double prTable[], int sizeOfTable)

{

       //It is a word of sizeOfTable characters:

       //     i.e. the number of character states is sizeOfTable.

       //     let's index these characters from 0 to sizeOfTable-1.

       //

 

       //First calculate the sum of ratios of probabilities of

       //     going from the special I state into these character states.

       //This allows you to calculate the scaling factor to determine the probabilities.

 

       

       //Second, for each character state calculate the probability

       //     transitioning from the special I state into the character state.

 

       //**************************************************

       //Replace the following with your own implementation

       //**************************************************

 

 

}

 

 

 

 

/************************************************************************/

//Calculate for each actual cognitive state for a word

//     (excluding the special I state),

//     the probability of that cognitive state being the next cognitive state

//     given that currentState is the index of the current state.

//Note that the value of the parameter sizeOfTable should be

//     1 + the number characters in the word,

//Store these probabilities in the transitionPrTable[] array.

/************************************************************************/

void getPrTableForPossibleNextStates

(double transitionPrTable[], int sizeOfTable, int currentState)

{

       //We are working on a word of sizeOfTable-1 characters:

       //     i.e. the number of character states is sizeOfTable-1.

       //Index these character states from 0 to sizeOfTable-2 respectively, while

       //     the index of the special final state F is sizeOfTable-1.

       //currentState is the index of the current state in the word

 

       //First calculate the sum of ratios of probabilities of

       //     going from the current state into the other down-stream states down in word

       //     including all the down-stream character states and the

       //     special F final state.

       //This allows you to calculate the scaling factor to determine the probabilities.

        

       //Second, for each state (excluding the special I state)

       //     calculate the probability of

       //     transitioning from the current state into the character state

       //     and store the probability into the table.

 

 

       //**************************************************

       //Replace the following with your own implementation

       //**************************************************

 

}

 

 

 

 

/************************************************************************/

//     Please also read and check how these three function above

//           are called in the following demo functions in demo.cpp

/************************************************************************/

 

 

 

/************************************************************************/

/************************************************************************/

void demoOfKeyboardModel()

{

       cout << endl << endl

              << "*********************************************************" << endl

              << "Test the correctness of the keyboard model implementation, and" << endl

              << "show the probabilities of generating (touching) various characters" << endl

              << "when mentally you are in a state of trying to type something." << endl

              << "*********************************************************" << endl;

       cout<<endl<<"Press space to continue."<<endl;

 

       _getch();

       char asciiMap[] = "abcdefghijklmnopqrstuvwxyz";

       for (int i=0; i< 26; i++)

       {double prSum;

        prSum = 0;

        cout << "Probability distribution regarding " << endl

               << "all possible resulting characters when trying to type "

               << asciiMap[i] << endl;

        for (int j=0; j< 26; j++)

       {     

                    cout << '\t'

                           << "Pr[Char=" << asciiMap[j] << "|State=" << asciiMap[i] << "]="

                           << prCharGivenCharOfState(asciiMap[j], asciiMap[i]) << endl;

                    prSum += prCharGivenCharOfState(asciiMap[j], asciiMap[i]);

       }

        cout << "The sum of probability distribution over " << endl

               << "all possible resulting characters when trying to type \'" << endl

               << asciiMap[i]

               << "\' has been verified as "

               << prSum  << endl << endl;

 

        cout<<endl<<"Press space to continue."<<endl;            

        _getch();

 

       }

       cout << endl << endl ;

}

 

 

 

/************************************************************************/

/************************************************************************/

void demoInitialProbability()

{

      

       cout << endl << endl

              << "*********************************************************" << endl

              << "Consider the 3-character word \"his\" "<< endl

              << "Demonstrate the probability distribution over the possible cognitive states" << endl

              << "after the transition from the special initial state I."  << endl

              << "*********************************************************" << endl;

       cout<<endl<<"Press space to continue."<<endl;      

       _getch();

 

       int sizeOfTable = strlen("his");

       int currentState;

       double prSum = 0;

       double initialPrTable[3];

       getPrTableForPossibleInitialStates(initialPrTable,3);

 

       string word = "his";

       for (currentState=0; currentState < sizeOfTable; currentState++)

       {      cout << '\t'

                           << "Pr[State=" << word[currentState]  << "]="

                            << initialPrTable[currentState] << endl;

                    prSum += initialPrTable[currentState];

       }

 

       cout << "The sum of probability distribution over " << endl

              << "all possible states after the first transition" << endl

              << "has been verified as " << prSum  << endl << endl;

       cout<<endl<<"Press space to continue." << endl;           

       _getch();

 

}

 

 

/************************************************************************/

/************************************************************************/

void demoTransitionProbability()

{

 

       cout << endl << endl

              << "*********************************************************" << endl

              << "Consider the 3-character word \"his\" "<< endl

              << "Show the transition probabilities when spelling the word." << endl

              << "*********************************************************" << endl

              << "Demonstrate transition probabilities from current state to the next state."  << endl

              << "We use special character \'_\' to represent the special final state F."  << endl

              << "*********************************************************" << endl; 

       cout<<endl<<"Press space to continue."<<endl;      

       _getch();

 

       //One more example

       int sizeOfSpace = strlen("his")+1;

       string word="his_";

       for (int currentState=0; currentState< sizeOfSpace-1; currentState++)

       {      double prSum, transitionPrTable[4];

             cout << "Probability distribution over " << endl

                    << "all possible next states given that the current state is "

                    << word[currentState] << endl;

 

             prSum = 0;

       //nextStatePrTableGivenCurrentState(currentState,transitionPrTable,6);

             getPrTableForPossibleNextStates

             (transitionPrTable, 4, currentState);

 

             for (int nextState=0; nextState< sizeOfSpace; nextState++)

             {      cout << '\t'

                           << "Pr[NextState=" << word[nextState] << "|CurrentState=" << word[currentState] << "]="

                           << transitionPrTable[nextState] << endl;

                    prSum += transitionPrTable[nextState];

             }

 

             cout << "The sum of probabilities over " << endl

               << "all possible next states given that CurrentState="

               << word[currentState] << endl

               << "is verified as "

               << prSum  << endl << endl;

 

             cout<<endl<<"Press space to continue."<<endl;      

             _getch();

       }

 

 

       //One more example

       cout << endl << endl

              << "*********************************************************" << endl

              << "Consider the 5-character word \"right\" "<< endl

              << "Show the transition probabilities when spelling the word." << endl

              << "*********************************************************" << endl

              << "Demonstrate transition probabilities from current state to the next state."  << endl

              << "We use special character \'_\' to represent the special final state F."  << endl

              << "*********************************************************" << endl; 

       cout<<endl<<"Press space to continue."<<endl;      

       _getch();

       sizeOfSpace = strlen("right")+1;

       word="right_";

       for (int currentState=0; currentState< sizeOfSpace-1; currentState++)

       {      double prSum, transitionPrTable[6];

             cout << "Probability distribution over " << endl

                    << "all possible next states given that the current state is "

                    << word[currentState] << endl;

 

             prSum = 0;

       //nextStatePrTableGivenCurrentState(currentState,transitionPrTable,6);

             getPrTableForPossibleNextStates

             (transitionPrTable, 6, currentState);

 

             for (int nextState=0; nextState< sizeOfSpace; nextState++)

             {      cout << '\t'

                           << "Pr[NextState=" << word[nextState] << "|CurrentState=" << word[currentState] << "]="

                           << transitionPrTable[nextState] << endl;

                    prSum += transitionPrTable[nextState];

             }

 

             cout << "The sum of probabilities over " << endl

               << "all possible next states given that CurrentState="

               << word[currentState] << endl

               << "is verified as "

               << prSum  << endl << endl;

 

             cout<<endl<<"Press space to continue."<<endl;      

             _getch();

       }

 

}