From 80da8535e2a5b799e11e01b350707d2ba084480e Mon Sep 17 00:00:00 2001 From: Erickson Silva Date: Tue, 7 Apr 2015 16:11:02 -0300 Subject: [PATCH] Modifica pyTradutor para chamar PortGlosa --- tradutor/src/pyTradutor.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------- tradutor/src/pyTradutor.cpp~ | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 52 deletions(-) create mode 100644 tradutor/src/pyTradutor.cpp~ diff --git a/tradutor/src/pyTradutor.cpp b/tradutor/src/pyTradutor.cpp index 3916aa3..8173e98 100644 --- a/tradutor/src/pyTradutor.cpp +++ b/tradutor/src/pyTradutor.cpp @@ -1,58 +1,57 @@ +//***************************************************************** +/* + VLibras: Automatic contents translator from Portuguese to LIBRAS + + Copyright (c) 2015 Gustavo Sobral, Erickson Silva, Leonardo Araújo + VLibras-Core group at LAViD, Federal University of Paraiba +*/ +//***************************************************************** + /** - * 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 - * + * \file pyTradutor.cpp + * \authors Erickson Silva, Gustavo Sobral + * \date Janeiro 2015 */ +#include "../include/pyTradutor.hpp" -#include "pyTradutor.h" +PyTradutor::PyTradutor() +{ + // Initialize the Python interpreter + Py_Initialize(); + // Type conversion between C++ string to Py string + pName = PyString_FromString("PortGlosa"); + assert(pName!=NULL); + // Loads the script name saved in 'pName' + pModule = PyImport_Import(pName); + PyErr_Print(); + assert(pModule!=NULL); + // Returns the dictionary object that implements the namespace pModule + pDict = PyModule_GetDict(pModule); + PyErr_Print(); + assert(pDict!=NULL); + // Return the object from dictionary pDict which has a key 'iniciar' + pFunc = PyDict_GetItemString(pDict, "traduzir"); + PyErr_Print(); + assert(pFunc!=NULL); +} -namespace Tradutor { - PyTradutor::PyTradutor() { - DPRINTF("Done!\n"); - } - PyTradutor::~PyTradutor() { - Py_DECREF(pName); - Py_DECREF(pModule); - Py_DECREF(pDict); - Py_DECREF(pFunc); - Py_DECREF(pArgs); - Py_DECREF(pResult); - DDDPRINTF("PyTranslator finalized!\n"); - } +PyTradutor::~PyTradutor() +{ + // Free the allocated memory and finalize the Python interpreter + Py_Finalize(); +} -/** -* 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) { - if(!isRunning){ - Py_Initialize(); - 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); - isRunning = 1; - } - 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); - } -} \ No newline at end of file +char * PyTradutor::convertStringToGlosa(const char * input) +{ + // Generates a new Tuple object with size '1' and value 'input' (converted) + pArgs = PyTuple_Pack(1, PyString_FromString(input)); + PyErr_Print(); + assert(pArgs!=NULL); + // Call the callable object 'pFunc' with arguments given by the tuple 'pArgs' + pResult = PyObject_CallObject(pFunc, pArgs); + PyErr_Print(); + assert(pResult!=NULL); + // Converts the string Python type to C++ + return PyString_AsString(pResult); +} diff --git a/tradutor/src/pyTradutor.cpp~ b/tradutor/src/pyTradutor.cpp~ new file mode 100644 index 0000000..8173e98 --- /dev/null +++ b/tradutor/src/pyTradutor.cpp~ @@ -0,0 +1,57 @@ +//***************************************************************** +/* + VLibras: Automatic contents translator from Portuguese to LIBRAS + + Copyright (c) 2015 Gustavo Sobral, Erickson Silva, Leonardo Araújo + VLibras-Core group at LAViD, Federal University of Paraiba +*/ +//***************************************************************** + +/** + * \file pyTradutor.cpp + * \authors Erickson Silva, Gustavo Sobral + * \date Janeiro 2015 + */ + +#include "../include/pyTradutor.hpp" + +PyTradutor::PyTradutor() +{ + // Initialize the Python interpreter + Py_Initialize(); + // Type conversion between C++ string to Py string + pName = PyString_FromString("PortGlosa"); + assert(pName!=NULL); + // Loads the script name saved in 'pName' + pModule = PyImport_Import(pName); + PyErr_Print(); + assert(pModule!=NULL); + // Returns the dictionary object that implements the namespace pModule + pDict = PyModule_GetDict(pModule); + PyErr_Print(); + assert(pDict!=NULL); + // Return the object from dictionary pDict which has a key 'iniciar' + pFunc = PyDict_GetItemString(pDict, "traduzir"); + PyErr_Print(); + assert(pFunc!=NULL); +} + +PyTradutor::~PyTradutor() +{ + // Free the allocated memory and finalize the Python interpreter + Py_Finalize(); +} + +char * PyTradutor::convertStringToGlosa(const char * input) +{ + // Generates a new Tuple object with size '1' and value 'input' (converted) + pArgs = PyTuple_Pack(1, PyString_FromString(input)); + PyErr_Print(); + assert(pArgs!=NULL); + // Call the callable object 'pFunc' with arguments given by the tuple 'pArgs' + pResult = PyObject_CallObject(pFunc, pArgs); + PyErr_Print(); + assert(pResult!=NULL); + // Converts the string Python type to C++ + return PyString_AsString(pResult); +} -- libgit2 0.21.2