Commit 80da8535e2a5b799e11e01b350707d2ba084480e
1 parent
a083088a
Exists in
master
and in
1 other branch
Modifica pyTradutor para chamar PortGlosa
Showing
2 changed files
with
108 additions
and
52 deletions
Show diff stats
tradutor/src/pyTradutor.cpp
| 1 | +//***************************************************************** | ||
| 2 | +/* | ||
| 3 | + VLibras: Automatic contents translator from Portuguese to LIBRAS | ||
| 4 | + | ||
| 5 | + Copyright (c) 2015 Gustavo Sobral, Erickson Silva, Leonardo Araújo | ||
| 6 | + VLibras-Core group at LAViD, Federal University of Paraiba | ||
| 7 | +*/ | ||
| 8 | +//***************************************************************** | ||
| 9 | + | ||
| 1 | /** | 10 | /** |
| 2 | - * Essa classe invoca os metodos do tradutor em Python | ||
| 3 | - * Onde efetua a tradução do texto passado por parametro | ||
| 4 | - * | ||
| 5 | - * @author Erickson Silva | ||
| 6 | - * @date 14/10/2013 | ||
| 7 | - * | 11 | + * \file pyTradutor.cpp |
| 12 | + * \authors Erickson Silva, Gustavo Sobral | ||
| 13 | + * \date Janeiro 2015 | ||
| 8 | */ | 14 | */ |
| 9 | 15 | ||
| 16 | +#include "../include/pyTradutor.hpp" | ||
| 10 | 17 | ||
| 11 | -#include "pyTradutor.h" | 18 | +PyTradutor::PyTradutor() |
| 19 | +{ | ||
| 20 | + // Initialize the Python interpreter | ||
| 21 | + Py_Initialize(); | ||
| 22 | + // Type conversion between C++ string to Py string | ||
| 23 | + pName = PyString_FromString("PortGlosa"); | ||
| 24 | + assert(pName!=NULL); | ||
| 25 | + // Loads the script name saved in 'pName' | ||
| 26 | + pModule = PyImport_Import(pName); | ||
| 27 | + PyErr_Print(); | ||
| 28 | + assert(pModule!=NULL); | ||
| 29 | + // Returns the dictionary object that implements the namespace pModule | ||
| 30 | + pDict = PyModule_GetDict(pModule); | ||
| 31 | + PyErr_Print(); | ||
| 32 | + assert(pDict!=NULL); | ||
| 33 | + // Return the object from dictionary pDict which has a key 'iniciar' | ||
| 34 | + pFunc = PyDict_GetItemString(pDict, "traduzir"); | ||
| 35 | + PyErr_Print(); | ||
| 36 | + assert(pFunc!=NULL); | ||
| 37 | +} | ||
| 12 | 38 | ||
| 13 | -namespace Tradutor { | ||
| 14 | - PyTradutor::PyTradutor() { | ||
| 15 | - DPRINTF("Done!\n"); | ||
| 16 | - } | ||
| 17 | - PyTradutor::~PyTradutor() { | ||
| 18 | - Py_DECREF(pName); | ||
| 19 | - Py_DECREF(pModule); | ||
| 20 | - Py_DECREF(pDict); | ||
| 21 | - Py_DECREF(pFunc); | ||
| 22 | - Py_DECREF(pArgs); | ||
| 23 | - Py_DECREF(pResult); | ||
| 24 | - DDDPRINTF("PyTranslator finalized!\n"); | ||
| 25 | - } | 39 | +PyTradutor::~PyTradutor() |
| 40 | +{ | ||
| 41 | + // Free the allocated memory and finalize the Python interpreter | ||
| 42 | + Py_Finalize(); | ||
| 43 | +} | ||
| 26 | 44 | ||
| 27 | -/** | ||
| 28 | -* Traduz um texto (char * input) para uma string contendo a | ||
| 29 | -* traducao para glosa | ||
| 30 | -* | ||
| 31 | -* @param input texto de entrada | ||
| 32 | -* @return string contendo os tokens em glosa traduzidos. | ||
| 33 | -**/ | ||
| 34 | - char * PyTradutor::convertStringToGlosa(const char * input) { | ||
| 35 | - if(!isRunning){ | ||
| 36 | - Py_Initialize(); | ||
| 37 | - pName = PyString_FromString("ModuleTranslate"); | ||
| 38 | - assert(pName!=NULL); | ||
| 39 | - pModule = PyImport_Import(pName); | ||
| 40 | - PyErr_Print(); | ||
| 41 | - assert(pModule!=NULL); | ||
| 42 | - pDict = PyModule_GetDict(pModule); | ||
| 43 | - PyErr_Print(); | ||
| 44 | - assert(pDict!=NULL); | ||
| 45 | - pFunc = PyDict_GetItemString(pDict, "iniciar"); | ||
| 46 | - PyErr_Print(); | ||
| 47 | - assert(pFunc!=NULL); | ||
| 48 | - isRunning = 1; | ||
| 49 | - } | ||
| 50 | - pArgs = PyTuple_Pack(1,PyString_FromString(input)); | ||
| 51 | - PyErr_Print(); | ||
| 52 | - assert(pArgs!=NULL); | ||
| 53 | - pResult = PyObject_CallObject(pFunc, pArgs); | ||
| 54 | - PyErr_Print(); | ||
| 55 | - assert(pResult!=NULL); | ||
| 56 | - return PyString_AsString(pResult); | ||
| 57 | - } | ||
| 58 | -} | ||
| 59 | \ No newline at end of file | 45 | \ No newline at end of file |
| 46 | +char * PyTradutor::convertStringToGlosa(const char * input) | ||
| 47 | +{ | ||
| 48 | + // Generates a new Tuple object with size '1' and value 'input' (converted) | ||
| 49 | + pArgs = PyTuple_Pack(1, PyString_FromString(input)); | ||
| 50 | + PyErr_Print(); | ||
| 51 | + assert(pArgs!=NULL); | ||
| 52 | + // Call the callable object 'pFunc' with arguments given by the tuple 'pArgs' | ||
| 53 | + pResult = PyObject_CallObject(pFunc, pArgs); | ||
| 54 | + PyErr_Print(); | ||
| 55 | + assert(pResult!=NULL); | ||
| 56 | + // Converts the string Python type to C++ | ||
| 57 | + return PyString_AsString(pResult); | ||
| 58 | +} |
| @@ -0,0 +1,57 @@ | @@ -0,0 +1,57 @@ | ||
| 1 | +//***************************************************************** | ||
| 2 | +/* | ||
| 3 | + VLibras: Automatic contents translator from Portuguese to LIBRAS | ||
| 4 | + | ||
| 5 | + Copyright (c) 2015 Gustavo Sobral, Erickson Silva, Leonardo Araújo | ||
| 6 | + VLibras-Core group at LAViD, Federal University of Paraiba | ||
| 7 | +*/ | ||
| 8 | +//***************************************************************** | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * \file pyTradutor.cpp | ||
| 12 | + * \authors Erickson Silva, Gustavo Sobral | ||
| 13 | + * \date Janeiro 2015 | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "../include/pyTradutor.hpp" | ||
| 17 | + | ||
| 18 | +PyTradutor::PyTradutor() | ||
| 19 | +{ | ||
| 20 | + // Initialize the Python interpreter | ||
| 21 | + Py_Initialize(); | ||
| 22 | + // Type conversion between C++ string to Py string | ||
| 23 | + pName = PyString_FromString("PortGlosa"); | ||
| 24 | + assert(pName!=NULL); | ||
| 25 | + // Loads the script name saved in 'pName' | ||
| 26 | + pModule = PyImport_Import(pName); | ||
| 27 | + PyErr_Print(); | ||
| 28 | + assert(pModule!=NULL); | ||
| 29 | + // Returns the dictionary object that implements the namespace pModule | ||
| 30 | + pDict = PyModule_GetDict(pModule); | ||
| 31 | + PyErr_Print(); | ||
| 32 | + assert(pDict!=NULL); | ||
| 33 | + // Return the object from dictionary pDict which has a key 'iniciar' | ||
| 34 | + pFunc = PyDict_GetItemString(pDict, "traduzir"); | ||
| 35 | + PyErr_Print(); | ||
| 36 | + assert(pFunc!=NULL); | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +PyTradutor::~PyTradutor() | ||
| 40 | +{ | ||
| 41 | + // Free the allocated memory and finalize the Python interpreter | ||
| 42 | + Py_Finalize(); | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +char * PyTradutor::convertStringToGlosa(const char * input) | ||
| 46 | +{ | ||
| 47 | + // Generates a new Tuple object with size '1' and value 'input' (converted) | ||
| 48 | + pArgs = PyTuple_Pack(1, PyString_FromString(input)); | ||
| 49 | + PyErr_Print(); | ||
| 50 | + assert(pArgs!=NULL); | ||
| 51 | + // Call the callable object 'pFunc' with arguments given by the tuple 'pArgs' | ||
| 52 | + pResult = PyObject_CallObject(pFunc, pArgs); | ||
| 53 | + PyErr_Print(); | ||
| 54 | + assert(pResult!=NULL); | ||
| 55 | + // Converts the string Python type to C++ | ||
| 56 | + return PyString_AsString(pResult); | ||
| 57 | +} |