首页 > 解决方案 > 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". 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.

标签: c#dictionaryunity3dspeech-recognition

解决方案


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

推荐阅读