C#一句话多个答案

C#一句话多个答案,c#,speech-recognition,C#,Speech Recognition,我想写一个程序来做下面的事情 用户提出一个问题,程序对此问题有5个不同的答案,程序需要选择一个随机响应 该程序使用声音命令,并且来自该程序的反馈是声音的 代码 public partial class Form1 : Form { SpeechSynthesizer s = new SpeechSynthesizer(); Choices list = new Choices(); Boolean wake = true; public

我想写一个程序来做下面的事情

  • 用户提出一个问题,程序对此问题有5个不同的答案,程序需要选择一个随机响应

  • 该程序使用声音命令,并且来自该程序的反馈是声音的

代码

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","wake", "sleep", "restart", "open studio", "close studio", "search ", "standby", "mute", "unmute", "up", "down", "hex system up", "google dog"});
        Grammar gr = new Grammar(new GrammarBuilder(list));
        try
        {

            rec.RequestRecognizerUpdate();
            rec.LoadGrammar(gr);
            rec.SpeechRecognized += rec_SpeachRecognized;
            rec.SetInputToDefaultAudioDevice();
            rec.RecognizeAsync(RecognizeMode.Multiple);
            rec.LoadGrammar(new DictationGrammar());
            RecognitionResult Result = rec.Recognize();
            string ResultString = "";
            foreach (RecognizedWordUnit w in Result.Words)                
            {
                ResultString += w.Text;
            }

        }
        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")  wake = true;
        if (r == "sleep")  wake = false;
        if (wake == true)
        {
            if (r.ToLower().Contains("search"))
            {  string query = r.Replace("search", "");
                query = System.Web.HttpUtility.UrlEncode(query);
                string url = "https://www.google.com.au/search?q=" + query;
                System.Diagnostics.Process.Start(url);  }
                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; }

    public Grammar dictationGrammar { get; set; }
}
 }
您需要从
x
y
(0..4,1..5,任意):

就这样。就这么简单。玩上面的代码

在您的情况下,您可能不会编写
Console.WriteLine(answers[randomnumber])但是
说(答案[随机数])

另一方面:代替:

if (r == "hex system up") { ... }
if (r == "down") { ... }
if (r == "up")  { ... }
if (r == "unmute") { ... }
if (r == "mute")  { ... }
if (r == "standby") { ... }
研究并重写代码,使其看起来类似于:

switch (r.ToLowerInvariant()) {
    case "hex system up":
        Process.Start(@"C:\Program Files\Rainmeter\Rainmeter.exe");
        Say("Welcome back sir");
        break;
    case "down":
        ExecuteCommand(@"C:\nircmd.exe changesysvolume -10000");
        break;
    case "up":
        ExecuteCommand(@"C:\nircmd.exe changesysvolume 10000");
        break;
    case "unmute":
        ExecuteCommand(@"C:\nircmd.exe mutesysvolume 0");
        Say("system unmute sir");
        break;
    case "mute":
        Say("mute now!");
        ExecuteCommand(@"C:\nircmd.exe mutesysvolume 1");
        break;
    case "standby":
        Say("The system will enter in waiting mode sir");
        ExecuteCommand(@"C:\nircmd.exe standby");
        break;
    // ... rest here ...
    default:
        Say("Unknown command sir!");
        break;
}
此外,尽量保持一致:

  • EEXECUTECommand->say=>say
  • @“C:\program文件…”->“C:/nircmd.exe”=>@“C:\nircmd.exe”
读这本书

我是C#的新手,所以请一步一步地解释一切:d

如果你需要更多的信息,那么请问一个具体的问题,不要要求人们“一步一步地解释一切”

switch (r.ToLowerInvariant()) {
    case "hex system up":
        Process.Start(@"C:\Program Files\Rainmeter\Rainmeter.exe");
        Say("Welcome back sir");
        break;
    case "down":
        ExecuteCommand(@"C:\nircmd.exe changesysvolume -10000");
        break;
    case "up":
        ExecuteCommand(@"C:\nircmd.exe changesysvolume 10000");
        break;
    case "unmute":
        ExecuteCommand(@"C:\nircmd.exe mutesysvolume 0");
        Say("system unmute sir");
        break;
    case "mute":
        Say("mute now!");
        ExecuteCommand(@"C:\nircmd.exe mutesysvolume 1");
        break;
    case "standby":
        Say("The system will enter in waiting mode sir");
        ExecuteCommand(@"C:\nircmd.exe standby");
        break;
    // ... rest here ...
    default:
        Say("Unknown command sir!");
        break;
}