摘要:Visual Basic and Visual C# Speech Recogntion Sample Code
辨識中文,如二三點五==>23.5
完整的程式碼(Visual Studio 2010):https://skydrive.live.com/embed?cid=FBEB6373D9321A7F&resid=FBEB6373D9321A7F%2124225&authkey=AGuk0nwo7uGFd4I">https://skydrive.live.com/embed?cid=FBEB6373D9321A7F&resid=FBEB6373D9321A7F%2124225&authkey=AGuk0nwo7uGFd4I
C#:
//our speech synthesizer (for making the computer talk)
SpeechSynthesizer synthesizer;
//our speech recognition engine (for making the computer listen)
SpeechRecognitionEngine sre;
public Form1()
{
InitializeComponent();
//speech
synthesizer = new SpeechSynthesizer();
//synthesizer.SelectVoice("Microsoft Anna");
//create the recognition engine
sre = new SpeechRecognitionEngine();
//set our recognition engine to use the default audio device
sre.SetInputToDefaultAudioDevice();
//create a new GrammarBuilder to specify which commands we want to use
GrammarBuilder grammarBuilder = new GrammarBuilder();
//append all the choices we want for commands.
//we want to be able to move, stop, quit the game, and check for the cake.
Choices choice = new Choices();
choice.Add("一");
choice.Add("二");
choice.Add("三");
choice.Add("四");
choice.Add("五");
choice.Add("六");
choice.Add("七");
choice.Add("八");
choice.Add("九");
choice.Add("十");
choice.Add("點");
grammarBuilder.Append(new Choices(choice));
//create the Grammar from th GrammarBuilder
Grammar customGrammar = new Grammar(grammarBuilder);
//unload any grammars from the recognition engine
sre.UnloadAllGrammars();
//load our new Grammar
sre.LoadGrammar(customGrammar);
//add an event handler so we get events whenever the engine recognizes spoken commands
sre.SpeechRecognized += new EventHandler(sre_SpeechRecognized);
//set the recognition engine to keep running after recognizing a command.
//if we had used RecognizeMode.Single, the engine would quite listening after
//the first recognized command.
sre.RecognizeAsync(RecognizeMode.Multiple);
Speak("I am ready");
}
///
/// A small helper method for making the computer talk.
///
///The text to read.
public void Speak(string textToSpeak)
{
//if the synthesizer is ready (i.e. not already talking), speak the text
if (synthesizer.State == SynthesizerState.Ready)
synthesizer.SpeakAsync(textToSpeak);
}
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//simple check to see what the result of the recognition was
textBox1.Text = e.Result.Text;
string szWord = "";
switch (e.Result.Text)
{
//check for the directions and set the velocity appropriately.
//we have to make sure we aren't in checking mode first.
case "點":
szWord = ".";
break;
case "一":
szWord = "1";
break;
case "二":
szWord = "2";
break;
case "三":
szWord = "3";
break;
case "四":
szWord = "4";
break;
case "五":
szWord = "5";
break;
case "六":
szWord = "6";
break;
case "七":
szWord = "7";
break;
case "八":
szWord = "8";
break;
case "九":
szWord = "9";
break;
default:
szWord = e.Result.Text;
break;
}
textBoxResult.Text += szWord;
}
VB:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''speech
synthesizer = New SpeechSynthesizer()
''synthesizer.SelectVoice("Microsoft Anna");
''create the recognition engine
sre = New SpeechRecognitionEngine()
''set our recognition engine to use the default audio device
sre.SetInputToDefaultAudioDevice()
''create a new GrammarBuilder to specify which commands we want to use
Dim grammarBuilder As GrammarBuilder = New GrammarBuilder()
''append all the choices we want for commands.
''we want to be able to move, stop, quit the game, and check for the cake.
Dim choice As Choices = New Choices()
choice.Add("一")
choice.Add("二")
choice.Add("三")
choice.Add("四")
choice.Add("五")
choice.Add("六")
choice.Add("七")
choice.Add("八")
choice.Add("九")
choice.Add("十")
choice.Add("點")
GrammarBuilder.Append(New Choices(choice))
''create the Grammar from th GrammarBuilder
Dim customGrammar As Grammar = New Grammar(grammarBuilder)
''unload any grammars from the recognition engine
sre.UnloadAllGrammars()
''load our new Grammar
sre.LoadGrammar(customGrammar)
''add an event handler so we get events whenever the engine recognizes spoken commands
''sre.SpeechRecognized += New EventHandler < SpeechRecognizedEventArgs > (sre_SpeechRecognized())
AddHandler sre.SpeechRecognized, AddressOf sre_SpeechRecognized
''set the recognition engine to keep running after recognizing a command.
''if we had used RecognizeMode.Single, the engine would quite listening after
''the first recognized command.
sre.RecognizeAsync(RecognizeMode.Multiple)
Speak("I am ready")
End Sub
''
'' A small helper method for making the computer talk.
''
Private Sub Speak(ByVal textToSpeak As String)
''if the synthesizer is ready (i.e. not already talking), speak the text
If synthesizer.State = SynthesizerState.Ready Then
synthesizer.SpeakAsync(textToSpeak)
End If
End Sub
Private Sub sre_SpeechRecognized(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
''simple check to see what the result of the recognition was
textBox1.Text = e.Result.Text
Dim szWord As String = ""
Select Case e.Result.Text
''check for the directions and set the velocity appropriately.
''we have to make sure we aren't in checking mode first.
Case "點"
szWord = "."
Case "一"
szWord = "1"
Case "二"
szWord = "2"
Case "三"
szWord = "3"
Case "四"
szWord = "4"
Case "五"
szWord = "5"
Case "六"
szWord = "6"
Case "七"
szWord = "7"
Case "八"
szWord = "8"
Case "九"
szWord = "9"
Case Else
szWord = e.Result.Text
End Select
textBoxResult.Text &= szWord
End Sub