如何为我的关键字语音识别C#程序制作外部dict文件。 [英] How to make external dict file for my keyword speech recognition C# program.

查看:72
本文介绍了如何为我的关键字语音识别C#程序制作外部dict文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是让C#关键字语音识别程序从文本file.dict加载关键字,而不是程序数组中的类型关键字。下面的代码是一个工作程序,但如果我想在数组中添加数千个新关键字,这将使string [] {array}如此之大,每当我进行关键字列表修改时,我都必须重建项目。如果我可以让程序在程序启动时加载关键字文本或字符串文件,那么我可以随时修改外部关键字字典文件而无需编译代码。





使用System;

使用System.Collections.Generic;

使用System.Threading.Tasks;

使用System.Speech.Recognition;

使用System.Speech.Synthesis;

使用System.Collections。 Generic;

使用System.Data;

使用System.Text;

使用System.Globalization;

使用System.IO;



//语音演讲

amespace CSharp_Speech_ConsoleApp



{

班级课程

{



[DllImport(winmm.dll)]



public static extern int waveInGetNumDevs();



SpeechRecognitionEngine识别器=新

SpeechRecognitionEngine (新

System.Globalization.CultureInfo(en-US));



static void Main(string [] args)

{

//制作关键字数组

Choices commands = new Choices();

//如何通过从外部关键字文件导入字符串来创建此数组?

commands.Add(new String [] {早上好。,你好迈克。,

早上好,艾迪。,下午好。, 晚安,你好,

你好吗,听我迈克,别再听迈克了!

});



GrammarBuilder gBuilder = new GrammarBuilder();



gBuilder.Append(commands);

语法语法=新语法(gBuilder);



recogEngine.LoadGrammar(语法);





//获取声音输入设备的总数



int waveInDevicesCount = waveInGetNumDevs();



if(waveInDevicesCount == 0)

{

Console.WriteLine(未检测到麦克风。! );



}



else



{

Console.WriteLine(检测到麦克风。);



recogEngine.SetInputToDefaultAudioDevice();



recogEngine.SpeechRecognized + = recogEngine_SpeechRecognized;



recogEngine.RecognizeAsync(RecognizeMode.Multiple);



}



Console.ReadLine();

}

//控制台显示语音识别结果文本

static void recogEngine_SpeechRecognized(object sender,

SpeechRecognizedEventArgs e)

{



string managedString = e.Result.Text;



char [] st = managedString.ToCharArray();



Console.WriteLine(st);



}



}



}



我尝试了什么:



我已经测试了一些方法,比如make array first并使用varia ble替换字符串数组命令.Add(new String [] {(arr1)}; :



string [] arr1 = new string [] {早上好。,你好迈克。,早安艾迪。,下午好。 ,晚安,你好,你好吗,听我迈克,别再听迈克了! };



我用arr1替换{}这样的内容:

.. Choices commands = new Choices();

.. commands.Add(new String [] {(arr1)};



但是我发现这个数组不能被替换为等价变量arr1。如果这个数组可以被arr1替换,那么我可以通过在程序启动时导入一个包含关键字的文本文件来创建数组arr1。如何用另一个数组变量替换这个关键字字符串数组?

解决方案

使用 System.IO 命名空间中的类,您可以使用文件系统来加载命令。您必须首先决定格式,但最简单的方法是在每一行创建一个带有命令的文本文件。



所以, commands.txt 文件可能如下所示:

早上好。
Hello Mike。
早上好Eddy 。
下午好。



然后你可以从你的程序中读到它:

 < span class =code-keyword>使用 System.IO; 

 string [] commands = File.ReadAllLines(commands.txt); 
选择选择=新选择(命令);





关于File.ReadAllLines的文档:

File.ReadAllLines Method(String)(System.IO) [ ^ ]


What I want is to let C# Keyword Speech Recognition program load keywords from a text file.dict, instead of type keywords in program's array. The code below is a working program but if I want to add thousands new keywords in array that will make string[]{array} so large and I have to rebuild project every time whenever I do keywords list modification. If I can let program load a keywords text or string file when program is started so I can modify the external keyword dictionary file anytime I want without compiling code.


using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Globalization;
using System.IO;

//Speech to Text
amespace CSharp_Speech_ConsoleApp

{
class Program
{

[DllImport("winmm.dll")]

public static extern int waveInGetNumDevs();

SpeechRecognitionEngine recognizer = new
SpeechRecognitionEngine(new
System.Globalization.CultureInfo("en-US"));

static void Main(string[] args)
{
// Make a Keywords array
Choices commands = new Choices();
//How to make this array by importing strings from an external keywords file ?
commands.Add(new String[] { "Good morning.","Hello Mike.",
"Good morning Eddy.","Good afternoon.","Good Evening","Hello",
"How are you", "Listen to me Mike", "Stop listening Mike!"
});

GrammarBuilder gBuilder = new GrammarBuilder();

gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);

recogEngine.LoadGrammar(grammar);


//get total number of sound input devices

int waveInDevicesCount = waveInGetNumDevs();

if(waveInDevicesCount == 0)
{
Console.WriteLine("No microphone detected.!");

}

else

{
Console.WriteLine("Microphone detected. ");

recogEngine.SetInputToDefaultAudioDevice();

recogEngine.SpeechRecognized += recogEngine_SpeechRecognized;

recogEngine.RecognizeAsync(RecognizeMode.Multiple);

}

Console.ReadLine();
}
// Console Display Speech Recognized result Text
static void recogEngine_SpeechRecognized(object sender,
SpeechRecognizedEventArgs e)
{

string managedString = e.Result.Text;

char[] st = managedString.ToCharArray();

Console.WriteLine(st);

}

}

}

What I have tried:

I have tested some way , such as make array first and use variable replace the string array commands.Add(new String[] { (arr1) }; :

string[] arr1 = new string[] { "Good morning.","Hello Mike.", "Good morning Eddy.","Good afternoon.","Good Evening","Hello", "How are you", "Listen to me Mike", "Stop listening Mike!" };

I use arr1 replace the { } content like this :
.. Choices commands = new Choices();
.. commands.Add(new String[] { (arr1) } ;

but I found this array can not be replaced by a equivalent variable arr1. If this array can be replaced by arr1 then I can create array arr1 by importing a text file that contents keywords when program start. How to replace this keyword string array by another array variable ?

解决方案

Using the classes in the System.IO namespace, you can work with the file system to load the commands. You'll have to decide on a format first, but the simplest way to do it is to create a text file with a command on each line.

So, a commands.txt file could look like this:

Good morning.
Hello Mike.
Good morning Eddy.
Good afternoon.


And then you could read it from your program:

using System.IO;

string[] commands = File.ReadAllLines("commands.txt");
Choices choices = new Choices(commands);



Documentation about File.ReadAllLines:
File.ReadAllLines Method (String) (System.IO)[^]


这篇关于如何为我的关键字语音识别C#程序制作外部dict文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆