Skip to content

Commit

Permalink
working on C wrapper for srgs
Browse files Browse the repository at this point in the history
  • Loading branch information
crienzo committed Jan 13, 2014
1 parent 9069ee6 commit 2842344
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 23 deletions.
3 changes: 1 addition & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ libcspeech_@CSPEECH_API_VERSION@_la_LDFLAGS = -version-info $(CSPEECH_SO_VERSION
## source tree matches the hierarchy at the install location, however.
cspeech_includedir = $(includedir)/cspeech-$(CSPEECH_API_VERSION)
nobase_cspeech_include_HEADERS = cspeech.h \
cspeech/nlsml.h \
cspeech/srgs.h
cspeech/cspeech_private.h

## The generated configuration header is installed in its own subdirectory of
## $(libdir). The reason for this is that the configuration information put
Expand Down
9 changes: 9 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ Speech document (SSML, SRGS, NLSML) modelling and matching for C

* PCRE
* [iksemel](https://code.google.com/p/iksemel/)

# To build
aclocal
libtoolize
autoreconf
automake --add-missing
./configure
make

95 changes: 95 additions & 0 deletions cspeech.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,98 @@
* cspeech.cc
*
*/
#include "cspeech.h"
#include "cspeech/cspeech_private.h"

struct cspeech_srgs_parser {
struct srgs_parser *parser;
};

struct cspeech_srgs_grammar {
struct srgs_grammar *grammar;
}

int cspeech_init(void)
{
srgs_init();
}

struct cspeech_srgs_parser *cspeech_srgs_parser_new(const char *uuid)
{
cspeech_srgs_parser *parser = new cspeech_srgs_parser;
std::string u;
if (!cspeech_zstr(uuid)) {
u = uuid;
}
parser->parser = new srgs_parser(u);
return parser;
}

struct cspeech_srgs_grammar *cspeech_srgs_parse(struct cspeech_srgs_parser *parser, const char *document)
{
std::string doc;
if (!parser) {
return NULL;
}
if (!cspeech_zstr(document)) {
doc = document;
}
srgs_grammar *g = parser->parser->parse(doc);
if (g) {
cspeech_srgs_grammar *grammar = new cspeech_srgs_grammar;
grammar->grammar = g;
return grammar;
}
return NULL;
}

const char *cspeech_srgs_grammar_to_jsgf(struct cspeech_srgs_grammar *grammar)
{
if (!grammar) {
return NULL;
}
const str::string &jsgf = grammar->grammar->to_jsgf();
if (jsgf == "") {
return NULL;
}
return jsgf.c_str();
}

const char *cspeech_srgs_grammar_to_jsgf_file(struct cspeech_srgs_grammar *grammar, const char *basedir, const char *ext)
{
if (!grammar || !basedir || !ext) {
return NULL;
}
const str::string &jsgf_file = grammar->grammar->to_jsgf_file(basedir_str, ext_str);
if (jsgf_file == "") {
return NULL;
}
return jsgf_file.c_str();
}

enum srgs_match_type cspeech_srgs_grammar_match(struct cspeech_srgs_grammar *grammar, const char *input, const char **interpretation)
{
std::string input_str;
std::string interpretation_str;
*interpretation = NULL;
if (!cspeech_zstr(input)) {
input_str = input;
}
enum_srgs_match_type match = grammar->grammar->match(input_str, interpretation_str);
if (interpretation_str != "") {
*interpretation = strdup(interpretation_str.c_str());
}
return match;
}

void cspeech_srgs_grammar_destroy(struct cspeech_srgs_grammar *grammar)
{
delete grammar;
}

void cspeech_srgs_parser_destroy(struct cspeech_srgs_parser *parser)
{
delete parser->parser;
delete parser;
}

39 changes: 23 additions & 16 deletions cspeech.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#ifndef CSPEECH_H_
#define CSPEECH_H_

#ifdef __cplusplus
extern "C" {
#endif

int cspeech_init(void);

/* logging */

Expand All @@ -30,7 +34,7 @@ typedef enum {

typedef int (*cspeech_logging_callback)(void *context, cspeech_log_level_t log_level, const char *log_message, ...);


#if 0
/* NLSML */

enum nlsml_match_type {
Expand All @@ -44,32 +48,35 @@ extern int nlsml_init(void);
enum nlsml_match_type nlsml_parse(const char *result, const char *uuid);
iks *nlsml_normalize(const char *result);
extern iks *nlsml_create_dtmf_match(const char *digits, const char *interpretation);
#endif

/* SRGS */

struct srgs_parser;
struct srgs_grammar;
struct cspeech_srgs_parser;
struct cspeech_srgs_grammar;

enum srgs_match_type {
enum cspeech_srgs_match_type {
/** invalid input */
SMT_NO_MATCH,
CSMT_NO_MATCH,
/** matches, can accept more input */
SMT_MATCH,
CSMT_MATCH,
/** not yet a match, but valid input so far */
SMT_MATCH_PARTIAL,
CSMT_MATCH_PARTIAL,
/** matches, cannot accept more input */
SMT_MATCH_END
CSMT_MATCH_END
};

extern int srgs_init(void);
extern struct srgs_parser *srgs_parser_new(const char *uuid);
extern struct srgs_grammar *srgs_parse(struct srgs_parser *parser, const char *document);
extern const char *srgs_grammar_to_regex(struct srgs_grammar *grammar);
extern const char *srgs_grammar_to_jsgf(struct srgs_grammar *grammar);
extern const char *srgs_grammar_to_jsgf_file(struct srgs_grammar *grammar, const char *basedir, const char *ext);
extern enum srgs_match_type srgs_grammar_match(struct srgs_grammar *grammar, const char *input, const char **interpretation);
extern void srgs_parser_destroy(struct srgs_parser *parser);
struct cspeech_srgs_parser *cspeech_srgs_parser_new(const char *uuid);
struct cspeech_srgs_grammar *srgs_parse(struct cspeech_srgs_parser *parser, const char *document);
const char *cspeech_srgs_to_regex(struct cspeech_srgs_grammar *grammar);
const char *cspeech_srgs_to_jsgf(struct cspeech_srgs_grammar *grammar);
const char *cspeech_srgs_to_jsgf_file(struct cspeech_srgs_grammar *grammar, const char *basedir, const char *ext);
enum cspeech_srgs_match_type srgs_grammar_match(struct cspeech_srgs_grammar *grammar, const char *input, const char **interpretation);
void cspeech_srgs_grammar_destroy(struct cspeech_srgs_grammar *grammar);
void cspeech_srgs_parser_destroy(struct cspeech_srgs_parser *parser);

#ifdef __cplusplus
}
#endif

#endif // CSPEECH_H_
8 changes: 4 additions & 4 deletions test/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test_nlsml.c"
//#include "test_nlsml.c"
#include "test_srgs.c"

int main(int argc, char **argv)
Expand All @@ -7,9 +7,9 @@ int main(int argc, char **argv)
return -1;
}

if(test_srgs() != 0) {
return -1;
}
//if(test_srgs() != 0) {
// return -1;
// }

return 0;
}
2 changes: 2 additions & 0 deletions test/test_nlsml.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if 0
#include "test.h"
#include "cspeech/nlsml.h"

Expand Down Expand Up @@ -316,3 +317,4 @@ int test_nlsml()
TEST(test_normalize);
return 0;
}
#endif
2 changes: 1 addition & 1 deletion test/test_srgs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "test.h"
#include "cspeech/srgs.h"
#include "cspeech.h"

static const char *adhearsion_menu_grammar =
"<grammar xmlns=\"http://www.w3.org/2001/06/grammar\" version=\"1.0\" xml:lang=\"en-US\" mode=\"dtmf\" root=\"options\" tag-format=\"semantics/1.0-literals\">"
Expand Down

0 comments on commit 2842344

Please sign in to comment.