Hello everyone, I know this is basic stuff in C# that uses basic .NET API's and references but would like to share it for the people who don't know very much C# or coding at all.
Now starting with explaining the program:
The program can be modified or edited how ever you really want it. And made to respond to you how ever you want it to really. It is a very basic program and can probably be modified to do a lot more then I have it to do. The program first gets what ever word it thinks you said(I'm going to tell you right now that the speech recognition is very trash if you want to go to real stuff I'd recommend Google's API but it costs money I couldn't find any free) it will output it on your console and show you what it thinks you said then takes it and compares it to a string to see if you want the AI to respond to you or not and if not it will just sit there and be quiet
Disclaimer: I take no credit for the code below as I am not the first one to find this nor write this most likely.
The boring code part:
I will be using JetBrains Rider since I kinda like it more now for Console Applications but still use Visual Studio for most of my programs it is up to personal choice to what you want to use. I will be first showing screenshots of the code then breaking it down into more simple pieces for people who don't understand the language
Main Function:
Used in the second function to be able to speak back to the user
The Main function is used to run any code on start up of the program. In this function you can see that instantly you are using the microphone to see what the user is saying(and no it's not exported to a .mp3 or anything so chill out it can't record your mic)
Since this is kinda all speechRecognitionEng we will be explaining it all together.
"LoadGrammer" is just doing what is says it is loading the grammer and for the user to understand. "SetInputToDefaultAudioDevice" is just setting the default microphone input to the default microphone on the computer so we can use that later to write it to the console window and see what we are saying. "RecognizeAsync" is just opening another speech recognition operation which opens up to listen to multiple threads(Not very sure on that to be honest). Console.ReadLine just makes sure that the console stays open while everything is going on
The code "SpeechRecognized" is just doing what it says it is doing it is recognizing what is said and the function "speechRecognition" is telling it what to do from there.
Response Class:
The response class is what it says it is. It is just telling the main function to do when it gets input into the microphone.
"var result" is just a local string native to that function used for later in the talking part of it so I don't have to sit there and retype that it just makes it easier on yourself. Console.WriteLine is just getting the text that is recognized and outputting it onto the console. "e" is a Event handler argument that is the words that the user has said for the computer to pick up.
Now to the speaking part
If the result(remember all result is is "e.Result.Text.ToLower();") matches(==) to the word placed in the string then Sythensizer.Speak will trigger saying what ever you want it to say
This is just as simple as it gets with it. You could go more out with it you could also write a array for the result stuff which makes it 10000x easier then the way I do it but it's just a basic program that I wrote at 5AM and decided to write up on it.
If you don't understand something or need help with something let me know and I can try my best to help you
Now starting with explaining the program:
The program can be modified or edited how ever you really want it. And made to respond to you how ever you want it to really. It is a very basic program and can probably be modified to do a lot more then I have it to do. The program first gets what ever word it thinks you said(I'm going to tell you right now that the speech recognition is very trash if you want to go to real stuff I'd recommend Google's API but it costs money I couldn't find any free) it will output it on your console and show you what it thinks you said then takes it and compares it to a string to see if you want the AI to respond to you or not and if not it will just sit there and be quiet
Disclaimer: I take no credit for the code below as I am not the first one to find this nor write this most likely.
The boring code part:
I will be using JetBrains Rider since I kinda like it more now for Console Applications but still use Visual Studio for most of my programs it is up to personal choice to what you want to use. I will be first showing screenshots of the code then breaking it down into more simple pieces for people who don't understand the language
Main Function:
- Code:
static SpeechSynthesizer Synthesizer = new SpeechSynthesizer();
Used in the second function to be able to speak back to the user
The Main function is used to run any code on start up of the program. In this function you can see that instantly you are using the microphone to see what the user is saying(and no it's not exported to a .mp3 or anything so chill out it can't record your mic)
- Code:
using(SpeechRecognitionEngine speechRecognitionEng = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
- Code:
speechRecognitionEng.LoadGrammar(new DictationGrammar());
speechRecognitionEng.SetInputToDefaultAudioDevice();
speechRecognitionEng.RecognizeAsync(RecognizeMode.Multiple);
Since this is kinda all speechRecognitionEng we will be explaining it all together.
"LoadGrammer" is just doing what is says it is loading the grammer and for the user to understand. "SetInputToDefaultAudioDevice" is just setting the default microphone input to the default microphone on the computer so we can use that later to write it to the console window and see what we are saying. "RecognizeAsync" is just opening another speech recognition operation which opens up to listen to multiple threads(Not very sure on that to be honest). Console.ReadLine just makes sure that the console stays open while everything is going on
- Code:
speechRecognitionEng.SpeechRecognized += speechRecognition;
The code "SpeechRecognized" is just doing what it says it is doing it is recognizing what is said and the function "speechRecognition" is telling it what to do from there.
Response Class:
The response class is what it says it is. It is just telling the main function to do when it gets input into the microphone.
- Code:
var result = e.Result.Text.ToLower();
Console.WriteLine("Recognized text: " + e.Result.Text);
"var result" is just a local string native to that function used for later in the talking part of it so I don't have to sit there and retype that it just makes it easier on yourself. Console.WriteLine is just getting the text that is recognized and outputting it onto the console. "e" is a Event handler argument that is the words that the user has said for the computer to pick up.
Now to the speaking part
- Code:
if (result == "hello")
{
Synthesizer.Speak("Hello There");
}
If the result(remember all result is is "e.Result.Text.ToLower();") matches(==) to the word placed in the string then Sythensizer.Speak will trigger saying what ever you want it to say
This is just as simple as it gets with it. You could go more out with it you could also write a array for the result stuff which makes it 10000x easier then the way I do it but it's just a basic program that I wrote at 5AM and decided to write up on it.
If you don't understand something or need help with something let me know and I can try my best to help you