-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkama.i
50 lines (42 loc) · 1.56 KB
/
kama.i
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
%module kama
%include exception.i
%{
#include "kama.h"
#include <Python.h>
%}
%exception {
try { $action }
catch (char *e) { SWIG_exception (SWIG_RuntimeError, e); }
catch (const char *e) { SWIG_exception (SWIG_RuntimeError, (char*)e); }
}
%include <std_vector.i>
%include <std_string.i>
%include <std_list.i>
%typemap(out) std::vector<Kama::Node>* %{
$result = PyList_New($1->size()); // Create outer Python list of correct size
for(size_t i = 0; i < $1->size(); ++i){
PyObject* node = PyDict_New();
PyDict_SetItem(node, PyString_FromString("nbest"), PyInt_FromLong((*$1)[i].nbest));
PyDict_SetItem(node, PyString_FromString("id"), PyInt_FromLong((*$1)[i].id));
PyDict_SetItem(node, PyString_FromString("surface"), PyString_FromString((*$1)[i].surface));
PyDict_SetItem(node, PyString_FromString("feature"), PyString_FromString((*$1)[i].feature));
PyList_SetItem($result, i, node);
}
%}
%typemap(in) std::list<const char*> %{
if(PyList_Check($input)){
std::list<const char*> strList;
for(int i = 0; i < PyList_Size($input); i++){
PyObject* pyItem = PyList_GetItem($input, i);
PyObject* str = PyUnicode_AsEncodedString(pyItem, "utf-8", "strict");
const char* tmpStr = PyBytes_AsString(str);
strList.push_back(tmpStr);
}
$1 = strList;
} else {
PyErr_SetString(PyExc_TypeError,"Wrong argument type, list expected");
return NULL;
}
%}
%include version.h
%include kama.h