I am rather perplexed over how to use grammar files with Python's speech_recognition
package. I've tried hunting for solutions online, but information is sparse to say the least regarding grammars. Here's my setup:
main.py:
import speech_recognition as sr
converter = sr.Recognizer()
with sr.Microphone() as source:
print('Please say "zero":')
audio = converter.listen(source)
print('Without: ' + str(converter.recognize_sphinx(audio, show_all=False)))
print('With: ' + str(converter.recognize_sphinx(audio, show_all=False, grammar='Numbers.jsgf')))
Numbers.jsgf:
#JSGF V1.0;
grammar Numbers;
<Zero> = ( zero );
Output:
Please say "zero":
Without: zero
INFO: jsgf.c(706): Defined rule: <Numbers.g00000>
INFO: jsgf.c(706): Defined rule: <Numbers.Zero>
Process finished with exit code -1073741819 (0xC0000005)
I'm working in PyCharm, using Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
. Any ideas what's going on? Thanks in advance for your help!
After much tinkering, and comparing against some of the examples from the project page I finally discovered what the bug was. It boils down to a naming convention that isn't easy to find if you don't know to look for it or what you're looking for. This is an incorrect file:
Numbers.jsgf
#JSGF V1.0;
grammar Values;
<Zero> = ( zero );
And this is a correct file:
Numbers.jsgf
#JSGF V1.0;
grammar Numbers;
<Numbers> = ( zero );
To explain it simply:
<Numbers>
) with the same name as the file / grammar nameIf either of those conditions isn't met, then the program won't be able to understand the file and will crash. As a side note, .jsgf
and .gram
files are referencing the same coding standards, and the extension seems to come down to personal taste.