2017-01-27 75 views
7

我有一個SQL數據庫應用程序,我想結合了語音搜索功能。我已經知道如何搜索數據庫,但我不知道如何從數據庫中的表中創建語法。這是我的代碼到目前爲止。語音識別在SQL數據庫VB.net

Dim WithEvents reco As New Recognition.SpeechRecognitionEngine 


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    reco.SetInputToDefaultAudioDevice() 

    Dim gram As New Recognition.SrgsGrammar.SrgsDocument 

    Dim RecipeRule As New Recognition.SrgsGrammar.SrgsRule("recipe") 

    Dim colorsList As New Recognition.SrgsGrammar.SrgsOneOf(DataSet1.Table.ToString) 

    RecipeRule.Add(colorsList) 

    gram.Rules.Add(RecipeRule) 

    gram.Root = RecipeRule 

    reco.LoadGrammar(New Recognition.Grammar(gram)) 

    reco.RecognizeAsync() 

End Sub 

Private Sub reco_RecognizeCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles reco.RecognizeCompleted 

    reco.RecognizeAsync() 

End Sub 

Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles reco.SpeechRecognized 
    Try 
     Me.TableTableAdapter.Recipe(Me.DataSet1.Table, e.Result.Text & "%") 
    Catch ex As System.Exception 
     System.Windows.Forms.MessageBox.Show(ex.Message) 
    End Try 
End Sub 

我需要在DataSet1.Table中的配方行中使項目成爲語法。

回答

1

SrgsOneOf有一個構造函數,這需要string[]作爲參數:https://msdn.microsoft.com/en-us/library/ms554280(v=vs.110).aspx

您當前的代碼試圖表轉換爲字符串,這是行不通的。相反,您可以通過迭代表來創建一個字符串數組。

從帖子中不清楚該表是包含單獨的單詞/短語作爲行,還是包含單詞/短語列表的單身行。

假設每行有一個單詞或短語

例子:

DataTable 
--------- 
red 
green 
blue 
indigo 

有多種方法可以做到這一點(創建一個ArrayList,使用LINQ),但這裏有一個非常基本的方法:

Dim rowCount As Integer = DataSet1.Table.Rows.Count 
Dim arrWords(rowCount) As String 
For i = 0 to rowCount - 1 
    arrWords(i)=DataSet1.Table.Rows(i)(0).ToString(); 
Next 

Dim colorsList As New Recognition.SrgsGrammar.SrgsOneOf(arrWords) 

假設有一行包含所有單詞/短語

在這種情況下,我假設它們是用逗號分隔的。

實施例:

DataTable 
--------- 
red,green,blue,indigo 

在這種情況下,只需使用分割來創建陣列:

Dim strWords as String = DataSet1.Table.Rows(i)(0).ToString() 
Dim arrwords As String() = strWords.Split(New Char() {","c}) 

Dim colorsList As New Recognition.SrgsGrammar.SrgsOneOf(arrWords)