如何从带有短语主题的Cortana命令中提取参数,通过文本激活?

时间:2016-05-09 00:09:30

标签: c# win-universal-app cortana

高级别:

我想在 TEXT 模式下使用我的自定义Cortana命令“Notepad”。例如,按下WIN + S并输入“appname Notepad Example sentence!”。
(这将打开记事本并输入“例句!”。)

记事本命令已经在 VOICE 模式下工作:当我按下WIN + C并说“appname Notepad Example sentence!”时,我的记事本脚本运行有效负载“例句!”。

问题:

当我按下WIN + S并输入“appname Notepad Example sentence!”时,SpeechRecognitionResult的text属性为“Notepad ...”(与语音相反,它是“Notepad Example sentence!”,如预期的那样)。

代码:

VCD.xml摘录

<Command Name="notepad">
  <Example> Notepad Example Sentence! </Example>
  <ListenFor> Notepad {wildcardArgs} </ListenFor>
  <Feedback> Notepadding {wildcardArgs} </Feedback>
  <Navigate/>
</Command>

<PhraseTopic Label="wildcardArgs" Scenario="Dictation">
  <!--<Subject>Wildcard</Subject>-->
</PhraseTopic>

CommandHandler.cs

public static CortanaCommand ProcessCommand(SpeechRecognitionResult speechRecognitionResult, CommandDiagnostics diagnostics)
{
    // Get the name of the voice command and the raw text
    string voiceCommandName = speechRecognitionResult.RulePath[0];
    string text = speechRecognitionResult.Text;
    string mode = speechRecognitionResult.SemanticInterpretation.Properties[interpretationKey].FirstOrDefault();
    // When mode is voice, text is "Notepad Example sentence!"
    // When mode is text, text is "Notepad ..."
      // How can one retrieve "Example sentence!" from "..." !?
      // Is there some property other than speechRecognitionResult.Text that holds the raw text typed? 

    string argument = null;
    CortanaCommand processedCommand = null;

    switch (voiceCommandName)
    {
       // ...
       case CortanaCommand.Notepad:
           const string notepad = "Notepad";
           argument = CortanaCommand.StripOffCommandName(notepad, text);
           processedCommand = new NotepadCortanaCommand(argument, diagnostics);
           break;

        default:
                Debug.WriteLine("Command Name Not Found:  " + voiceCommandName);
            break;
    }
    return processedCommand;
}

问题重述

如何更改上述代码以在文本模式下提取命令参数(即应用程序名称和命令名称以外的所有内容)?

1 个答案:

答案 0 :(得分:4)

case CortanaCommand.Notepad:
       argument = speechRecognitionResult.SemanticInterpretation.Properties["wildcardArgs"].FirstOrDefault();
       // the magic line ^
       processedCommand = new NotepadCortanaCommand(argument, diagnostics);
       break;