C#SAPI-识别没有预定义条件语句的短语 脚本:

C#SAPI-识别没有预定义条件语句的短语 脚本:,c#,.net,speech-recognition,grammar,sapi,C#,.net,Speech Recognition,Grammar,Sapi,我有两个命令 1) 在谷歌上搜索“这里的任何单词” 2) 打开应用程序“此处有任何单词” 问题: 既然“搜索谷歌”后面的单词可以是任何东西,我怎么知道我要为我的IF语句写什么呢 有了预定义的句子,我就可以像 void Engine_SpeechRecognized (object sender, SpeechRecognizedEventsArgs e) { if (e.Result.Text == "Search Google Stackoverflow") {

我有两个命令

1) 在谷歌上搜索“这里的任何单词”

2) 打开应用程序“此处有任何单词”

问题: 既然“搜索谷歌”后面的单词可以是任何东西,我怎么知道我要为我的IF语句写什么呢

有了预定义的句子,我就可以像

void Engine_SpeechRecognized (object sender, SpeechRecognizedEventsArgs e)
{
    if (e.Result.Text == "Search Google Stackoverflow")
    {
       Search("Stackoverflow");
    }
}
但既然它不是预先定义的,我该怎么办 为我的IF语句条件编写? 我不可能做到这一点

if (e.Result.Text == "Search Google" + e.Result.Text)
{
    Search(e.Result.Text);
}
那么,我该怎么做呢?如果我只有一个命令并且只需要执行,那么这很容易 1个操作,然后我可以将默认操作设置为Search(),但现在情况不同了

这是我的代码(仅用于1个命令和操作,我需要2个及以上) *使用系统语言

public MainWindow() 
{ 
    InitializeComponent(); 
    builder.Append("search google for"); builder.AppendDictation();

    Grammar grammar = new Grammar(builder);
    grammar.Name = ("Google Searching");

    engine.LoadGrammarAsync(grammar);
    engine.SetInputToDefaultAudioDevice();
    engine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Engine_SpeechRecognized);
    engine.RecognizeAsync(RecognizeMode.Multiple);
}

string result;
void Engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    txtSpeech.Text = e.Result.Text;
    ExtractKeywords(e.Result.Text);

    OpenApp("https://www.google.com/#q=" + result);
}
public主窗口()
{ 
初始化组件();
builder.Append(“搜索谷歌”);builder.AppendDictation();
语法=新语法(生成器);
grammar.Name=(“谷歌搜索”);
engine.LoadGrammarAsync(语法);
engine.setInputOdeFaultAudioDevice();
engine.SpeechRecognized+=新事件处理程序(engine\u SpeechRecognized);
engine.RecognizeAsync(RecognizeMode.Multiple);
}
字符串结果;
无效引擎_SpeechRecognized(对象发送方,SpeechRecognizedEventArgs e)
{
txtSpeech.Text=e.Result.Text;
关键词提取(如结果文本);
OpenApp(“https://www.google.com/#q=“+结果);
}

您可以使用一个字典和一个匿名方法来保存要执行的命令。为了提高性能,您最好将字典重构为静态的,并且只实例化一次,但这将为您提供总体思路

void Engine_SpeechRecognized (object sender, SpeechRecognizedEventsArgs e)
{
    var commands = new Dictionary<string, Action<string>>();
    commands.Add(
         "search google", 
         (arg) => { Search(arg);  }) ;
    commands.Add(
         "open application", 
         (arg) => { OpenApp( "https://www.google.com/#q=" + arg);  }) ;

    foreach(var command in commands.Keys)
    {
       if (e.Result.Text.StartsWith(command))
       {
          Action(command, e.Result.Text, commands[command]);
       }
    }
}

/* helper for getting one point for an arguments extractor */
static void Action(string cmd,string all, Action<string> act)
{
    string args = all.Replace(cmd,"");
    act(args);
}
void引擎\u SpeechRecognized(对象发送方,SpeechRecognizedEventsArgs e)
{
var commands=newdictionary();
命令。添加(
“搜索谷歌”,
(arg)=>{Search(arg);};
命令。添加(
“公开申请”,
(arg)=>{OpenApp(“https://www.google.com/#q=“+arg);});
foreach(commands.Keys中的var命令)
{
if(例如Result.Text.StartsWith(命令))
{
动作(命令,例如Result.Text,命令[command]);
}
}
}
/*为参数提取器获取一个点的帮助器*/
静态无效操作(字符串cmd、字符串all、操作act)
{
字符串args=all.Replace(cmd,“”);
act(args);
}

您可以使用一个字典和一个匿名方法来保存要执行的命令。为了提高性能,您最好将字典重构为静态的,并且只实例化一次,但这将为您提供总体思路

void Engine_SpeechRecognized (object sender, SpeechRecognizedEventsArgs e)
{
    var commands = new Dictionary<string, Action<string>>();
    commands.Add(
         "search google", 
         (arg) => { Search(arg);  }) ;
    commands.Add(
         "open application", 
         (arg) => { OpenApp( "https://www.google.com/#q=" + arg);  }) ;

    foreach(var command in commands.Keys)
    {
       if (e.Result.Text.StartsWith(command))
       {
          Action(command, e.Result.Text, commands[command]);
       }
    }
}

/* helper for getting one point for an arguments extractor */
static void Action(string cmd,string all, Action<string> act)
{
    string args = all.Replace(cmd,"");
    act(args);
}
void引擎\u SpeechRecognized(对象发送方,SpeechRecognizedEventsArgs e)
{
var commands=newdictionary();
命令。添加(
“搜索谷歌”,
(arg)=>{Search(arg);};
命令。添加(
“公开申请”,
(arg)=>{OpenApp(“https://www.google.com/#q=“+arg);});
foreach(commands.Keys中的var命令)
{
if(例如Result.Text.StartsWith(命令))
{
动作(命令,例如Result.Text,命令[command]);
}
}
}
/*为参数提取器获取一个点的帮助器*/
静态无效操作(字符串cmd、字符串all、操作act)
{
字符串args=all.Replace(cmd,“”);
act(args);
}

对于此类内容,可以从属性中提取目标短语。由于
result.text
的前3个单词将是“搜索谷歌”,因此
result.words[3]…结果。words[result.words.count-1]
将有短语可搜索

将它们连接在一起,然后开始

若要支持多个操作,请使用属性告知要运行的命令

void Engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    txtSpeech.Text = e.Result.Text;
    ExtractKeywords(e.Result.Text);

    if (e.Result.Grammar.Name.Equals("Google Search"))
    {
         OpenApp("www.google.com", result);
    }
    else if (e.Result.Grammar.Name.Equals("StackOverflow Search"))
    {
         OpenApp("www.stackoverflow.com", result);
    }
    // etc...
}

对于这种情况,可以从属性中提取目标短语。由于
result.text
的前3个单词将是“搜索谷歌”,因此
result.words[3]…结果。words[result.words.count-1]
将有短语可搜索

将它们连接在一起,然后开始

若要支持多个操作,请使用属性告知要运行的命令

void Engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    txtSpeech.Text = e.Result.Text;
    ExtractKeywords(e.Result.Text);

    if (e.Result.Grammar.Name.Equals("Google Search"))
    {
         OpenApp("www.google.com", result);
    }
    else if (e.Result.Grammar.Name.Equals("StackOverflow Search"))
    {
         OpenApp("www.stackoverflow.com", result);
    }
    // etc...
}