简体   繁体   English

unity语音识别C#字典问题

[英]Unity Speech Recognition C# dictionary problem

I want to successfully implement an advanced speech recognition system that can know what to do if you speak a phrase of 2 or 3 actions like "GO FORWARD and CHANGE COLOR TO GREEN and then TURN TO THE LEFT".我想成功实现一个先进的语音识别系统,如果你说出一个包含 2 或 3 个动作的短语,例如“前进,将颜色更改为绿色,然后向左转”,它可以知道该怎么做。 I've implemented a dictionary:我已经实现了一个字典:

private Dictionary<string, Action[]> actions = new Dictionary<string, Action[]>();

and by now I added some words in it:现在我在里面加了一些词:

actions.Add("up", new Action[] { Up });
actions.Add("down", new Action[] { Down });
actions.Add("left", new Action[] { Left });
actions.Add("right", new Action[] { Right });
actions.Add("green", new Action[] { () => ChangeColor(this.word) });
actions.Add("red", new Action[] { () => ChangeColor(this.word) });
actions.Add("white", new Action[] { () => ChangeColor(this.word) });
actions.Add("blue", new Action[] { () => ChangeColor(this.word) });
actions.Add("yellow", new Action[] { () => ChangeColor(this.word) });

But then I thought...但后来我想...

I want to have advanced speech recognition, so I think (and I hope.) there is a better way than adding lots and lots of combinations of words in this dictionary.我想要先进的语音识别,所以我认为(我希望)有比在这本词典中添加大量单词组合更好的方法。

So can you help me?那你能帮帮我吗? Do you know if there is a better way?不知道有没有更好的方法? Because it would be very unpleasant and boring and it would take a long long time to get in the dictionary what you want.因为这会非常不愉快和无聊,而且需要很长时间才能在字典中找到你想要的东西。 Even for changing color, I can think now at: change color to green, change to green, change your color to green, make green your color, make green your new color, etc.即使是改变颜色,我现在可以想到:把颜色变成绿色,变成绿色,把你的颜色变成绿色,把绿色变成你的颜色,把绿色变成你的新颜色,等等。

To help you to code easier, you could use this method:为了帮助您更轻松地编写代码,您可以使用以下方法:

so using enum to list your command and inject automatically in the Recognizer.因此使用枚举列出您的命令并在识别器中自动注入。 the advantage of this method: if you add new commands no big modification in your code, just add new funtionality.. so i think the program is more readable.这种方法的优点:如果您在代码中添加新命令而没有大的修改,只需添加新的功能......所以我认为程序更具可读性。

public class TestPrg : MonoBehaviour
{
    private enum Commands
    {
        Up = 0, Forward, Left, Right,//first group
        Green, Blue, Red,            //second group
        Other, Other1                //third group....
    }

    private KeywordRecognizer kr; 

    void Start()
    {
        //Transform Enum Commands to string[]    
        kr = new KeywordRecognizer(Enum.GetNames(typeof(Commands)));

        kr.OnPhraseRecognized += RecognizedSpeech;
        kr.Start();
    }


    private void RecognizedSpeech(PhraseRecognizedEventArgs speech)
    {
        Commands cmd;
        if (Enum.TryParse(speech.text, true, out cmd))
        {
            ActionCmd(cmd);
        }
    }

    private void ActionCmd(Commands cmd)
    {
        if (cmd >= Commands.Up && cmd <= Commands.Right)
        {
            Move(cmd);
        }
        else if (cmd >= Commands.Green && cmd <= Commands.Red)
        {
            ChangeColor(cmd);
        }
        else
        {
                //Othercommand();
        }
    }

    void Move(Commands cmd)
    {
        switch (cmd)
        {
            case Commands.Up:
                //action
                break;
            case Commands.Forward:
                //action
                break;
            default:
                break;
        }
    }

    private void ChangeColor(Commands cmd)
    {
        switch (cmd)
        {
            case Commands.Green:
                //action
                break;
            case Commands.Blue:
                //action
                break;
            default:
                break;
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM