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.pauseGramamr = null;
                if (this.currentProfile.EnableVoicePausing)
                {
                    this.pauseGramamr = GeneratePauseGrammar();
                    ops.Add(new UpdateOperation()
                    {
                        UpdateType = UpdateOperationType.AddGrammar,
                        Grammar = pauseGramamr
                    });
                }

                this.ExecuteGrammarChanges(ops);
        }
        private void UpdateDialogueOptions()
        {
            if (this.Running)
            {
                string filename = System.IO.Path.Combine(this.currentProfile.Dialogue.FilePath, FILE_DIALOGUETEXT);

                ObservableCollection<Action> actionList = new ObservableCollection<Action>();
                bool readSuccess = true;
                do
                {
                    try
                    {
                        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("Item {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)
                    {
                        Console.WriteLine(String.Format("Failed to read \"{0}\"", filename));
                        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.ExecuteGrammarChanges(changes);

                        this.dialogueProfile = prof;
                        Console.WriteLine("Initializing Dialogue Mode");
                    }
                    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;
                    }
                }
            }
        }