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". 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;
}
}
}
推荐阅读
- common-lisp - 为什么 REPL 没有显示函数调用的完整跟踪?
- angular - 使用braintree PayPal Dropin的Angular不会触发提交事件
- python - 将字典转换为字符串,然后再次返回字典
- java - 如何解决异常 java.lang.NoSuchFieldError: NOT_ISSUE
- python - Pycharm一直说名称'用户'可以未定义
- ssh - 气流如何在两个远程服务器之间传输
- unit-testing - 如何用柏树检查体内是否存在元素?
- reactjs - 反应本机按钮单击重定向屏幕错误
- arrays - 查找子数组的第一个和最后一个值相等的最大连续子数组和
- php - Youtube“实时聊天”直接网址