gpt4 book ai didi

c# - Microsoft 语音识别引擎在语法之间切换

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:06 25 4
gpt4 key购买 nike

所以我有这个语音识别代码,我一直在使用 Microsoft 的语音识别引擎。

不幸的是,它在理解声音方面并不是那么出色,所以我一直在想办法解决这个问题。其中之一是在特定语法和通用词典语法之间切换。我似乎无法弄清楚如何在语法之间切换,但在不识别给定语音的情况下。

如果有人能帮我弄清楚如何构造它,那么只要我的 commandList 语法无法识别拾取的语音,就可以从我的 commandList 语法切换到 DictationGrammar()。

代码如下:

//using Microsoft.Speech.Recognition;

using System;
using System.Speech.Recognition;
using System.Windows.Forms;
using System.Collections.Generic;

namespace vRec
{
public class Form1
{
static int counter = 0;
static bool stop = false;
static string command = null;
static List<String> commandList = new List<string>() { "zooey", "open", "quit", "search", "close", "yes", "no" };
static Choices keywords = new Choices();

public static void Main()
{
command = null;
stop = false;

// Create an in-process speech recognizer for the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{

keywords.Add(commandList.ToArray());
GrammarBuilder grammarBuilder = new GrammarBuilder(keywords);
Grammar testGrammar = new Grammar(grammarBuilder);
recognizer.LoadGrammar(testGrammar);
recognizer.LoadGrammar(new DictationGrammar());

// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();

// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);

Console.WriteLine("NOT TERMINATED");

// Keep the console window open.
if(!stop)
Console.ReadLine();
}
}

// Handle the SpeechRecognized event.
public static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (counter == 2 && (e.Result.Text.ToUpper() == "YES" || e.Result.Text.ToUpper() == "US" || e.Result.Text.ToUpper() == "AS"))
{
counter = 0;
Console.WriteLine("THANK YOU LORD JEBUS");
stop = true;
SendKeys.SendWait("{ENTER}");
//command string to be passed in for functions in c++ code
}
else if (counter == 2 && (e.Result.Text.ToUpper() == "NO" || e.Result.Text.ToUpper() == "NOW" || e.Result.Text.ToUpper() == "KNOW" || e.Result.Text.ToUpper() == "OH" || e.Result.Text.ToUpper() == "NOT" || e.Result.Text.ToUpper() == "NOPE" || e.Result.Text.ToUpper() == "NAH"))
{
Console.WriteLine("Can you spell that?");
counter = 1;
command = e.Result.Text;
}
else if (counter == 2 && (e.Result.Text.ToUpper() != "YES" || e.Result.Text.ToUpper() != "US" || e.Result.Text.ToUpper() != "AS" || e.Result.Text.ToUpper() != "NO" || e.Result.Text.ToUpper() != "NOW" || e.Result.Text.ToUpper() != "KNOW" || e.Result.Text.ToUpper() != "OH" || e.Result.Text.ToUpper() != "NOT" || e.Result.Text.ToUpper() != "NOPE" || e.Result.Text.ToUpper() != "NAH"))
{
//Console.WriteLine(counter);
Console.WriteLine("Can you repeat that?");
counter = 1;
}

if (counter == 1)
{
command = e.Result.Text;

if (e.Result.Text.ToUpper() == "ALL BEEN" || e.Result.Text.ToUpper() == "OPIUM" || e.Result.Text.ToUpper() == "OLD AND" || e.Result.Text.ToUpper() == "HOLE IN" || e.Result.Text.ToUpper() == "HOPING" || e.Result.Text.ToUpper() == "OLD BEEN" || e.Result.Text.ToUpper() == "OPEN")
command = "open";

if (e.Result.Text.ToUpper() == "WAIT" || e.Result.Text.ToUpper() == "QUITE" || e.Result.Text.ToUpper() == "QUIP" || e.Result.Text.ToUpper() == "QUICK" || e.Result.Text.ToUpper() == "CLIP" || e.Result.Text.ToUpper() == "QUIT")
command = "quit";

if (e.Result.Text.ToUpper() == "SUCH" || e.Result.Text.ToUpper() == "SORT" || e.Result.Text.ToUpper() == "SEARCH")
command = "search";

if (e.Result.Text.ToUpper() == "RULES" || e.Result.Text.ToUpper() == "FELLOWS" || e.Result.Text.ToUpper() == "CLOSE")
command = "close";

commandList.Add(command);
Console.WriteLine(counter);
Console.WriteLine("Recognized text: " + command);
Console.WriteLine("Is this correct?");

for (int i = 0; i < commandList.Count; i++)
{
Console.WriteLine("/" + commandList[i]);
}

counter++;

}

if (e.Result.Text.ToUpper() == "ZOOEY" || e.Result.Text.ToUpper() == "ZOE" || e.Result.Text.ToUpper() == "EASILY" || e.Result.Text.ToUpper() == "SALLY" || e.Result.Text.ToUpper() == "ZONE" || e.Result.Text.ToUpper() == "ZONE WE" || e.Result.Text.ToUpper() == "SOLELY" || e.Result.Text.ToUpper() == "ZOELLICK" && counter == 0)
{
counter++;
Console.WriteLine("How can I help you?");
}

Console.WriteLine("Recognized text: " + e.Result.Text);
}

public static string getCommand()
{
return command;
}

}
}

如有任何帮助,我们将不胜感激。 ^.^

最佳答案

我能想到的一种方法:

设置一个置信度阈值,任意选择 0.6,然后如果在 recognizer_SpeechRecognized 方法中拾取的语音低于该阈值,则切换语法。为此,您应该将“SpeechRecognitionEngine.UnloadAllGrammars”与 SpeechRecognitionEngine.LoadGrammarAsync 结合使用获得识别语音的置信度,

e.Result.Confidence`, so your code could look like: 
if (e.Result.Confidence <0.6) {
recognizer.RequestRecognizerUpdate();
recognizer.UnloadAllGrammars();
recognizer.LoadGrammarAsync(//switch your grammmars);

}

你将不得不看一下你的语法等的可用性。在这个范围内。希望这对您有所帮助!

关于c# - Microsoft 语音识别引擎在语法之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com