forked from adhearsion/cspeech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cspeech.cc
109 lines (97 loc) · 2.42 KB
/
cspeech.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* cspeech - Speech document (SSML, SRGS, NLSML) modelling and matching for C
* Copyright (C) 2013, Grasshopper
*
* License: MIT
*
* Contributor(s):
* Chris Rienzo <[email protected]>
*
* cspeech.cc
*
*/
#include "cspeech.h"
#include "cspeech/cspeech_private.h"
#include <string.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 std::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 || cspeech_zstr(basedir) || cspeech_zstr(ext)) {
return NULL;
}
const std::string &jsgf_file = grammar->grammar->to_jsgf_file(std::string(basedir), std::string(ext));
if (jsgf_file == "") {
return NULL;
}
return jsgf_file.c_str();
}
enum cspeech_srgs_match_type cspeech_srgs_grammar_match(struct cspeech_srgs_grammar *grammar, const char *input, char **interpretation)
{
std::string input_str;
std::string interpretation_str;
*interpretation = NULL;
if (!cspeech_zstr(input)) {
input_str = input;
}
enum cspeech_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;
}