From 54ff389b0957c5230ff6bba4aaa4b0c2710ec26d Mon Sep 17 00:00:00 2001 From: Brian West Date: Sat, 21 Feb 2009 22:47:15 +0000 Subject: [PATCH] Revamp mod_pocketsphinx to use jsgf format. Known bugs which are going to be fixed are: Using words in your grammar that aren't in the dictionary will segfault. Bug is already brought up with the pocketsphinx maintainer. /b git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12224 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- scripts/yes_no.gram | 14 ++++++++++++ src/mod/asr_tts/mod_pocketsphinx/Makefile | 15 +++++-------- .../mod_pocketsphinx/mod_pocketsphinx.c | 22 +++++++++---------- 3 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 scripts/yes_no.gram diff --git a/scripts/yes_no.gram b/scripts/yes_no.gram new file mode 100644 index 0000000000..463df15948 --- /dev/null +++ b/scripts/yes_no.gram @@ -0,0 +1,14 @@ +#JSGF V1.0; + +/** + * JSGF Grammar for example + */ + +grammar example; + + = [ yes ]; + = [ no ]; + +public = [ | ]; + + diff --git a/src/mod/asr_tts/mod_pocketsphinx/Makefile b/src/mod/asr_tts/mod_pocketsphinx/Makefile index b1a7a24c63..4764f31671 100644 --- a/src/mod/asr_tts/mod_pocketsphinx/Makefile +++ b/src/mod/asr_tts/mod_pocketsphinx/Makefile @@ -15,7 +15,7 @@ BASE=../../../.. include $(BASE)/build/modmake.rules $(SPHINXBASE_DIR): - $(GETLIB) $(SPHINXBASE).tar.gz + $(GETLIB) $(SPHINXBASE)-latest.tar.gz $(SPHINXBASE_DIR)/buildstamp: $(SPHINXBASE_DIR) test -f $(SPHINXBASE_DIR)/Makefile || (cd $(SPHINXBASE_DIR) && ./configure --with-pic --without-python --disable-shared CFLAGS=) @@ -26,7 +26,7 @@ $(SPHINXBASE_LA): $(SPHINXBASE_DIR)/buildstamp $(TOUCH_TARGET) $(POCKETSPHINX_DIR): - $(GETLIB) $(POCKETSPHINX).tar.gz + $(GETLIB) $(POCKETSPHINX)-latest.tar.gz $(POCKETSPHINX_DIR)/Makefile: $(POCKETSPHINX_DIR) cd $(POCKETSPHINX_DIR) && ./configure --with-pic --without-python --disable-shared --with-sphinxbase=$(SPHINXBASE_DIR) CFLAGS= @@ -51,12 +51,7 @@ $(DESTDIR)$(PREFIX)/grammar/model/wsj1: mkdir -p $(DESTDIR)$(PREFIX)/grammar/model/wsj1 mv $(POCKETSPHINX_DIR)/model/hmm/wsj1/* $(DESTDIR)$(PREFIX)/grammar/model/wsj1 -grammar_maker: - @cp -f $(BASE)/scripts/mklm $(DESTDIR)$(PREFIX)/bin/mklm - @cp -f $(BASE)/scripts/quick_lm.pl $(DESTDIR)$(PREFIX)/bin/quick_lm.pl - @chmod 755 $(DESTDIR)$(PREFIX)/bin/mklm - @chmod 755 $(DESTDIR)$(PREFIX)/bin/quick_lm.pl - @cp -f $(BASE)/scripts/Makefile.gram $(DESTDIR)$(PREFIX)/grammar/Makefile - @cp -f $(POCKETSPHINX_DIR)/model/lm/cmudict.0.6d $(DESTDIR)$(PREFIX)/conf/cmudict.0.6d +dictionary: + @cp -f $(POCKETSPHINX_DIR)/model/lm/cmudict.0.6d $(DESTDIR)$(PREFIX)/grammar/default.dic -local_install: $(DESTDIR)$(PREFIX)/grammar/model $(DESTDIR)$(PREFIX)/grammar/model/communicator $(DESTDIR)$(PREFIX)/grammar/model/wsj1 grammar_maker +local_install: $(DESTDIR)$(PREFIX)/grammar/model $(DESTDIR)$(PREFIX)/grammar/model/communicator $(DESTDIR)$(PREFIX)/grammar/model/wsj1 dictionary diff --git a/src/mod/asr_tts/mod_pocketsphinx/mod_pocketsphinx.c b/src/mod/asr_tts/mod_pocketsphinx/mod_pocketsphinx.c index 01a665a026..f5e015a7c1 100644 --- a/src/mod/asr_tts/mod_pocketsphinx/mod_pocketsphinx.c +++ b/src/mod/asr_tts/mod_pocketsphinx/mod_pocketsphinx.c @@ -113,7 +113,7 @@ static switch_status_t pocketsphinx_asr_open(switch_asr_handle_t *ah, const char /*! function to load a grammar to the asr interface */ static switch_status_t pocketsphinx_asr_load_grammar(switch_asr_handle_t *ah, const char *grammar, const char *path) { - char *lm, *dic, *model, *rate = NULL; + char *jsgf, *dic, *model, *rate = NULL; pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info; switch_status_t status = SWITCH_STATUS_FALSE; @@ -123,11 +123,9 @@ static switch_status_t pocketsphinx_asr_load_grammar(switch_asr_handle_t *ah, co } if (switch_is_file_path(grammar)) { - lm = switch_mprintf("%s%s%s.lm", grammar, SWITCH_PATH_SEPARATOR, grammar); - dic = switch_mprintf("%s%s%s.dic", grammar, SWITCH_PATH_SEPARATOR, grammar); + jsgf = switch_mprintf("%s.gram", grammar); } else { - lm = switch_mprintf("%s%s%s%s%s.lm", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, grammar, SWITCH_PATH_SEPARATOR, grammar); - dic = switch_mprintf("%s%s%s%s%s.dic", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, grammar, SWITCH_PATH_SEPARATOR, grammar); + jsgf = switch_mprintf("%s%s%s.gram", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, grammar); } if (ah->rate == 8000) { @@ -135,24 +133,27 @@ static switch_status_t pocketsphinx_asr_load_grammar(switch_asr_handle_t *ah, co } else { model = switch_mprintf("%s%smodel%s%s", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, SWITCH_PATH_SEPARATOR, globals.model16k); } + + dic = switch_mprintf("%s%sdefault.dic", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, grammar); + if (switch_file_exists(dic, ah->memory_pool) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't open dictionary %s.\n", dic); goto end; } - if (switch_file_exists(lm, ah->memory_pool) != SWITCH_STATUS_SUCCESS) { + if (switch_file_exists(jsgf, ah->memory_pool) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't open language model %s.\n", model); goto end; } rate = switch_mprintf("%d", ah->rate); - switch_assert(lm && dic && model); + switch_assert(jsgf && dic && model); ps->config = cmd_ln_init(ps->config, ps_args(), FALSE, "-samprate", rate, "-hmm", model, - "-lm", lm, + "-jsgf", jsgf, "-dict", dic, "-frate", "50", "-silprob", "0.005", @@ -185,7 +186,7 @@ static switch_status_t pocketsphinx_asr_load_grammar(switch_asr_handle_t *ah, co end: switch_safe_free(rate); - switch_safe_free(lm); + switch_safe_free(jsgf); switch_safe_free(dic); switch_safe_free(model); @@ -201,15 +202,12 @@ static switch_status_t pocketsphinx_asr_unload_grammar(switch_asr_handle_t *ah, /*! function to close the asr interface */ static switch_status_t pocketsphinx_asr_close(switch_asr_handle_t *ah, switch_asr_flag_t *flags) { - char const *hyp; - int32_t score; pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info; switch_mutex_lock(ps->flag_mutex); if (switch_test_flag(ps, PSFLAG_ALLOCATED)) { if (switch_test_flag(ps, PSFLAG_READY)) { ps_end_utt(ps->ps); - hyp = ps_get_hyp(ps->ps, &score, &ps->uttid); } ps_free(ps->ps); ps->ps = NULL;