gpt4 book ai didi

c# - 监听特定语法c#

转载 作者:行者123 更新时间:2023-11-30 16:51:34 25 4
gpt4 key购买 nike

我目前正在编写语音识别程序,我希望程序在我说出某个词时执行某些操作。

例如,如果我说的是语法类greetingg,我希望语音合成器以“你好”作为回应。

我知道我可以使用 e.Result.Text == "something"|| e.Result.Text == "something else" 但我想指定特定的语法或语法构建或选择列表。

我该怎么做呢?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Web;



namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

List<string> jokelist = new List<string>();

SpeechSynthesizer sfos = new SpeechSynthesizer();
PromptBuilder pBuilder = new PromptBuilder();
SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));


internal static Choices slist2 = new Choices (new string[] { "hello", "hi", "howdy", "good morning", "hey", "what's up", "wazzup", "good day", "morning", "sup", "yo", "long time no see", "farewell", "take care", "later", "peace" });


internal static GrammarBuilder greetinggb = new GrammarBuilder(slist2);
internal static Grammar greetingg = new Grammar(greetinggb);


public void Form1_Load (object sender, EventArgs e)
{



jokelist.Add("Why was six afraid of seven? Because seven eight nine.");
jokelist.Add("I think you look good, ha-ha-ha-ha");


sRecognize.RequestRecognizerUpdate();

sRecognize.LoadGrammar(greetingg);

sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
sRecognize.SetInputToDefaultAudioDevice();
sRecognize.RecognizeAsync(RecognizeMode.Multiple);



}
Boolean listening = false;

public void sRecognize_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
{


SemanticValue semantics = e.Result.Semantics;
RecognitionResult result = e.Result;


if (listening)
{

if (from grammar greetingg)
{
sfos.Speak("hello");
listening = false;
}
else if (e.Result.Text == "tell me a joke")
{
sfos.Speak("Sure.");
sfos.Speak(jokelist[new Random().Next(jokelist.Count)]);
}
else if (e.Result.Confidence <= 0.79)
{
return;
}
else if (e.Result.Text == "sleep")
{
sfos.Speak("Okey then");
listening = false;
return;
}
}


else
{
if (e.Result.Text == "wake up")
{
sfos.Speak("yes master ?");
listening = true;
}
else
return;
}
richTextBox1.Text = richTextBox1.Text + " " + e.Result.Text.ToString();
}

}

最佳答案

RecognitionResult类有一个属性 Grammar语音识别器用于返回结果。您可以使用此属性来检查使用了哪个语法类。

MSDN 文档是您寻找此类内容的好资源,其中包含大量示例。

public void sRecognize_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
{

SemanticValue semantics = e.Result.Semantics;
RecognitionResult result = e.Result;

if (listening)
{
if (result.Grammar.Equals(greetingg))
{
sfos.Speak("hello");
}
else if (e.Result.Text == "tell me a joke")
{
chip.Speak("Sure.");
chip.Speak(jokelist[new Random().Next(jokelist.Count)]);
}
}
}

我看到的另一个问题是,当您开始收听时,您没有将 listening 变量设置为 true。

sRecognize.RecognizeAsync(RecognizeMode.Multiple) 调用之后添加 listening = true;

关于c# - 监听特定语法c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33719494/

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