-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCVoiceProcess.cpp
More file actions
130 lines (111 loc) · 3.2 KB
/
CVoiceProcess.cpp
File metadata and controls
130 lines (111 loc) · 3.2 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "CVoiceProcess.h"
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
CVoiceProcess::CVoiceProcess()
{
debug = 0;
passthrough = false;
}
CVoiceProcess::~CVoiceProcess()
{
}
void CVoiceProcess::GetConfig(std::string config_file)
{
//fprintf(stderr, "Opening config file...\n");
ifstream file(config_file.c_str(), ios::in);
if (!file.is_open())
{
printf("Can't find config file.\n");
return;
}
string line;
int i = 1;
while (getline(file, line))
{
boost::algorithm::replace_last(line, "\t", " ");
boost::algorithm::replace_all(line, "\t", "");
boost::algorithm::replace_all(line, "\r", "");
boost::algorithm::replace_all(line, "\n", "");
boost::algorithm::replace_all(line, "*", ".*");
unsigned int loc = line.find("==");
if (loc < 500 && loc != string::npos && line[0] != '#')
{
//This isn't a comment and is formatted properly
string v = line.substr(0, loc);
string c = line.substr(loc + 2);
//to_upper(v);
voice.push_back(v);
commands.push_back(c);
}
else if (line[0] != '#')
{
printf("Formatting error on line %d\n", i);
}
++i;
}
file.close();
}
CVoiceCommand CVoiceProcess::ProcessMessage(std::string message) {
CVoiceCommand vcmd;
unsigned int i = 0, loc = 0;
string tmp = message;
string sTmp = message;
to_lower(sTmp);
string rawCommand = "";
vcmd.mMessage = message;
while (i < voice.size())
{
loc = sTmp.find(voice[i]);
if (loc == 0)
{
tmp = commands[i];
rawCommand = tmp.c_str();
printf("command2: %s\n", tmp.c_str());
break;
}
else if (voice[i][0] == '~')
{
// see whether the voice keyword is *anywhere* in the message
string v = voice[i].substr(1, string::npos);
loc = sTmp.find(v);
//printf("v: %s\tloc: %d\tsTmp: %s\n",v.c_str(),loc,sTmp.c_str());
if (loc != string::npos && loc != -1)
{
// if it does, return
rawCommand = commands[i].c_str();
printf("command3: %s\n", commands[i].c_str());
break;
}
}
else
{
boost::regex expr{ voice[i] };
bool b = boost::regex_match(sTmp, expr);
//std::cout << std::boolalpha << b << '\n';
if (b)
{
rawCommand = commands[i].c_str();
}
//std::string s = "a what's this ddds";
//boost::regex expr{".* dddd" };
//std::cout << std::boolalpha << boost::regex_match(s, expr) << '\n';
}
++i;
}
printf("rawCommand = %s\n", (char*)rawCommand.c_str());
const char c[2] = ":";
char* tok;
tok = strtok((char*)rawCommand.c_str(), c);
if (tok != NULL)
{
vcmd.mCommand = tok;
tok = strtok(NULL, c);
if (tok != NULL)
{
vcmd.mArgument = tok;
}
}
return(vcmd);
}