如何在語音識別程序中添加數組請參見下面的代碼?我試過 用streamRead讀取一個字符串並製作一個數組並放在commands.Add(new String[]
的後面,看下面的代碼但是無法做到。如何從外部字符串文件創建數組
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 a 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);
}
}
}
有什麼'Choices'類的樣子?它真的有一個接受像這樣的字符串數組的'Add()'方法嗎? –
顯示我們在您的文件中的數據結構。 – DarkKnight
'File.ReadAllLines'和/或'String.Split'是你想要使用的 – BradleyDotNET