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

时间:2011-08-21 00:57:43

标签: c# wpf grammar speech-recognition

我正在尝试从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与识别器不同,对吗? RecognitionEngine是c#的识别器,SpeechRecognizer是Windows的识别器吗?

1 个答案:

答案 0 :(得分:1)

尝试

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; 
}