2016-12-29 47 views
2

如何在語音識別程序中添加數組請參見下面的代碼?我試過 用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); 

     } 

     } 

    } 
+0

有什麼'Choices'類的樣子?它真的有一個接受像這樣的字符串數組的'Add()'方法嗎? –

+0

顯示我們在您的文件中的數據結構。 – DarkKnight

+0

'File.ReadAllLines'和/或'String.Split'是你想要使用的 – BradleyDotNET

回答

3

如何通過從文件導入字符串做一個數組?

這會給你從文件中的所有行:

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

以上從文件中讀取所有行到內存中。還有另一種方法可以在您需要時逐行讀取這些行:

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

此人返回IEnumerable<string>。例如,假設您的文件有1000行,ReadAllLines將讀取所有1000行到內存中。但ReadLines會在你需要它們時一一讀取它們。因此,如果您這樣做:

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

即使您的文件有1000行,它也只會將第一行和最後一行讀入內存。

那麼何時使用ReadLines方法?

比方說,你需要閱讀其中有1000行,你有興趣閱讀是唯一行的文件900到920,那麼你可以這樣做:

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

選擇commands = new Choices(); commands.Add(new String [] {(Lines)} – Jason

+0

對不起?你在問我點什麼嗎? – CodingYoshi

+0

我可以這樣寫代碼嗎,使用Lines替換{}中的所有字符串: 選擇commands = new Choices(); commands.Add(new String [] {(Lines)} – Jason