在for循环中为语音识别添加单词

本文关键字:语音识别 添加 单词 for 循环 | 更新日期: 2023-09-27 18:06:26

我正在尝试从datagridview添加GrammarBuilder中的单词…

List<string> grammerList = new List<string>();                           
public myForm()
{
    InitializeComponent();
    recognizer = new SpeechRecognitionEngine();
    InitializeSpeechRecognitionEngine();
}
private void InitializeSpeechRecognitionEngine()
{
    recognizer.SetInputToDefaultAudioDevice();
    Grammar customGrammar = CreateCustomGrammar();
    recognizer.LoadGrammar(customGrammar);
    recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(
        recognizer_SpeechRecognized);
}
private Grammar CreateCustomGrammar()
{
    DataTable myTable = new DataTable();
    OleDbConnection myConnection = new OleDbConnection(connection);
    OleDbCommand myCommand = new OleDbCommand("Select * From wordtable", myConnection);
    OleDbDataAdapter adapter = new OleDbDataAdapter(myCommand);
    adapter.Fill(myTable);
    for (ctr = 1; ctr < myTable.RowCount; ctr++)
        grammerList.Add(myTable.Rows[ctr].Cells[0].Value.ToString());
    Choices mychoices = new Choices(grammerList.ToArray());
    GrammarBuilder myGrammarBuilder = new GrammarBuilder(c);
    Grammar myGrammar = new Grammar(myGrammarBuilder);
    return myGrammar; 
}

返回一个错误:对象引用未设置为对象的实例。

grammerList.Add(myTable.Rows[ctr].Cells[0].Value.ToString());

RecognitionEngine不同于recognizer,对吧?识别器是c#的识别器,语音识别器是Windows的识别器对吗?

在for循环中为语音识别添加单词

try

private Grammar CreateCustomGrammar()
{
    DataTable myTable = new DataTable();
    using ( OleDbConnection myConnection = new OleDbConnection(connection) )
    using ( OleDbCommand myCommand = new OleDbCommand("Select * From wordtable", myConnection) )
    {
    myConnection.Open();
    myTable.Load (myCommand.ExecuteReader();
    grammerList.AddRange ((from r in myTable.Rows select r[0]..ToString()).ToArray());
    }

   Choices mychoices = new Choices(grammerList.ToArray());
   GrammarBuilder myGrammarBuilder = new GrammarBuilder(c);
   Grammar myGrammar = new Grammar(myGrammarBuilder);
   return myGrammar; 
}