我希望将任何口头数字转换为整数,以便对它们执行操作,例如:
twenty-one >> 21 我设法对我正在使用的小范围数字进行了计算。
我遵循这个策略(但它不会起作用,因为我需要用户说出任何数字):
string[] numberString =
{
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen", "twenty"
};
Choices numberChoices = new Choices();
for (int i = 0; i < numberString.Length; i++)
{
numberChoices.Add(new SemanticResultValue(numberString[i], i));
}
gb[1].Append(new SemanticResultKey("number1", (GrammarBuilder)numberChoices));因为我不会写下所有的数字..。那么有什么聪明的方法吗??
更新1:
我尝试了以下几点:
Choices numberChoices = new Choices();
for (int i = 0; i <= 100; i++)
{
numberChoices.Add(i.ToString());
}
gb[1].Append(new SemanticResultKey("op1", (GrammarBuilder)numberChoices));
Choices choices = new Choices(gb);现在我可以有100个数字,但是如果我把它变成100万个,那就需要很长的时间来加载,它需要超过2GB的内存,而且它不能实时完成加载。与100个数字一起工作,准确性很差,它不能正确地识别12个数字,有时甚至低于10。
发布于 2015-05-29 06:57:55
您可以在语法中添加所有可能的单词,包括“100”、“数百”、“70”、“90”、“千”、“数千”作为原始的选择。
期望语义键给出结果不是一个好主意,相反,您应该只分析已识别的字符串并尝试将其解析为一个数字。
在输入时,有一个类似于“700万5003”的字符串。若要将其转换为数字,请执行以下操作:
int result = 0;
int final_result = 0;
for (String word : words) {
if (word == "one") {
result = 1;
}
if (word == "two") {
result = 2;
}
if (word == "twelve") {
result = 12;
}
if (word == "thousand") {
// Get what we accumulated before and add with thousands
final_result = final_result + result * 1000;
}
}
final_result = final_result + result;当然,语法允许识别类似于“25057”的东西,但是您必须在转换代码中处理这个问题。
https://stackoverflow.com/questions/30519785
复制相似问题