forked from wit-ai/pywit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpywit.c
More file actions
executable file
·211 lines (194 loc) · 5.64 KB
/
pywit.c
File metadata and controls
executable file
·211 lines (194 loc) · 5.64 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <Python.h>
#include "wit.h"
struct wit_context *context;
static PyObject *WitError;
static PyObject *pywit_init(PyObject *self, PyObject *args)
{
const char *device_opt = NULL;
if (!PyArg_ParseTuple(args, "|s", &device_opt))
return NULL;
context = wit_init(device_opt, 4);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pywit_close()
{
if (context != NULL)
wit_close(context);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pywit_text_query(PyObject *self, PyObject *args)
{
const char *text;
const char *access_token;
char *res;
if (context == NULL) {
PyErr_SetString(WitError, "Wit context uninitialized (did you call wit.init()?)");
return NULL;
}
if (!PyArg_ParseTuple(args, "ss", &text, &access_token))
return NULL;
res = wit_text_query(context, text, access_token);
PyObject *obj = Py_BuildValue("s", res);
Py_XDECREF(res);
return obj;
}
static PyObject *pywit_voice_query_start(PyObject *self, PyObject *args)
{
const char *access_token;
if (context == NULL) {
PyErr_SetString(WitError, "Wit context uninitialized (did you call wit.init()?)");
return NULL;
}
if (!PyArg_ParseTuple(args, "s", &access_token))
return NULL;
wit_voice_query_start(context, access_token);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pywit_voice_query_stop()
{
char *res;
if (context == NULL) {
PyErr_SetString(WitError, "Wit context uninitialized (did you call wit.init()?)");
return NULL;
}
res = wit_voice_query_stop(context);
PyObject *obj = Py_BuildValue("s", res);
Py_XDECREF(res);
return obj;
}
static PyObject *pywit_voice_query_auto(PyObject *self, PyObject *args)
{
const char *access_token;
char *res;
if (context == NULL) {
PyErr_SetString(WitError, "Wit context uninitialized (did you call wit.init()?)");
return NULL;
}
if (!PyArg_ParseTuple(args, "s", &access_token))
return NULL;
res = wit_voice_query_auto(context, access_token);
PyObject *obj = Py_BuildValue("s", res);
Py_XDECREF(res);
return obj;
}
static PyObject *saved_py_cb = NULL;
PyGILState_STATE state;
void my_wit_resp_callback(char *res) {
PyObject *result;
state = PyGILState_Ensure();
PyObject *args = Py_BuildValue("(s)", res);
Py_XDECREF(res);
result = PyObject_CallObject(saved_py_cb, args);
Py_XDECREF(args);
Py_XDECREF(result);
PyGILState_Release(state);
}
static PyObject *pywit_text_query_async(PyObject *self, PyObject *args)
{
const char *text;
const char *access_token;
PyObject *py_cb;
if (context == NULL) {
PyErr_SetString(WitError, "Wit context uninitialized (did you call wit.init()?)");
return NULL;
}
if (!PyArg_ParseTuple(args, "ssO", &text, &access_token, &py_cb))
return NULL;
if (!PyCallable_Check(py_cb)) {
PyErr_SetString(WitError, "Parameter must be callable.");
return NULL;
}
Py_XINCREF(py_cb);
Py_XDECREF(saved_py_cb);
saved_py_cb = py_cb;
wit_text_query_async(context, text, access_token, my_wit_resp_callback);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pywit_voice_query_auto_async(PyObject *self, PyObject *args)
{
const char *access_token;
PyObject *py_cb;
if (context == NULL) {
PyErr_SetString(WitError, "Wit context uninitialized (did you call wit.init()?)");
return NULL;
}
if (!PyArg_ParseTuple(args, "sO", &access_token, &py_cb))
return NULL;
if (!PyCallable_Check(py_cb)) {
PyErr_SetString(WitError, "Parameter must be callable.");
return NULL;
}
Py_XINCREF(py_cb);
Py_XDECREF(saved_py_cb);
saved_py_cb = py_cb;
wit_voice_query_auto_async(context, access_token, my_wit_resp_callback);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pywit_voice_query_stop_async(PyObject *self, PyObject *args)
{
PyObject *py_cb;
if (context == NULL) {
PyErr_SetString(WitError, "Wit context uninitialized (did you call wit.init()?)");
return NULL;
}
if (!PyArg_ParseTuple(args, "O", &py_cb))
return NULL;
if (!PyCallable_Check(py_cb)) {
PyErr_SetString(WitError, "Parameter must be callable.");
return NULL;
}
Py_XINCREF(py_cb);
Py_XDECREF(saved_py_cb);
saved_py_cb = py_cb;
wit_voice_query_stop_async(context, my_wit_resp_callback);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef WitMethods[] = {
{"init", pywit_init, METH_VARARGS, "Initialize wit."},
{"close", pywit_close, METH_NOARGS, "Close wit."},
{"text_query", pywit_text_query, METH_VARARGS, "Get intent via text."},
{"voice_query_start", pywit_voice_query_start, METH_VARARGS, "Start recording."},
{"voice_query_stop", pywit_voice_query_stop, METH_NOARGS, "Get intent via voice."},
{"voice_query_auto", pywit_voice_query_auto, METH_VARARGS, "Get intent via voice, and detect end of speech."},
{"text_query_async", pywit_text_query_async, METH_VARARGS, "Get intent via text asynchronously."},
{"voice_query_auto_async", pywit_voice_query_auto_async, METH_VARARGS, "Get intent via voice with automatic end-of-speech detection, asynchronously."},
{"voice_query_stop_async", pywit_voice_query_stop_async, METH_VARARGS, "Get intent via voice asynchronously."},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef WitModule = {
PyModuleDef_HEAD_INIT,
"wit",
"Wit Python SDK",
-1,
WitMethods
};
#endif
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_wit(void)
#else
PyMODINIT_FUNC initwit(void)
#endif
{
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&WitModule);
#else
m = Py_InitModule("wit", WitMethods);
#endif
if (m == NULL)
return NULL;
WitError = PyErr_NewException("wit.error", NULL, NULL);
Py_INCREF(WitError);
PyModule_AddObject(m, "error", WitError);
PyEval_InitThreads();
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}