Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?
Posted
Updated 29-Dec-16 8:33am

1 solution

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:
C#
using System.IO;
string[] commands = File.ReadAllLines("commands.txt");
Choices choices = new Choices(commands);


Documentation about File.ReadAllLines:
File.ReadAllLines Method (String) (System.IO)[^]
 
Share this answer
 
Comments
ArtiJason12335243 29-Dec-16 17:26pm    
I took out the line commands.Add(new String[] { "Good morning,.......}0; and replaced with your code ,
There is an error message say: Error CS1503 Argument 1: cannot convert from 'string[]' to 'string' at gBuilder.Append(commands);

static void Main(string[] args)
{

string[] commands = File.ReadAllLines("C:\\public\\Artigen\\commands.txt");
Choices choices = new Choices(commands);
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands); -<----------------------------------------message at this line.(red line)
Grammar grammar = new Grammar(gBuilder);
recogEngine.LoadGrammar(grammar);
ArtiJason12335243 29-Dec-16 18:26pm    
@ProgramFox Thank You, I found problem, 1). commands need be replaced with choice as code below.
....
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(choices); <-------------------------Here is the issue.
Grammar grammar = new Grammar(gBuilder);
recogEngine.LoadGrammar(grammar);
.....

and 2). command.txt content each line need use " " mark for each string. "Good Morning." " Good evening." ...and save file as unicode.
and now it is working.
Thank you very much,
Thomas Daniels 30-Dec-16 2:49am    
I don't get #2. You should not need quotation marks if you use my code... Doesn't it work without them?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900