-
Notifications
You must be signed in to change notification settings - Fork 11
/
_langid.c
58 lines (47 loc) · 1.5 KB
/
_langid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Python binding to liblangid
* Based on a tutorial by Dan Foreman-Mackey
* http://dan.iel.fm/posts/python-c-extensions/
* and on the implementation of chromium-compact-language-detector by Mike McCandless
* https://code.google.com/p/chromium-compact-language-detector
*
* Marco Lui <[email protected]>, September 2014
*/
#include <Python.h>
#include "liblangid.h"
/* Docstrings */
static char module_docstring[] =
"This module provides an off-the-shelf language identifier.";
static char identify_docstring[] =
"Identify the language of a piece of text.";
/* Available functions */
static PyObject *langid_identify(PyObject *self, PyObject *args);
/* Module specification */
static PyMethodDef module_methods[] = {
{"identify", langid_identify, METH_VARARGS, identify_docstring},
{NULL, NULL, 0, NULL}
};
/* Global LanguageIdentifier instance */
LanguageIdentifier *identifier;
/* Initialize the module */
PyMODINIT_FUNC init_langid(void)
{
PyObject *m = Py_InitModule3("_langid", module_methods, module_docstring);
if (m == NULL)
return;
identifier = get_default_identifier();
}
static PyObject *langid_identify(PyObject *self, PyObject *args)
{
char *bytes;
const char *lang;
int numBytes;
PyObject *ret;
if (!PyArg_ParseTuple(args, "s#", &bytes, &numBytes))
return NULL;
/* Run our identifier */
lang = identify(identifier, bytes, numBytes);
/* Build the output object */
ret = Py_BuildValue("s", lang);
return ret;
}