C# google 语音搜索

C# google search with voice

大家好,我m new in this C# so i don知道的不多,如果你能告诉我如何使用语音命令在 google 上搜索,我将不胜感激 ^^。

这是我的整个 code.Explain 一步一步请 :D。(告诉我是否必须添加新库或新入口或新 "using" 东西 :D

{

public partial class Form1 : Form
{           

    SpeechSynthesizer s = new SpeechSynthesizer();
    Choices list = new Choices();
    Boolean wake = true;
    public Form1()
    {
        SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
        list.Add(new String[] { "hello", "how are you", "what time is it", "what day is it", "open google", "wake", "sleep", "restart", "open studio", "close studio", "search", "standby", "mute", "unmute", "up", "down", "hex system up"});
        Grammar gr = new Grammar(new GrammarBuilder(list));
        try
        {
            rec.RequestRecognizerUpdate();
            rec.LoadGrammar(gr);
            rec.SpeechRecognized += rec_SpeachRecognized;
            rec.SetInputToDefaultAudioDevice();
            rec.RecognizeAsync(RecognizeMode.Multiple);
        }
        catch { return; }


        InitializeComponent();
    }
    public void KillProg(String s)
    {

        System.Diagnostics.Process[] procs = null;
        try
        {
            procs = Process.GetProcessesByName(s);
            Process prog = procs[0];
            if (!prog.HasExited) { prog.Kill(); }


    }finally
        {
            if (procs != null) 
            { 
                foreach (Process p in procs)
                {
                    p.Dispose();
                }
            }
        }
}    


    public void restart()
    {
        Process.Start(@"D:\here.exe");
        Environment.Exit(0);
    }
    public void  say(String h)
    {

        s.Speak(h);

    }

    public static void ExecuteCommand(string Command)
    {
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c" + Command);
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        proc.Close();
    }
    //Speech Commands
    private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
    {            
        String r = e.Result.Text;
        if (r == "wake") { say("The system is listening sir"); } wake = true;
        if (r == "sleep") { say("The system will no more listen sir"); } wake = false;
        if (wake == true)            
        {
            if (r == "hex system up") {Process.Start(@"C:\Program Files\Rainmeter\Rainmeter.exe"); say("Welcome back sir                   the system is loading and all energy is stabilized      ,          now i am at 100% capacity"); }
            if (r == "down") { ExecuteCommand("C:/nircmd.exe changesysvolume -10000"); }
            if (r =="up") {ExecuteCommand ("C:/nircmd.exe changesysvolume 10000");}
            if (r == "unmute"){ExecuteCommand ("C:/nircmd.exe mutesysvolume 0"); say("system unmute sir");}
            if (r == "mute") {say("mute now!"); ExecuteCommand ("C:/nircmd.exe mutesysvolume 1");}
            if (r == "standby") { say("The system will enter in waiting mode sir"); ExecuteCommand("C:/nircmd.exe standby"); }      
            if (r == "close studio") { KillProg("WDExpress"); }
            if (r == "open studio") { Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\WDExpress.exe"); }
            if (r == "hello") { say("Hi"); } 
            if (r == "how are you") { say("Great , and you?"); }
            if (r == "what time is it") { say(DateTime.Now.ToString("hh:mm")); }
            if (r == "what day is it") { say(DateTime.Now.ToString("M/d/yyyy")); }
            if (r == "open google") { Process.Start("https://www.google.ro"); }
        }   





    }

    private void say()
    {
        throw new NotImplementedException();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public bool c { get; set; }

    public int r { get; set; }

    public int rnd { get; set; }
}

}

SpeechRecognitionEngine 有多个 "modes"。你在命令模式下使用它,这意味着你给它一些它可以识别的预先指定的词。要在允许它识别任意单词的听写模式下使用它,请将 System.Speech.Recognition.DictationGrammar 的实例作为语法添加到您的 SpeechRecognitionEngine,方法是:rec.LoadGrammar(new DictationGrammar());。完成此操作后,SpeechRecognitionEngine 将识别任意单词。


要执行实际的 google 搜索部分,您可以通过以下方式在 google 上搜索内容:System.Diagnostics.Process.Start("http://www.google.com/search?q=" + StringToSearchFor)。这可以用在 rec_SpeechRecognized 方法中。例如,您可以检查识别的文本是否以 "google" 开头,然后搜索后面的任何内容。

示例:

    private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Text.StartsWith("google "))
        {
            System.Diagnostics.Process.Start(
                "https://www.google.com/search?q=" + e.Result.Text.Substring(7) //"google " is 7 characters long.
            );
        } 
        ...
    }

相关博客post:http://csharp-tricks-en.blogspot.dk/2011/03/speech-recognition-part-1-dictation-mode.html