Advertisement
Guest User

python/metaphone_ptbrpy.c

a guest
Mar 16th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. /************************************************************
  2. Copyright (C) 2017 Bruno Guberfain do Amaral <bruno.do.amaral@gmail.com>
  3. Released under the terms of the BSD license
  4.  
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7.  
  8. 1. Redistributions of source code must retain the above copyright notice, this
  9. list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or
  12. other materials provided with the distribution.
  13.  
  14.  
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  
  26.  
  27. ***********************************************************************/
  28.  
  29. #define PY_SSIZE_T_CLEAN
  30. #include <Python.h>
  31. #include "metaphone_ptbrpy.h"
  32. #include "../source/metaphone_ptbr.h"
  33.  
  34. // http://python3porting.com/cextensions.html
  35.  
  36. /* Docstrings */
  37. static char module_docstring[] = "This module provides an interface for Metaphone for Brazilian Portuguese.";
  38. static char phonetic_docstring[] =
  39. "Convert a string to its Brazilian Portuguese phonetic version.";
  40.  
  41. /* Module specification */
  42. static PyMethodDef module_methods[] = {
  43. {"phonetic", phonetic_phonetic, METH_VARARGS, phonetic_docstring},
  44. {NULL, NULL, 0, NULL}
  45. };
  46.  
  47. /* Initialize the module */
  48. #if PY_MAJOR_VERSION >= 3
  49. static struct PyModuleDef moduledef = {
  50. PyModuleDef_HEAD_INIT,
  51. "metaphoneptbr", /* m_name */
  52. module_docstring, /* m_doc */
  53. -1, /* m_size */
  54. module_methods, /* m_methods */
  55. NULL, /* m_reload */
  56. NULL, /* m_traverse */
  57. NULL, /* m_clear */
  58. NULL, /* m_free */
  59. };
  60.  
  61. PyMODINIT_FUNC PyInit_metaphoneptbr(void)
  62. {
  63. return PyModule_Create(&moduledef);;
  64. }
  65.  
  66. #else
  67.  
  68. PyMODINIT_FUNC initmetaphoneptbr(void)
  69. {
  70. Py_InitModule3("metaphoneptbr", module_methods, module_docstring);
  71. }
  72.  
  73. #endif
  74.  
  75. static PyObject *phonetic_phonetic(PyObject *self, PyObject *args)
  76. {
  77. const char *str_param = NULL;
  78. char *code = NULL;
  79. PyObject *result = NULL;
  80. int max_length = MAX_METAPHONE_LENGTH ;
  81. wchar_t phoneme[200] = {0};
  82. char *loc = NULL;
  83.  
  84.  
  85. if (!PyArg_ParseTuple(args, "s|i", &str_param, &max_length)) {
  86. return NULL;
  87. }
  88.  
  89. loc = setlocale(LC_CTYPE, NULL);
  90. if( !loc || !(*loc) || !strcmp(loc,"C") || !strcmp(loc,"POSIX") )
  91. if( !(loc=setlocale(LC_CTYPE,"pt_BR.UTF-8")) )
  92. return NULL;
  93.  
  94. mbstowcs(phoneme, str_param, 199);
  95.  
  96. code = Metaphone_PTBR(phoneme, max_length);
  97.  
  98. if (code) {
  99. //result = Py_BuildValue("s#", code, (int)strlen(code));
  100. result = PyUnicode_FromString(code);
  101. free(code);
  102. } else {
  103. //result = Py_BuildValue("s#", "", 0);
  104. result = PyUnicode_FromString("");
  105. }
  106.  
  107. return result;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement