c# - 监听特定语法c#

标签 c# if-statement speech-recognition speech-synthesis

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

例如,如果我说的是语法类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/

相关文章:

c# - 使用 Razor 引擎避免 asp.net mvc 中的特殊字符

c# - WPF 中的填充弧

c# - 创建委托(delegate) MouseEventHandler 的新实例

java - 如何在 if/else 语句中检查整数且小于或等于 100

ios - 我如何使用 websocket 将音频发送到 Microsoft Translator

c# - 如何使用 NuGet.Core 创建 NuGet 包?

字符不会正确比较

java - 如何根据 <c :if> with variable in a jsp page 显示不同的网页内容

python - 使用 Python 和 PyAudio 的语音转文本无法在操作系统上运行