コード例 #1
0
ファイル: VoiceSpeech.cs プロジェクト: Tapanito/F20CA
        public async void StartListening(object sender, EventArgs e)
        {
            try
           {    
                //args = e;
                speechRecognizer = new SpeechRecognizer();
                StorageFolder folder = ApplicationData.Current.LocalFolder;
                var uri = new System.Uri("ms-appx:///Assets/TestGrammar.xml");
                var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
                speechRecognizer.Constraints.Clear();
                speechRecognizer.Constraints.Add(new SpeechRecognitionGrammarFileConstraint(file));
                SpeechRecognitionCompilationResult compilationResult = await speechRecognizer.CompileConstraintsAsync();
                if (compilationResult.Status != SpeechRecognitionResultStatus.Success)
                    throw new Exception("Grammar compilation failed");

                

                speechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
                speechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;

                Debug.WriteLine("Listener initialized");
                isListening = true;
                await speechRecognizer.ContinuousRecognitionSession.StartAsync();
                uri = new System.Uri("ms-appx:///Assets/ResponseTemplates.xml");
                file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
                var t = new DialogueManager(file);
                var qq = t.GenerateResponse(new Dictionary<string, string>() { { "ACTION", "DESTINATION" }, { "DESTINATION", "COFFEE_SHOP" } }, ref args);
                Debug.WriteLine(qq);
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
               () =>
               {
                   Speak(qq);
               });






            }
            catch (Exception ex)
            {
                isListening = false;
            }

            //return "I was returned";
        }
コード例 #2
0
ファイル: VoiceSpeech.cs プロジェクト: Tapanito/F20CA
        private async void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs arguments)
        {
            Debug.WriteLine("User Input: " + arguments.Result.Text);

            var recoResult = arguments.Result;
            var prop = recoResult.SemanticInterpretation.Properties as IReadOnlyDictionary<string, IReadOnlyList<string>>;
            var dict = new Dictionary<string, string>();
            foreach (var key in prop.Keys)
            {
                var item = prop[key];
                dict.Add(key, item[0]);
            }
            var uri = new System.Uri("ms-appx:///Assets/ResponseTemplates.xml");
            var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
            DialogueManager dm = new DialogueManager(file);
            args.Name = "COFFEE_SHOP";
            string response = dm.GenerateResponse(dict, ref args);
            hasSpoken = true;
            if (response != null)
                VoiceResponse(response);
            //Debug.WriteLine(args.ToString());
            //ParseUtteranceMeaning(prop);
        }