public DialogueRecognitionEngine(Profile profile)
     : base(profile)
 {
     dialogueWatcher = new System.IO.FileSystemWatcher();
     dialogueWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
     dialogueWatcher.Changed += dialogueWatcher_Changed;
     this.dialogueWatcher.Filter = "*.diag";
 }
        public RecognitionEngine(Profile profile)
        {
            this.actions = new List<Action>();

            engine = new sp.SpeechRecognitionEngine();
            engine.SetInputToDefaultAudioDevice();
            engine.SpeechRecognized += engine_SpeechRecognized;
            engine.RecognizerUpdateReached += engine_RecognizerUpdateReached;

            LoadProfile(profile);
        }
        public void LoadProfile(Profile profile)
        {
            this.currentProfile = profile;
            if (this.actions != null)
                this.actions.Clear();

            this.currentProfile.UpdateGrammar();

            List<UpdateOperation> ops = new List<UpdateOperation>();
            foreach (sp.Grammar gram in this.engine.Grammars)
            {
                ops.Add(new UpdateOperation()
                {
                    UpdateType = UpdateOperationType.RemoveGrammar,
                    Grammar = gram
                });
            }
            ops.Add(new UpdateOperation()
            {
                UpdateType = UpdateOperationType.AddGrammar,
                Grammar = this.currentProfile.Grammar,
                AssociatedActions = this.currentProfile.Actions
            });

            this.pauseGrammar = null;
            if (this.currentProfile.EnableVoicePausing)
            {
                this.pauseGrammar = GeneratePauseGrammar();
                ops.Add(new UpdateOperation()
                {
                    UpdateType = UpdateOperationType.AddGrammar,
                    Grammar = pauseGrammar
                });
            }

            this.ExecuteGrammarChanges(ops);
        }
        void UpdateDialogueOptions()
        {
            if (this.Running)
            {
                List<Action> actionlist = new List<Action>();
                bool readSuccess = true;
                do
                {

                    try
                    {
                        string filename = System.IO.Path.Combine(this.currentProfile.Dialogue.FilePath, FILE_DIALOGUETEXT);
                        using (System.IO.StreamReader rdr = new System.IO.StreamReader(filename))
                        {
                            readSuccess = true;
                            int index = 0;

                            while (!rdr.EndOfStream)
                            {
                                string text = rdr.ReadLine();
                                if (text.Split('(')[0].Length > 0)
                                    text = text.Split('(')[0];
                                text = System.Text.RegularExpressions.Regex.Replace(text, "[?\"!\\.]", "");
                                //Replace('?', '.').Replace('\"',' ').
                                Action action = new Action();
                                action.ActionName = text;
                                action.Phrases.Add(text);

                                Command move = new Command();
                                Command tmp;

                                if (this.dialoguePosition > index)
                                    tmp = this.currentProfile.Dialogue.CommandPrevious;
                                else
                                    tmp = this.currentProfile.Dialogue.CommandNext;

                                move.Repeat = Math.Abs(this.dialoguePosition - index);

                                move.CommandName = String.Format("Go Down {0}", index);
                                move.Key = tmp.Key;
                                move.ModifierKey = tmp.ModifierKey;
                                move.HeldDuration = tmp.HeldDuration;
                                move.PausedDuration = tmp.PausedDuration;

                                action.Commands.Add(move);
                                action.Commands.Add(this.currentProfile.Dialogue.CommandAccept);
                                actionlist.Add(action);
                                ++index;

                            }
                        }
                    }
                    catch (System.IO.IOException)
                    {
                        readSuccess = false;
                    }

                }
                while (!readSuccess);

                if (actionlist.Count > 0)
                {
                    Profile prof = new Profile();
                    prof.Actions = actionlist;
                    prof.Actions.Add(GenerateGoodbyeAction());
                    prof.ProfileName = "Dialogue";
                    bool same = false;
                    if (this.dialogueProfile != null && this.dialogueProfile.Actions.Count == prof.Actions.Count)
                    {
                        same = true;
                        for (int i = 0; i < this.dialogueProfile.Actions.Count; i++)
                        {
                            if (this.dialogueProfile.Actions[i].ActionName != prof.Actions[i].ActionName)
                                same = false;
                        }
                    }
                    if (!same)
                    {

                        prof.UpdateGrammar();

                        var changes = new List<UpdateOperation>();
                        changes.Add(new UpdateOperation()
                        {
                            UpdateType = UpdateOperationType.DisableGrammar,
                            Grammar = this.currentProfile.Grammar
                        });
                        changes.Add(new UpdateOperation()
                        {
                            UpdateType = UpdateOperationType.AddGrammar,
                            Grammar = prof.Grammar,
                            AssociatedActions = prof.Actions
                        });

                        if (this.dialogueProfile != null)
                        {
                            changes.Add(new UpdateOperation()
                            {
                                UpdateType = UpdateOperationType.RemoveGrammar,
                                Grammar = this.dialogueProfile.Grammar,
                                AssociatedActions = this.dialogueProfile.Actions
                            });
                            //this.engine.RemoveGrammar(this.dialogProfile.Grammar, this.dialogProfile.Actions);
                        }

                        this.ExecuteGrammarChanges(changes);

                        this.dialogueProfile = prof;
                        Console.WriteLine("Dialogue Mode Initializing");
                    }
                }
                else if (this.dialogueProfile != null)
                {
                    Console.WriteLine("End Dialogue");

                    var changes = new List<UpdateOperation>();
                    changes.Add(new UpdateOperation()
                    {
                        UpdateType = UpdateOperationType.EnableGrammar,
                        Grammar = this.currentProfile.Grammar
                    });
                    changes.Add(new UpdateOperation()
                    {
                        UpdateType = UpdateOperationType.RemoveGrammar,
                        Grammar = this.dialogueProfile.Grammar,
                        AssociatedActions = this.dialogueProfile.Actions
                    });

                    this.ExecuteGrammarChanges(changes);

                    this.dialogueProfile = null;
                }
            }
        }