pyTradutor.cpp 3.07 KB
/**
 * 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);**/
	}
}