【问题标题】:C# SAPI - Recognizing phrases without pre-defined condition statementsC# SAPI - 识别没有预定义条件语句的短语
【发布时间】:2013-10-27 13:47:49
【问题描述】:

场景:

我有 2 个命令。

1) 在 Google 上搜索“此处的任何单词”

2) 打开应用程序“这里的任何单词”

问题:

既然“在 Google 上搜索”之后的词可以是任何东西,我想我怎么知道我要为我的 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 个命令并且只需要执行,这很容易 1 个动作,然后我可以将默认动作设置为 Search(),但现在情况不同了。

这是我的代码(仅适用于 1 个命令和操作,我需要 2 个及以上) * 使用 System.Speech

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);
}

【问题讨论】:

    标签: c# .net speech-recognition grammar sapi


    【解决方案1】:

    对于这类事情,您可以从RecognizedPhrase.Words 属性中提取目标短语。由于 result.text 的前 3 个单词将是“在 Google 上搜索”,result.words[3]..results.words[result.words.count-1] 将具有要搜索的短语。

    将它们连接在一起,然后就可以使用了。

    要支持多个操作,请使用Grammar.Name 属性来告诉您要运行哪个命令。

    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...
    }
    

    【讨论】:

      【解决方案2】:

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

      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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多