-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpp.tmpl
More file actions
75 lines (69 loc) · 1.79 KB
/
Copy pathcpp.tmpl
File metadata and controls
75 lines (69 loc) · 1.79 KB
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
FILE lib.hpp BEGIN
#include "lib/proteus.hpp"
#include <map>
#include <string>
#include <functional>
typedef std::function<void(std::string &)> callback_t;
class Recognizer {
prot_t backend;
std::map<std::string, std::string > word_id; // utterance -> id
std::multimap<std::string, callback_t> id_handler; // id -> callback
callback_t errorFunc;
public:
Recognizer(const std::string & lmDir);
std::string recognize_one();
void register_callback(std::function<void(std::string &)> func, const std::string &id );
void set_error_callback(callback_t f);
~Recognizer();
};
FILE END
FILE lib.cpp BEGIN
const char * grammarFile = "THE_GRAMMAR";
const char * dictFile = "THE_DICT";
#include "lib.hpp"
Recognizer::Recognizer(const std::string & lmDir) {
std::string gramPath(lmDir + "/" + grammarFile);
std::string dictPath(lmDir + "/" + dictFile);
backend = prot_init(gramPath.c_str(), dictPath.c_str());
// populate it
EXPAND_ME_BEGIN
word_id[std::string("COMMAND_EX")] = std::string("ID_EX");
EXPAND_ME_END
}
std::string Recognizer::recognize_one() {
std::string hypStr;
char * hyp = recog_word( backend );
if(hyp != nullptr) {
std::string hypStr = hyp;
delete[] hyp;
if (word_id.count(hyp) != 0) {
auto id = word_id[hypStr];
auto range = id_handler.equal_range(id);
while(range.first != range.second) {
(range.first->second)(id);
++range.first;
}
}else {
// unknown string
if(errorFunc){
errorFunc(hypStr);
}
}
return hypStr;
}else {
if(errorFunc){
errorFunc(hypStr);
}
return hypStr;
}
}
void Recognizer::register_callback(callback_t func, const std::string &id ){
id_handler.emplace(std::make_pair(id, func));
}
void Recognizer::set_error_callback(callback_t f) {
errorFunc = f;
}
Recognizer::~Recognizer() {
//prot_free( backend );
}
FILE END