-
Notifications
You must be signed in to change notification settings - Fork 0
/
mica_ictclas.cpp
75 lines (60 loc) · 1.61 KB
/
mica_ictclas.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#define OS_LINUX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include "ICTCLAS50.h"
#include <Python.h>
using namespace std;
static PyObject * mica_ictclas_translate(PyObject *self, PyObject *args)
{
PyObject * ret;
unsigned int nPaLen;
char * sRst;
const char * source;
if (!PyArg_ParseTuple(args, "s", &source))
return NULL;
nPaLen = strlen(source);
sRst = (char *) malloc(nPaLen * 6);
ICTCLAS_ParagraphProcess(source, nPaLen, sRst, CODE_TYPE_UNKNOWN, 0);
ret = Py_BuildValue("s", sRst);
free(sRst);
return ret;
}
static PyMethodDef MicaMethods[] = {
{"trans", mica_ictclas_translate, METH_VARARGS,
"Identify chinese character groups."},
{NULL, NULL, 0, NULL}
};
static PyObject *MicaError;
static string errorname1("mica_ictclas.error"), errorname2("error");
static char name[100];
PyMODINIT_FUNC
initmica_ictclas(void)
{
PyObject *m;
if(!ICTCLAS_Init()) {
printf("Init fails\n");
PyErr_SetString(MicaError, "MICA ICTCLAS initialization failed!");
return;
}
ICTCLAS_SetPOSmap(2);
m = Py_InitModule("mica_ictclas", MicaMethods);
if (m == NULL)
return;
strcpy(name, errorname1.c_str());
MicaError = PyErr_NewException(name, NULL, NULL);
Py_INCREF(MicaError);
strcpy(name, errorname2.c_str());
PyModule_AddObject(m, name, MicaError);
//ICTCLAS_Exit();
}
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
initmica_ictclas();
return 1;
}