The AI Book Engine Acting as Enterprise Star Trek Computer
The AI Book Engine onboard the Enterprise of Star Trek

The AI Book Engine Acting as Enterprise Star Trek Computer

There was still a whisper in my mind of something lacking and ready to be made. The urge for that was also created by the lack of intelligence in Alexa or the Google Home Talk-a-To's and never get really interesting 'smart' answers.

What I needed was the Star Trek Enterprise Ship Computer.

So, I've created just that and it works fine!

What does it basically do?

  1. It waits till you say the word "Computer"
  2. Then it says the Star Trek computer word "Affirmative"
  3. It stands in receive mode for the question.
  4. You Ask it whatever question.
  5. The computer says Star Trek "Working"
  6. The computer gives you the answer.
  7. It finishes with the Star Trek Computer words "Transfer Complete"
  8. Computer goes in waiting mode for the word "Computer again.

First the code that is added to: Full_Article_Code


The command args[] part:

else if (args[0] != null && args[0].Contains("startrek"))
        {
            try
            {
                TextToSpeechClient client = IniGoogleSound();
                string textToSynthesize = "Please, start communicating with me.";
                GlobalMethods.DoSpeak(client, textToSynthesize);

                while(true)
                {
                    var speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);
                    speechConfig.SpeechRecognitionLanguage = "en-US";
                    using var audioConfig = Microsoft.CognitiveServices.Speech.Audio.AudioConfig.FromDefaultMicrophoneInput();
                    using var speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);
                    Console.Clear();
                    Console.WriteLine("Speak into your microphone.");
                    var speechRecognitionResult = await speechRecognizer.RecognizeOnceAsync();
                    
                    // Check if the recognized text starts with the word "computer"
                    if (speechRecognitionResult.Text.StartsWith("computer", StringComparison.OrdinalIgnoreCase))
                    {
                        
                        GlobalMethods.PlayAudio("affirmative.wav");
                        var speechConfigC = SpeechConfig.FromSubscription(speechKey, speechRegion);
                        speechConfigC.SpeechRecognitionLanguage = "en-US";
                        using var audioConfigC = Microsoft.CognitiveServices.Speech.Audio.AudioConfig.FromDefaultMicrophoneInput();
                        using var speechRecognizerC = new SpeechRecognizer(speechConfigC, audioConfigC);
                        Console.Clear();
                        Console.WriteLine("Ask the question");
                        var speechRecognitionResultC = await speechRecognizerC.RecognizeOnceAsync();
                        StartrekResult(speechRecognitionResultC);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }        

The voice engine part:

static async void StartrekResult(Microsoft.CognitiveServices.Speech.SpeechRecognitionResult speechRecognitionResult)
    {
        switch (speechRecognitionResult.Reason)
        {
            case ResultReason.RecognizedSpeech:
                GlobalMethods.PlayAudio("working.wav");
                Console.WriteLine($"RECOGNIZED: Text={speechRecognitionResult.Text}");
                TextToSpeechClient client = IniGoogleSound();
                string textToSynthesize = speechRecognitionResult.Text;
                string[] words = textToSynthesize.Split(' ');
                
                // Check if the first word is "computer" (case-insensitive)
                if (words.Length > 0 && words[0].Equals("computer", StringComparison.OrdinalIgnoreCase))
                {
                    // Remove the first word and join the remaining words
                    textToSynthesize = string.Join(" ", words.Skip(1));
                }
                Console.WriteLine("Question asked to the computer:" + textToSynthesize);
                string responseText = GetRESTAPICallContent("https://bibleapje.azurewebsites.net/api/ChatGPT/"
                                + textToSynthesize);

                GlobalMethods.DoSpeak(client, RemoveNonLettersForTalk(responseText));
                GlobalMethods.PlayAudio("transfercomplete.wav");
                break;
            case ResultReason.NoMatch:
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                break;
            case ResultReason.Canceled:
                var cancellation = CancellationDetails.FromResult(speechRecognitionResult);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you set the speech resource key and region values?");
                }
                break;
        }
    }        

If you are a Trekkie, like I am, then probably you like this functionality. Now, besides me, stands my old laptop in waiting mode. If I say "computer", the Star Trek Enterprise Computer voice says: "Affirmative", and I can talk to it.

I'm thrilled. :-)

Happy coding.

要查看或添加评论,请登录