Exemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            // General variables
            DateTime            timeStart = DateTime.Now;
            string              path      = context.Request.PhysicalApplicationPath;
            string              userid    = HttpContext.Current.User.Identity.Name;
            Tuple <string, int> imageInfo = new Tuple <string, int>("", 2);
            string              page      = "";

            // response to transmit back to caller
            string transResponse = "";


            // Variables important to the speaker's state
            string     audioFile         = path + "data\\userAudio\\blob.wav";
            string     transcript        = "";
            string     dialogueAct       = "";
            int        speakerSpoke      = 0;
            List <int> problemStep       = new List <int>();                                                                // [0] is problem, [1] is the step, and [2] is the image key loaded for this step, [3] is the answer pattern
            int        sessionid         = 1;                                                                               // ************************* FIX THIS ********
            int        nextstepanswerkey = 0;

            // Variables important to Nico's state
            Tuple <string, int, string> nicoResponse;                                                                           // string => Nico's response, int is the movement code, string contains whether Nico is 'answering', 'confirming', or 'not answering'

            try
            {
                problemStep = SQLProblemStepTracker.ReadProbStep(userid);                                                   // Get current step
                int    problem          = problemStep[0];
                int    step             = problemStep[1];
                int    probImg          = problemStep[2];
                int    answerKey        = problemStep[3];
                int    numAutoResponses = problemStep[5];
                int    numturns         = problemStep[6];
                string clickstep        = "none";
                int    newanswer        = 0;

/*
 *              if (step == 0)
 *              {
 *                  step = 1;
 *              }
 */
                // the transcript is the one get from the voice
                // "problem start" is the first transcript
                // if no transcript from the voice file generated, the auto response comes from js part, nothing in this place
                transcript = context.Request.Params["transcript"];    // Get transcript (if there is one)
                // Debug.WriteLine("di: transcript is : " + transcript);
                page = context.Request.Params["page_loc"];

                if (context.Request.Files.Count > 0)                                                                       // Write out audio file (if it's there)
                {
                    HttpFileCollection files = context.Request.Files;
                    //audioFile = writeFile(files, path, userid, timeStart);
                }
                if (transcript == "problem start")
                {
                    speakerSpoke = 0;
                    clickstep    = "problem start";
                    step         = 1;
                }
                else if (transcript == "no response") // TODO where does the "no response" generated, does that important?
                {
                    speakerSpoke      = 0;
                    clickstep         = "null";
                    numAutoResponses += 1;
                    if (numAutoResponses > 3)
                    {
                        transcript = "Triggered Speech";
                    }
                    else
                    {
                        transcript = "Triggered Speech " + numAutoResponses.ToString();
                    }
                }
                else if (transcript == "HELLO FIRST TIME")
                {
                    speakerSpoke = 0;
                    clickstep    = "hello nico start";
                }
                else if ((transcript != "") && (transcript != null))                                                             // Is there a transcript? If there is get the speaker's dialogue act
                {
                    dialogueAct  = estimateDialogueAct(transcript);
                    speakerSpoke = 1;                                                                                           // Set that the speaker actually spoke this turn
                }
                else
                {
                    speakerSpoke = 2;
                    transcript   = "transcript empty or null";
                }


                // TODO:  Simplify this - NicoResponseText should be MUCH simpler - no need for audio file or any of the audio processing
                if (SQLConditionGenderInfo.GetVoiceText(userid) == "text")
                {
                    Debug.WriteLine("di: xxx is text");
                    nicoResponse = ResponseGeneration.NicoResponseText(path, problemStep, speakerSpoke, transcript, timeStart, page, userid, audioFile);                 // Generate and initiate Nico's response
                    path         = nicoResponse.Item1;
                    if (path == "")
                    {
                        transResponse = "I'm sorry!  Can you please try again?";
                    }
                    else
                    {
                        transResponse = File.ReadAllText(path);
                    }
                }
                else
                {
                    Debug.WriteLine("di: xxx is not text");
                    Debug.WriteLine("di: current transcript: " + transcript);
                    nicoResponse = ResponseGeneration.NicoResponse(path, problemStep, speakerSpoke, transcript, timeStart, page, userid, audioFile);                 // Generate and initiate Nico's response
                    //transResponse = "Saved User Wav File!";
                    transResponse = transcript;
                    Debug.WriteLine("di: transResponse is : " + nicoResponse);
                }


                SQLUserState.UpdateSpeakerState(userid, dialogueAct, transcript, speakerSpoke, problemStep, timeStart, clickstep, numAutoResponses); // Write out speaker state info
                SQLNicoState.UpdateNicoState(userid, nicoResponse, problemStep, timeStart);                                                          // Write out Nico's state info & update problem/step


                // Nico response: string => Nico's response, int is the movement code, the boolean indicates whether Nico answered the step
                // To update the step, we check if Nico answered the question.

                numturns += 1;
                if (nicoResponse.Item3 == "answering")
                {
                    nextstepanswerkey = SQLProblemStepTracker.CalculateNewAnswerKey(1, answerKey, step, userid);                // Passing 1 as first argument because Nico DID answer this step
                    newanswer         = step;
                    SQLProblemStepTracker.UpdateProbStep(userid, sessionid, problem, step, probImg, nextstepanswerkey, newanswer, numAutoResponses, numturns);
                }
                else
                {
                    nextstepanswerkey = answerKey;                                                                                 // current answer key
                    SQLProblemStepTracker.UpdateProbStep(userid, sessionid, problem, step, probImg, nextstepanswerkey, newanswer, numAutoResponses, numturns);
                }
            }
            catch (Exception error)
            {
                SQLLog.InsertLog(DateTime.Now, error.Message, error.ToString(), "DialogueEngine.ashx.cs", 0, userid);
            }


            context.Response.ContentType = "text/plain";
            //context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            //context.Response.AppendHeader("Access-Control-Allow-Headers", "x-requested-with");
            context.Response.Write(transResponse);
        }