scripting - 如何将 Cortana 命令连接到自定义脚本?

标签 scripting nlp windows-10 cortana

现在问这个问题可能有点早,但我正在运行 Windows 10 技术预览版 10122。我想将 Cortana 设置为具有自定义命令。她的工作方式如下:

Hey Cortana, <she'll listen and process this command>

微软将处理该命令,如果没有任何内容,她只会在 bing 上搜索输入。但是,我希望能够说一些类似的话,例如

Hey Cortana, I'm going to bed now

并让输入我现在要 sleep 触发运行批处理脚本、VBScript、命令或任何某种基本上执行以下操作的自定义响应。

C:\> shutdown -s

有没有办法为 Cortana 设置预定义的自定义命令?

更新:

我创建了这个basic YouTube tutorialthis more advanced one相应的GitHub repo基于talkitbr的优秀且非常有帮助的答案below

起初他的回答超出了我的理解范围,所以我决定为像我这样的 future 用户更详细地分解它。

最佳答案

您可以创建供 Cortana 监听的命令。 这些命令需要在名为 Voice Command Definitions 的 XML 文件中进行描述。或VCD。

这是一个例子:

<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
    <CommandSet xml:lang="en-us" Name="HomeControlCommandSet_en-us">
        <CommandPrefix>HomeControl</CommandPrefix>
        <Example>Control alarm, temperature, light and others</Example>

        <Command Name="Activate_Alarm">
            <Example>Activate alarm</Example>
            <ListenFor>[Would] [you] [please] activate [the] alarm [please]</ListenFor>
            <ListenFor RequireAppName="BeforeOrAfterPhrase">Activate alarm</ListenFor>
            <ListenFor RequireAppName="ExplicitlySpecified">Activate {builtin:AppName} alarm</ListenFor>
            <Feedback>Activating alarm</Feedback>
            <Navigate />
        </Command>
        ...
    </CommandSet>
</VoiceCommands>

创建此定义后,您需要在应用程序启动时注册它:

protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    // Install the VCD
    try
    {
        StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"HomeControlCommands.xml");
        await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("There was an error registering the Voice Command Definitions", ex);
    }
}

然后重写App.OnActivated方法来处理事件触发的时间:

protected override void OnActivated(IActivatedEventArgs e)
{
    // Handle when app is launched by Cortana
    if (e.Kind == ActivationKind.VoiceCommand)
    {
        VoiceCommandActivatedEventArgs commandArgs = e as VoiceCommandActivatedEventArgs;
        SpeechRecognitionResult speechRecognitionResult = commandArgs.Result;
 
        string voiceCommandName = speechRecognitionResult.RulePath[0];
        string textSpoken = speechRecognitionResult.Text;
        IReadOnlyList<string> recognizedVoiceCommandPhrases;
 
        System.Diagnostics.Debug.WriteLine("voiceCommandName: " + voiceCommandName);
        System.Diagnostics.Debug.WriteLine("textSpoken: " + textSpoken);
 
        switch (voiceCommandName)
        {
            case "Activate_Alarm":
                System.Diagnostics.Debug.WriteLine("Activate_Alarm command");
                break;

The tutorial shows the complete code [web archive] .

完成所有这些操作后,您可以使用 ProcessStartInfoSystem.Diagnostics.Process.Start 调用批处理脚本。

此外,如果您有兴趣通过 Cortana 窗口响应用户,请检查此 post regarding Cortana in background [web archive] .

关于scripting - 如何将 Cortana 命令连接到自定义脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431688/

相关文章:

python-3.x - 非英语语料的词云

java - Eclipse Maven : "Cannot be resolved to a type" error

php - XSym 符号链接(symbolic link)不能在 Docker for Windows 上的 PHP 中使用

WPF native Windows 10 toast

c# - 有没有办法打开文件资源管理器并从 UWP 应用程序中选择文件?

Powershell:从文件末尾向后搜索

linux - 使用脚本从函数中提取参数

machine-learning - 自动摘要中句子提取的基于图的加权?

node.js - Node.js 不适合做什么样的任务?

python - 将 MFC .dll 文件与 Python 3.2 结合使用