How to extract the argument from a Cortana command with a phrase topic, activated via text?

杀马特。学长 韩版系。学妹 提交于 2019-12-07 06:01:11

问题


High Level:

I want to use my custom Cortana command "Notepad" in TEXT mode. For instance, by pressing WIN+S and typing "appname Notepad Example sentence!".
(This will open Notepad and input "Example sentence!".)

The Notepad command already works in VOICE mode: when I press WIN+C and say "appname Notepad Example sentence!", my notepad script is run with the payload "Example sentence!".

The Problem:

When I press WIN+S and input "appname Notepad Example sentence!", the text property of SpeechRecognitionResult is "Notepad ..." (as opposed to voice where it is "Notepad Example sentence!", as expected).

Code:

VCD.xml excerpt

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

Question Restated

How can the above code be changed to extract command arguments (i.e. everything other than the app name and command name) in text mode?


回答1:


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


来源:https://stackoverflow.com/questions/37106171/how-to-extract-the-argument-from-a-cortana-command-with-a-phrase-topic-activate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!