Magic09
asked on
One Question
I am making a speech recognition program in Visual Basic 6.0, and since I am a beginner, I completely messed up a certain part of the code that is in a module. I am wondering if anyone could help me fix this code up, so I can get my program to work. DICTIONARY.txt is a dictionary saved as a text file in which every single line is another word. I have more code in the form of my program, but this is where my problem is. I want my program to be able to type in a textbox called Form1.T1.Text what you say when you say it into a microphone. Here is the code that is in a module, and thanks in advanced!
Option Explicit
Function LoadVoiceCommands(lstBox As Control) As Boolean
On Local Error GoTo LoadVoiceCommandsError
Dim sNam As String
Dim FileFree As Integer
FileFree = FreeFile
'Check for voice commands text file...
If Dir$(App.Path & "\DICTONARY.Txt") = "" Then
Exit Function
End If
Dim fnum
Dim Counter
Dim words
'Find next free file
fnum = FreeFile
Counter = 0
Open App.Path & "DICTIONARY.txt" For Input As #fnum
While Not EOF(fnum)
Input #fnum, words, Counter
Counter = Counter + 1
Wend
Close #fnum
LoadVoiceCommands = True
Exit Function
LoadVoiceCommandsError:
Exit Function
End Function
Function ExecuteVoiceCommand(Who As Form, sPhrase As String) As Boolean
On Local Error GoTo ExecuteVoiceCommandError
Select Case LCase$(sPhrase)
Dim X
'Carry out command...
Case "test"
Form1.T1.Text = "test"
Case "exit"
End
Case "read"
Form1.Talk1.Speak Form1.T1.Text
Form1.Talk1.Resume
Case "pause"
Form1.Talk1.Pause
Case "off"
Call Form1.VR.Deactivate
Case "save"
Form1.CommonDialog1.Cancel Error = True
On Error GoTo Errhandler
Form1.Save_DriveA
Case "print"
Form1.CommonDialog1.Cancel Error = True
On Error GoTo Errhandler
Form1.CommonDialog1.ShowPr inter
Printer.Print "Ultra Speak 2.0"
Printer.Print ""
Printer.Print ""
Printer.Print ""
Printer.Print Form1.T1.Text
Case "open"
Form1.CommonDialog1.Cancel Error = True
On Error GoTo Errhandler 'You must know the file extensions for the open types'
Form1.CommonDialog1.Filter = "All Files (*)|*|" & _
"Text Files (*.txt)|*.txt|" & _
"Rich Text Format (*.rtf)|*.rtf|"
Form1.CommonDialog1.Filter Index = 1 'The filter determines which type of files can be viewed, some types can't be viewed without calling dll's, etc'
Form1.CommonDialog1.ShowOp en
Form1.T1.Text = Form1.CommonDialog1.FileNa me
Open Form1.T1.Text For Input As #1
Form1.T1.Text = Input(LOF(1), 1)
Close #1
Errhandler:
End Select
End Function
Option Explicit
Function LoadVoiceCommands(lstBox As Control) As Boolean
On Local Error GoTo LoadVoiceCommandsError
Dim sNam As String
Dim FileFree As Integer
FileFree = FreeFile
'Check for voice commands text file...
If Dir$(App.Path & "\DICTONARY.Txt") = "" Then
Exit Function
End If
Dim fnum
Dim Counter
Dim words
'Find next free file
fnum = FreeFile
Counter = 0
Open App.Path & "DICTIONARY.txt" For Input As #fnum
While Not EOF(fnum)
Input #fnum, words, Counter
Counter = Counter + 1
Wend
Close #fnum
LoadVoiceCommands = True
Exit Function
LoadVoiceCommandsError:
Exit Function
End Function
Function ExecuteVoiceCommand(Who As Form, sPhrase As String) As Boolean
On Local Error GoTo ExecuteVoiceCommandError
Select Case LCase$(sPhrase)
Dim X
'Carry out command...
Case "test"
Form1.T1.Text = "test"
Case "exit"
End
Case "read"
Form1.Talk1.Speak Form1.T1.Text
Form1.Talk1.Resume
Case "pause"
Form1.Talk1.Pause
Case "off"
Call Form1.VR.Deactivate
Case "save"
Form1.CommonDialog1.Cancel
On Error GoTo Errhandler
Form1.Save_DriveA
Case "print"
Form1.CommonDialog1.Cancel
On Error GoTo Errhandler
Form1.CommonDialog1.ShowPr
Printer.Print "Ultra Speak 2.0"
Printer.Print ""
Printer.Print ""
Printer.Print ""
Printer.Print Form1.T1.Text
Case "open"
Form1.CommonDialog1.Cancel
On Error GoTo Errhandler 'You must know the file extensions for the open types'
Form1.CommonDialog1.Filter
"Text Files (*.txt)|*.txt|" & _
"Rich Text Format (*.rtf)|*.rtf|"
Form1.CommonDialog1.Filter
Form1.CommonDialog1.ShowOp
Form1.T1.Text = Form1.CommonDialog1.FileNa
Open Form1.T1.Text For Input As #1
Form1.T1.Text = Input(LOF(1), 1)
Close #1
Errhandler:
End Select
End Function
Oh btw just in case you don't know, an array starts at 0, so declaring the Words() array as (10000) will actaully mean it has 10001 entries (0 - 10000)
This is why in that Open->Close section (which seems to be from an answer I gave on a previous question lol), Counter starts at 0, not 1
This is why in that Open->Close section (which seems to be from an answer I gave on a previous question lol), Counter starts at 0, not 1
ASKER
Here is the rest of my code... But if I did a routine, wouldn't I have to write a routine for every word in the dictionary?
Dim a As Boolean
Dim b As Integer
Dim c As Integer
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComCtlLib.Button)
On Error Resume Next
Select Case Button.Key
Case "New"
'ToDo: Add 'New' button code.
MsgBox "Add 'New' button code."
Case "Open"
'ToDo: Add 'Open' button code.
MsgBox "Add 'Open' button code."
Case "Save"
'ToDo: Add 'Save' button code.
MsgBox "Add 'Save' button code."
Case "Print"
'ToDo: Add 'Print' button code.
MsgBox "Add 'Print' button code."
Case "Undo"
'ToDo: Add 'Undo' button code.
MsgBox "Add 'Undo' button code."
Case "Copy"
'ToDo: Add 'Copy' button code.
MsgBox "Add 'Copy' button code."
Case "Cut"
'ToDo: Add 'Cut' button code.
MsgBox "Add 'Cut' button code."
Case "Paste"
'ToDo: Add 'Paste' button code.
MsgBox "Add 'Paste' button code."
Case "Bold"
'ToDo: Add 'Bold' button code.
MsgBox "Add 'Bold' button code."
Case "Italic"
'ToDo: Add 'Italic' button code.
MsgBox "Add 'Italic' button code."
Case "Strike Through"
'ToDo: Add 'Strike Through' button code.
MsgBox "Add 'Strike Through' button code."
Case "Underline"
'ToDo: Add 'Underline' button code.
MsgBox "Add 'Underline' button code."
End Select
End Sub
Private Sub Command1_Click()
Call VR.Deactivate
End Sub
Private Sub Command2_Click()
Save_DriveA
End Sub
Public Sub Command3_Click()
CommonDialog1.CancelError = True
On Error GoTo Errhandler 'You must know the file extensions for the open types'
CommonDialog1.Filter = "All Files (*)|*|" & _
"Text Files (*.txt)|*.txt|" & _
"Rich Text Format (*.rtf)|*.rtf|"
CommonDialog1.FilterIndex = 1 'The filter determines which type of files can be viewed, some types can't be viewed without calling dll's, etc'
CommonDialog1.ShowOpen
StrFileName = CommonDialog1.FileName
Open StrFileName For Input As #1
T1.Text = Input(LOF(1), 1)
Close #1
Exit Sub
Errhandler:
End Sub
Private Sub Command4_Click()
Call VR.GrammarFromFile(App.Pat h & "\Commands.Txt")
Call VR.Activate
End Sub
Private Sub Command5_Click()
Form1.CommonDialog1.Cancel Error = True
On Error GoTo Errhandler
CommonDialog1.ShowPrinter
Printer.Print "Ultra Speak 2.0"
Printer.Print ""
Printer.Print ""
Printer.Print ""
Printer.Print T1.Text
Errhandler:
End Sub
Private Sub Command6_Click()
Talk1.Speak T1.Text
Talk1.Resume
End Sub
Private Sub Command7_Click()
Unload Form1
End Sub
Private Sub Command8_Click()
Talk1.Pause
End Sub
Private Sub Command9_Click()
End
End Sub
Private Sub Form_Load()
Dim M1
Dim WordsIndex As Integer
Dim Words(349899) As String
On Local Error Resume Next
'Load the voice commands from the commands text file...
Call LoadVoiceCommands(lstComma nds)
'Setup the voice recognition control...
Call VR.Deactivate
Call VR.GrammarFromFile(App.Pat h & "\DICTIONARY.Txt")
Call VR.Activate
T1.Text = Words(WordsIndex)
End Sub
Private Sub StatusBar1_PanelClick(ByVa l Panel As MSComCtlLib.Panel)
End Sub
Private Sub Label2_Click()
End
End Sub
Private Sub VR_PhraseFinish(ByVal flags As Long, ByVal beginhi As Long, ByVal beginlo As Long, ByVal endhi As Long, ByVal endlo As Long, ByVal Phrase As String, ByVal parsed As String, ByVal results As Long)
On Local Error Resume Next
'Check to see if we have a matching word...
If Trim$(Phrase) <> "" Then
Me.Caption = Phrase
Call ExecuteVoiceCommand(Me, Phrase)
Else
Me.Caption = "Unrecognized Command..."
End If
End Sub
Private Sub Image5_Click()
Me.WindowState = 1
End Sub
Private Sub DirectSR1_ClickIn(ByVal X As Long, ByVal y As Long)
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)
If Button = vbRightButton Then
Else
a = True
b = X
c = y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, y As Single)
a = False
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)
If a Then
Me.Move (Me.Left + X - b), (Me.Top + y - c)
End If
End Sub
Private Sub Dm_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)
If a Then Me.Move (Me.Left + X - b), (Me.Top + y - c)
End Sub
Private Sub Dm_MouseUp(Button As Integer, Shift As Integer, X As Single, y As Single)
a = False
End Sub
Function Save_DriveA()
Dim X As Integer
CommonDialog1.CancelError = True
On Error GoTo Errhandler
Do
CommonDialog1.Action = 2 'save file
Exit Do
DoEvents
Loop
X = FreeFile
Open CommonDialog1.FileName For Output As #X
Print #X, T1.Text
Close #X
MsgBox "Your file has been saved."
Errhandler:
End
End Function
Function Print_T1()
Dim Z As Integer
Do
CommonDialog1.Action = 5 'save file
Exit Do
DoEvents
Loop
Print "T1.Text"
Close #X
MsgBox "Printing..."
End Function
Dim a As Boolean
Dim b As Integer
Dim c As Integer
Private Sub Toolbar1_ButtonClick(ByVal
On Error Resume Next
Select Case Button.Key
Case "New"
'ToDo: Add 'New' button code.
MsgBox "Add 'New' button code."
Case "Open"
'ToDo: Add 'Open' button code.
MsgBox "Add 'Open' button code."
Case "Save"
'ToDo: Add 'Save' button code.
MsgBox "Add 'Save' button code."
Case "Print"
'ToDo: Add 'Print' button code.
MsgBox "Add 'Print' button code."
Case "Undo"
'ToDo: Add 'Undo' button code.
MsgBox "Add 'Undo' button code."
Case "Copy"
'ToDo: Add 'Copy' button code.
MsgBox "Add 'Copy' button code."
Case "Cut"
'ToDo: Add 'Cut' button code.
MsgBox "Add 'Cut' button code."
Case "Paste"
'ToDo: Add 'Paste' button code.
MsgBox "Add 'Paste' button code."
Case "Bold"
'ToDo: Add 'Bold' button code.
MsgBox "Add 'Bold' button code."
Case "Italic"
'ToDo: Add 'Italic' button code.
MsgBox "Add 'Italic' button code."
Case "Strike Through"
'ToDo: Add 'Strike Through' button code.
MsgBox "Add 'Strike Through' button code."
Case "Underline"
'ToDo: Add 'Underline' button code.
MsgBox "Add 'Underline' button code."
End Select
End Sub
Private Sub Command1_Click()
Call VR.Deactivate
End Sub
Private Sub Command2_Click()
Save_DriveA
End Sub
Public Sub Command3_Click()
CommonDialog1.CancelError = True
On Error GoTo Errhandler 'You must know the file extensions for the open types'
CommonDialog1.Filter = "All Files (*)|*|" & _
"Text Files (*.txt)|*.txt|" & _
"Rich Text Format (*.rtf)|*.rtf|"
CommonDialog1.FilterIndex = 1 'The filter determines which type of files can be viewed, some types can't be viewed without calling dll's, etc'
CommonDialog1.ShowOpen
StrFileName = CommonDialog1.FileName
Open StrFileName For Input As #1
T1.Text = Input(LOF(1), 1)
Close #1
Exit Sub
Errhandler:
End Sub
Private Sub Command4_Click()
Call VR.GrammarFromFile(App.Pat
Call VR.Activate
End Sub
Private Sub Command5_Click()
Form1.CommonDialog1.Cancel
On Error GoTo Errhandler
CommonDialog1.ShowPrinter
Printer.Print "Ultra Speak 2.0"
Printer.Print ""
Printer.Print ""
Printer.Print ""
Printer.Print T1.Text
Errhandler:
End Sub
Private Sub Command6_Click()
Talk1.Speak T1.Text
Talk1.Resume
End Sub
Private Sub Command7_Click()
Unload Form1
End Sub
Private Sub Command8_Click()
Talk1.Pause
End Sub
Private Sub Command9_Click()
End
End Sub
Private Sub Form_Load()
Dim M1
Dim WordsIndex As Integer
Dim Words(349899) As String
On Local Error Resume Next
'Load the voice commands from the commands text file...
Call LoadVoiceCommands(lstComma
'Setup the voice recognition control...
Call VR.Deactivate
Call VR.GrammarFromFile(App.Pat
Call VR.Activate
T1.Text = Words(WordsIndex)
End Sub
Private Sub StatusBar1_PanelClick(ByVa
End Sub
Private Sub Label2_Click()
End
End Sub
Private Sub VR_PhraseFinish(ByVal flags As Long, ByVal beginhi As Long, ByVal beginlo As Long, ByVal endhi As Long, ByVal endlo As Long, ByVal Phrase As String, ByVal parsed As String, ByVal results As Long)
On Local Error Resume Next
'Check to see if we have a matching word...
If Trim$(Phrase) <> "" Then
Me.Caption = Phrase
Call ExecuteVoiceCommand(Me, Phrase)
Else
Me.Caption = "Unrecognized Command..."
End If
End Sub
Private Sub Image5_Click()
Me.WindowState = 1
End Sub
Private Sub DirectSR1_ClickIn(ByVal X As Long, ByVal y As Long)
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)
If Button = vbRightButton Then
Else
a = True
b = X
c = y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, y As Single)
a = False
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)
If a Then
Me.Move (Me.Left + X - b), (Me.Top + y - c)
End If
End Sub
Private Sub Dm_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)
If a Then Me.Move (Me.Left + X - b), (Me.Top + y - c)
End Sub
Private Sub Dm_MouseUp(Button As Integer, Shift As Integer, X As Single, y As Single)
a = False
End Sub
Function Save_DriveA()
Dim X As Integer
CommonDialog1.CancelError = True
On Error GoTo Errhandler
Do
CommonDialog1.Action = 2 'save file
Exit Do
DoEvents
Loop
X = FreeFile
Open CommonDialog1.FileName For Output As #X
Print #X, T1.Text
Close #X
MsgBox "Your file has been saved."
Errhandler:
End
End Function
Function Print_T1()
Dim Z As Integer
Do
CommonDialog1.Action = 5 'save file
Exit Do
DoEvents
Loop
Print "T1.Text"
Close #X
MsgBox "Printing..."
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
He solves the problem once again! Rofl :-)
'Input #fnum, words, Counter'
should read
Input #fnum, words(Counter)
and also words should be declared like this:
Dim Words(10000) As String
where 10000 should be changed to suit number of words in your dictionary.
As for the question...
I'm guessing that somewhere you have a routine that matches the spoken word to an entry to your *words*, but with out seeing more of your code, I'm a bit lost :(
This routine should return an integer value which represents the location of the word within the Words() array. (I will call this value WordsIndex)
You can then simply use
Form.T1.Text = Words(WordsIndex)
Hope that helps....Good luck :)