pyTradutor.cpp 1.66 KB
//*****************************************************************
/*
VLibras: Automatic contents translator from Portuguese to LIBRAS

Copyright (c) 2015 Gustavo Sobral, Erickson Silva, Leonardo Araújo,
Bruno santos
VLibras-Core group at LAViD, Federal University of Paraiba
*/
//*****************************************************************

/**
* \file pyTradutor.cpp
* \authors Erickson Silva, Gustavo Sobral
* \date Janeiro 2015
*/

#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);
}

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);

}