pyTradutor.cpp
3.07 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
/**
* Essa classe invoca os metodos do tradutor em Python
* Onde efetua a tradução do texto passado por parametro
*
* @author Erickson Silva
* @date 14/10/2013
*
*/
#include "Python.h"
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "dprintf.h"
#include <vector>
#include <utility>
#include <algorithm>
#include "pyTradutor.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
namespace Tradutor {
PyTradutor::PyTradutor() {
DPRINTF("Done!\n");
}
PyTradutor::~PyTradutor() {
DPRINTF("PyTradutor finalized!\n");
}
/**
* Traduz um texto (char * input) para uma string contendo a
* traducao para glosa
*
* @param input texto de entrada
* @return string contendo os tokens em glosa traduzidos.
**/
char * PyTradutor::convertStringToGlosa(const char * input) {
//cout << "[INFO] PyTradutor recebeu input" << endl;
char* glosa = new char();
if((!pName) or (!pModule)){
Py_Initialize();
PyRun_SimpleString("import sys, os; sys.path.append(os.path.expanduser(\"~/gtaaas/tradutor/src/py\"))");
PyErr_Print();
pName = PyString_FromString("ModuleTranslate");
assert(pName!=NULL);
pModule = PyImport_Import(pName);
PyErr_Print();
assert(pModule!=NULL);
pDict = PyModule_GetDict(pModule);
PyErr_Print();
assert(pDict!=NULL);
pFunc = PyDict_GetItemString(pDict, "iniciar");
PyErr_Print();
assert(pFunc!=NULL);
}
if(pModule){
pArgs = PyTuple_Pack(1,PyString_FromString(input));
PyErr_Print();
assert(pArgs!=NULL);
pResult = PyObject_CallObject(pFunc, pArgs);
PyErr_Print();
assert(pResult!=NULL);
return PyString_AsString(pResult);
}
/**if((!moduleName) or (!pluginModule)){
Py_Initialize();
PyObject* moduleName = PyString_FromString("ModuleTranslate");
PyErr_Print();
cout << moduleName << " PyString_FromString" << endl;
PyObject* pluginModule = PyImport_Import(moduleName);
PyErr_Print();
cout << pluginModule << " PyImport_Import" << endl;
}
if(moduleName or pluginModule){
PyObject* transformFunc = PyObject_GetAttrString(pluginModule, "iniciar");
PyErr_Print();
cout << transformFunc << " PyObject_GetAttrString" << endl;
PyObject* argsTuple = Py_BuildValue("(s)", s.c_str());
PyErr_Print();
cout << argsTuple << " Py_BuildValue" << endl;
PyObject* result = PyObject_CallObject(transformFunc, argsTuple);
PyErr_Print();
cout << result << " PyObject_CallObject" << endl;
string resultStr(PyString_AsString(result));
PyErr_Print();
cout << resultStr << " PyString_AsString" << endl;
strcpy(glosa,resultStr.c_str());
cout << glosa << endl;
}**/
//Py_Finalize();
//Free all temporary Python objects.
/**Py_DECREF(moduleName);
Py_DECREF(pluginModule);
Py_DECREF(transformFunc);
Py_DECREF(argsTuple);
Py_DECREF(result);**/
}
}