How to create an array from an external string file

How to add an additional array to a speech recognition program, see the code below? I tried using streamRead by reading a string and creating an array and putting it behind commands.Add(new String[] , see the code below, but could not do it.

 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); } } } 
+2
source share
1 answer

How to make an array by importing lines from a file?

This will give you all the lines from the file:

 var lines = File.ReadAllLines(@"PathToYourFile"); 

The above reads all lines from a file into memory. There is another method that will read the lines one by one as necessary:

 var lines = File.ReadLines(@"PathToYourFile"); 

This returns an IEnumerable<string> . For example, let's say your file has 1000 lines, ReadAllLines will read all 1000 lines in memory. But ReadLines will read them 1 on 1 as needed. Therefore, if you do this:

 var lines = File.ReadLines(@"PathToYourFile"); var line1 = lines.First(); var lastLine = lines.Last(); 

It will only read the first and last line in memory, even if your file has 1000 lines.

So when to use the ReadLines method?

Let's say you need to read a file that has 1000 lines, and the only lines that you are interested in reading are from 900 to 920, then you can do this:

 var lines = File.ReadLines(@"PathToYourFile"); var line900To920 = lines.Skip(899).Take(21); 
+3
source

Source: https://habr.com/ru/post/1262194/


All Articles