bambocher / pocketsphinx-python

Python interface to CMU Sphinxbase and Pocketsphinx libraries
https://pypi.python.org/pypi/pocketsphinx
Other
373 stars 187 forks source link

Grammar File Compiling Issue with Jsgf() #34

Closed jhoelzl closed 6 years ago

jhoelzl commented 6 years ago

Hello

i have a simple grammar file hello.gram like this:

#JSGF V1.0;

grammar hello;

public <hello> = (good morning | hello+ | good evening | hi )+ ( how are you | john | steve )* ;

Now i want to compile this file with pocketsphinx in order to get the corresponding fsg format:

import os
from pocketsphinx import pocketsphinx, Jsgf, FsgModel

model_path = 'speech_recognizing/lib/pocketsphinx/'
language = 'en-US'
grammar_file = 'hello.gram'

grammar_path = os.path.join(model_path, language, grammar_file)
fsg_path = "{}.fsg".format(grammar_path)

print("Grammar Filepath: {}".format(grammar_path))

# Create decoder object
config = pocketsphinx.Decoder.default_config()
config.set_string("-hmm", os.path.join(model_path, language, 'acoustic-model'))
config.set_string("-lm", os.path.join(model_path, language, 'language-model.lm.bin'))
config.set_string("-dict", os.path.join(model_path, language, 'pronounciation-dictionary.dict'))
config.set_string("-logfn", os.devnull) 
decoder = pocketsphinx.Decoder(config)

# Compile Grammar File
jsgf = Jsgf(grammar_path)
rule = jsgf.get_rule("{0}.{0}".format(grammar_path))
fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5)
fsg.writefile(fsg_path)

However, it always returns the error (in German language):

Speicherzugriffsfehler (Speicherabzug geschrieben).

So, this is the output of the script:

Grammar Filepath: speech_recognizing/lib/pocketsphinx/en-US/hello.gram INFO: jsgf.c(706): Defined rule: INFO: jsgf.c(706): Defined rule: INFO: jsgf.c(706): Defined rule: INFO: jsgf.c(706): Defined rule: INFO: jsgf.c(706): Defined rule: INFO: jsgf.c(706): Defined rule: PUBLIC Speicherzugriffsfehler (Speicherabzug geschrieben)

This also happens with other grammar file examples.

Ubuntu 14.04 Python 3.4.3

Any suggestions? Thanks for support!

nshmyrev commented 6 years ago

rule = jsgf.get_rule("{0}.{0}".format(grammar_path))

returns you null rule, so it fails, it should be

rule = jsgf.get_rule("hello.hello")

jhoelzl commented 6 years ago

@nshmyrev thanks to you. I had the whole file path in grammar_path, to it could not find the function in the grammar file.

jhoelzl commented 6 years ago

Hello again @nshmyrev,

it worked for a while, but now i have another error. This is my code:


import os
from pocketsphinx import pocketsphinx, Jsgf, FsgModel

grammar_file = 'contact'  # do not use any extension for this file
model_path = 'speech_recognizing/lib/pocketsphinx/'
language = 'de-DE'

grammar_path = os.path.join(model_path, language, grammar_file)
fsg_path = "{}.fsg".format(grammar_path)

print("Grammar Filepath: {}".format(grammar_path))
print("FSG Grammar Filepath: {}".format(fsg_path))

# Create decoder object
config = pocketsphinx.Decoder.default_config()
config.set_string("-hmm", os.path.join(model_path, language, 'acoustic-model'))
config.set_string("-lm", os.path.join(model_path, language, 'language-model.lm.bin'))
config.set_string("-dict", os.path.join(model_path, language, 'pronounciation-dictionary.dict'))
config.set_string("-logfn", os.devnull)
decoder = pocketsphinx.Decoder(config)

# Compile Grammar File
jsgf = Jsgf(grammar_path)
rule_string = "{0}.{0}".format(grammar_file)
rule = jsgf.get_rule(rule_string)
fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5)
fsg.writefile(fsg_path)

# Set grammar to decoder
decoder.set_fsg(grammar_file, fsg)
decoder.set_search(grammar_file)

The grammar file contact.fsg is build correctly from contact, but i always get an error setting the grammar to the decoder:

RuntimeError: Decoder_set_fsg returned -1

Output of this script is:

Grammar Filepath: speech_recognizing/lib/pocketsphinx/de-DE/contact FSG Grammar Filepath: speech_recognizing/lib/pocketsphinx/de-DE/contact.fsg INFO: jsgf.c(706): Defined rule: INFO: jsgf.c(706): Defined rule: PUBLIC INFO: fsg_model.c(208): Computing transitive closure for null transitions INFO: fsg_model.c(270): 0 null transitions added INFO: fsg_model.c(843): Writing FSG file 'speech_recognizing/lib/pocketsphinx/de-DE/contact.fsg' Traceback (most recent call last): File "speech_recognizing/pocketsphinx_test_rule.py", line 35, in decoder.set_fsg(grammar_file, fsg) File "/media/data/myproject/modules/venv/lib/python3.4/site-packages/pocketsphinx/pocketsphinx.py", line 313, in set_fsg return _pocketsphinx.Decoder_set_fsg(self, *args) RuntimeError: Decoder_set_fsg returned -1

The content of the grammar file contact is very simple:

#JSGF V1.0;

grammar contact;

public <contact> = (josef | michael | thomas | herbert | michael );

which leads to contact.fsg:

FSG_BEGIN <contact.contact>
NUM_STATES 2
START_STATE 0
FINAL_STATE 1
TRANSITION 0 1 1.000000 josef
TRANSITION 0 1 1.000000 thomas
TRANSITION 0 1 1.000000 herbert
TRANSITION 0 1 1.000000 michael
FSG_END

Any suggestions? Thanks!

jhoelzl commented 6 years ago

I could solve the issue. All words defined in the grammar file must also be included into the pronounciation-dictionary.dict and in an consistent form (upper and lower case).

msaccone commented 5 years ago

@jhoelzl is absolutelty right !

iProMC commented 4 years ago

hello, I have a similar to this issue when for example, this code in the contact file
`#JSGF V1.0;

grammar contact;

public = (josef | michael | thomas | herbert | michael );`

when I start the project it showed me these logs.

INFO: jsgf.c(709): Defined rule: INFO: jsgf.c(709): Defined rule: PUBLIC INFO: fsg_model.c(843): Writing FSG file 'C:\Users\iProMC\source\repos\Veronica\Veronica/grammar.fsg'

can any one help me please?