1 | /* |
---|
2 | * pymigemo.c - C/Migemo wrapper for Python |
---|
3 | * Copyright(C) 2005-2009, Atzm WATANABE <atzm@atzm.org> |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <Python.h> |
---|
9 | #include <structmember.h> |
---|
10 | #include <migemo.h> |
---|
11 | #include <stdbool.h> |
---|
12 | #include <string.h> |
---|
13 | |
---|
14 | #define PYMIGEMO_VERSION "0.3" |
---|
15 | |
---|
16 | /* for dereference migemo object members */ |
---|
17 | struct _migemo { |
---|
18 | int enable; |
---|
19 | void *mtree; |
---|
20 | int charset; |
---|
21 | void *roma2hira; |
---|
22 | void *hira2kata; |
---|
23 | void *han2zen; |
---|
24 | void *zen2han; |
---|
25 | void *rx; |
---|
26 | void *addword; |
---|
27 | void *char2int; |
---|
28 | }; |
---|
29 | |
---|
30 | typedef struct { |
---|
31 | PyObject_HEAD |
---|
32 | migemo *migemo_obj; |
---|
33 | } Migemo; |
---|
34 | |
---|
35 | static bool |
---|
36 | get_encoding(char *encoding, size_t size, int charset) |
---|
37 | { |
---|
38 | char *enc; |
---|
39 | |
---|
40 | switch(charset) { |
---|
41 | case 1: |
---|
42 | enc = "cp932"; |
---|
43 | break; |
---|
44 | case 2: |
---|
45 | enc = "euc_jp"; |
---|
46 | break; |
---|
47 | case 3: |
---|
48 | enc = "utf_8"; |
---|
49 | break; |
---|
50 | default: |
---|
51 | enc = "ascii"; |
---|
52 | } |
---|
53 | |
---|
54 | if (strlen(enc) < size) { |
---|
55 | strcpy(encoding, enc); |
---|
56 | return true; |
---|
57 | } |
---|
58 | |
---|
59 | return false; |
---|
60 | } |
---|
61 | |
---|
62 | static void |
---|
63 | Migemo_dealloc(Migemo *self) |
---|
64 | { |
---|
65 | if (self->migemo_obj) { |
---|
66 | migemo_close(self->migemo_obj); |
---|
67 | } |
---|
68 | |
---|
69 | self->ob_type->tp_free((PyObject *)self); |
---|
70 | } |
---|
71 | |
---|
72 | static PyObject * |
---|
73 | Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
74 | { |
---|
75 | Migemo *self; |
---|
76 | |
---|
77 | self = (Migemo *)type->tp_alloc(type, 0); |
---|
78 | |
---|
79 | if (self != NULL) { |
---|
80 | self->migemo_obj = NULL; |
---|
81 | } |
---|
82 | |
---|
83 | return (PyObject *)self; |
---|
84 | } |
---|
85 | |
---|
86 | static int |
---|
87 | Migemo_init(Migemo *self, PyObject *args, PyObject *kwds) |
---|
88 | { |
---|
89 | migemo *migemo_obj; |
---|
90 | char *dictionary; |
---|
91 | |
---|
92 | static char *kwlist[] = {"dictionary", NULL}; |
---|
93 | |
---|
94 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dictionary)) { |
---|
95 | return -1; |
---|
96 | } |
---|
97 | |
---|
98 | if (dictionary) { |
---|
99 | if (self->migemo_obj) { |
---|
100 | migemo_close(self->migemo_obj); |
---|
101 | } |
---|
102 | |
---|
103 | migemo_obj = migemo_open(dictionary); |
---|
104 | |
---|
105 | if (migemo_obj) { |
---|
106 | self->migemo_obj = migemo_obj; |
---|
107 | } |
---|
108 | else { |
---|
109 | PyErr_SetString(PyExc_AssertionError, "migemo_open() failed"); |
---|
110 | return -1; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | static PyObject * |
---|
118 | Migemo_get_encoding(Migemo *self) |
---|
119 | { |
---|
120 | char encoding[7]; |
---|
121 | |
---|
122 | if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { |
---|
123 | PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); |
---|
124 | return NULL; |
---|
125 | } |
---|
126 | |
---|
127 | return PyString_FromString(encoding); |
---|
128 | } |
---|
129 | |
---|
130 | static PyObject * |
---|
131 | Migemo_query(Migemo *self, PyObject *args, PyObject *kwds) |
---|
132 | { |
---|
133 | PyObject *result, *pyquery, *pyrestr; |
---|
134 | char *query, encoding[7]; |
---|
135 | unsigned char *regex; |
---|
136 | |
---|
137 | static char *kwlist[] = {"query", NULL}; |
---|
138 | |
---|
139 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &pyquery)) { |
---|
140 | return NULL; |
---|
141 | } |
---|
142 | |
---|
143 | if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { |
---|
144 | PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); |
---|
145 | return NULL; |
---|
146 | } |
---|
147 | |
---|
148 | if (PyUnicode_Check(pyquery)) { |
---|
149 | PyObject *q = PyUnicode_AsEncodedString(pyquery, encoding, "strict"); |
---|
150 | |
---|
151 | if (q == NULL) { |
---|
152 | return NULL; |
---|
153 | } |
---|
154 | |
---|
155 | query = PyString_AS_STRING(q); |
---|
156 | Py_DECREF(q); |
---|
157 | } |
---|
158 | else if (PyString_Check(pyquery)) { |
---|
159 | query = PyString_AS_STRING(pyquery); |
---|
160 | } |
---|
161 | else { |
---|
162 | PyErr_SetString(PyExc_ValueError, "argument must be string"); |
---|
163 | return NULL; |
---|
164 | } |
---|
165 | if (query == NULL) { |
---|
166 | return NULL; |
---|
167 | } |
---|
168 | |
---|
169 | regex = migemo_query(self->migemo_obj, query); |
---|
170 | if (regex == NULL) { |
---|
171 | PyErr_SetString(PyExc_AssertionError, "migemo_query() failed"); |
---|
172 | return NULL; |
---|
173 | } |
---|
174 | |
---|
175 | pyrestr = PyString_FromString(regex); |
---|
176 | migemo_release(self->migemo_obj, regex); |
---|
177 | if (pyrestr == NULL) { |
---|
178 | return NULL; |
---|
179 | } |
---|
180 | |
---|
181 | result = PyUnicode_FromEncodedObject(pyrestr, encoding, "strict"); |
---|
182 | Py_DECREF(pyrestr); |
---|
183 | return result; |
---|
184 | } |
---|
185 | |
---|
186 | static PyObject * |
---|
187 | Migemo_set_operator(Migemo *self, PyObject *args, PyObject *kwds) |
---|
188 | { |
---|
189 | PyObject *result = NULL; |
---|
190 | char *op; |
---|
191 | int index; |
---|
192 | |
---|
193 | static char *kwlist[] = {"index", "op", NULL}; |
---|
194 | |
---|
195 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "is", kwlist, &index, &op)) { |
---|
196 | return NULL; |
---|
197 | } |
---|
198 | |
---|
199 | if (op) { |
---|
200 | result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op)); |
---|
201 | } |
---|
202 | |
---|
203 | return result; |
---|
204 | } |
---|
205 | |
---|
206 | static PyObject * |
---|
207 | Migemo_get_operator(Migemo *self, PyObject *args, PyObject *kwds) |
---|
208 | { |
---|
209 | PyObject *result = NULL; |
---|
210 | const unsigned char *op; |
---|
211 | int index; |
---|
212 | |
---|
213 | static char *kwlist[] = {"index", NULL}; |
---|
214 | |
---|
215 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &index)) { |
---|
216 | return NULL; |
---|
217 | } |
---|
218 | |
---|
219 | if (op = migemo_get_operator(self->migemo_obj, index)) { |
---|
220 | result = PyString_FromString(op); |
---|
221 | } |
---|
222 | else { |
---|
223 | PyErr_SetString(PyExc_ValueError, "invalid opindex"); |
---|
224 | } |
---|
225 | |
---|
226 | return result; |
---|
227 | } |
---|
228 | |
---|
229 | static PyObject * |
---|
230 | Migemo_load(Migemo *self, PyObject *args, PyObject *kwds) |
---|
231 | { |
---|
232 | PyObject *result = NULL; |
---|
233 | char *dict_file; |
---|
234 | int dict_id; |
---|
235 | |
---|
236 | static char *kwlist[] = {"dict_id", "dict_file", NULL}; |
---|
237 | |
---|
238 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "is", kwlist, &dict_id, &dict_file)) { |
---|
239 | return NULL; |
---|
240 | } |
---|
241 | |
---|
242 | if (dict_file) { |
---|
243 | result = PyInt_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file)); |
---|
244 | } |
---|
245 | |
---|
246 | return result; |
---|
247 | } |
---|
248 | |
---|
249 | static PyObject * |
---|
250 | Migemo_is_enable(Migemo *self) |
---|
251 | { |
---|
252 | return PyBool_FromLong((long)migemo_is_enable(self->migemo_obj)); |
---|
253 | } |
---|
254 | |
---|
255 | static PyMethodDef Migemo_methods[] = { |
---|
256 | {"query", (PyCFunction)Migemo_query, METH_KEYWORDS, |
---|
257 | "return regex from romaji string\n\ |
---|
258 | \n\ |
---|
259 | def query(query)\n\ |
---|
260 | query: romaji string (str or unicode)\n\ |
---|
261 | \n\ |
---|
262 | returns: regex string as Unicode object"}, |
---|
263 | {"set_operator", (PyCFunction)Migemo_set_operator, METH_KEYWORDS, |
---|
264 | "set operator string as the meta character of regex\n\ |
---|
265 | \n\ |
---|
266 | def set_operator(index, op):\n\ |
---|
267 | index: (OPINDEX_NEST_IN|OPINDEX_NEST_OUT|OPINDEX_NEWLINE|\n\ |
---|
268 | OPINDEX_OR|OPINDEX_SELECT_IN|OPINDEX_SELECT_OUT)\n\ |
---|
269 | op: operator string (str)\n\ |
---|
270 | \n\ |
---|
271 | returns: boolean value"}, |
---|
272 | {"get_operator", (PyCFunction)Migemo_get_operator, METH_KEYWORDS, |
---|
273 | "get operator string as the meta character of regex\n\ |
---|
274 | \n\ |
---|
275 | def get_operator(index)\n\ |
---|
276 | index: (OPINDEX_NEST_IN|OPINDEX_NEST_OUT|OPINDEX_NEWLINE|\n\ |
---|
277 | OPINDEX_OR|OPINDEX_SELECT_IN|OPINDEX_SELECT_OUT)\n\ |
---|
278 | \n\ |
---|
279 | returns: operator string (str)"}, |
---|
280 | {"load", (PyCFunction)Migemo_load, METH_KEYWORDS, |
---|
281 | "add dictionary to Migemo object\n\ |
---|
282 | \n\ |
---|
283 | def load(dict_id, dict_file)\n\ |
---|
284 | dict_id: (DICTID_HAN2ZEN|DICTID_HIRA2KATA|DICTID_MIGEMO|\n\ |
---|
285 | DICTID_ROMA2HIRA|DICTID_ZEN2HAN)\n\ |
---|
286 | dict_file: path to dictionary file (str)\n\ |
---|
287 | \n\ |
---|
288 | returns: boolean value"}, |
---|
289 | {"is_enable", (PyCFunction)Migemo_is_enable, METH_NOARGS, |
---|
290 | "check internal migemo_dict\n\ |
---|
291 | \n\ |
---|
292 | def is_enable()\n\ |
---|
293 | returns: boolean value"}, |
---|
294 | {"get_encoding", (PyCFunction)Migemo_get_encoding, METH_NOARGS, |
---|
295 | "get dictionary encoding\n\ |
---|
296 | \n\ |
---|
297 | def get_encoding()\n\ |
---|
298 | returns: encoding string (str)"}, |
---|
299 | {NULL} /* Sentinel */ |
---|
300 | }; |
---|
301 | |
---|
302 | static PyMemberDef Migemo_members[] = { |
---|
303 | {NULL} /* Sentinel */ |
---|
304 | }; |
---|
305 | |
---|
306 | static PyTypeObject MigemoType = { |
---|
307 | PyObject_HEAD_INIT(NULL) |
---|
308 | 0, /*ob_size*/ |
---|
309 | "migemo.Migemo", /*tp_name*/ |
---|
310 | sizeof(Migemo), /*tp_basicsize*/ |
---|
311 | 0, /*tp_itemsize*/ |
---|
312 | (destructor)Migemo_dealloc, /*tp_dealloc*/ |
---|
313 | 0, /*tp_print*/ |
---|
314 | 0, /*tp_getattr*/ |
---|
315 | 0, /*tp_setattr*/ |
---|
316 | 0, /*tp_compare*/ |
---|
317 | 0, /*tp_repr*/ |
---|
318 | 0, /*tp_as_number*/ |
---|
319 | 0, /*tp_as_sequence*/ |
---|
320 | 0, /*tp_as_mapping*/ |
---|
321 | 0, /*tp_hash */ |
---|
322 | 0, /*tp_call*/ |
---|
323 | 0, /*tp_str*/ |
---|
324 | 0, /*tp_getattro*/ |
---|
325 | 0, /*tp_setattro*/ |
---|
326 | 0, /*tp_as_buffer*/ |
---|
327 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
328 | "Migemo wrapper object", /* tp_doc */ |
---|
329 | 0, /* tp_traverse */ |
---|
330 | 0, /* tp_clear */ |
---|
331 | 0, /* tp_richcompare */ |
---|
332 | 0, /* tp_weaklistoffset */ |
---|
333 | 0, /* tp_iter */ |
---|
334 | 0, /* tp_iternext */ |
---|
335 | Migemo_methods, /* tp_methods */ |
---|
336 | Migemo_members, /* tp_members */ |
---|
337 | 0, /* tp_getset */ |
---|
338 | 0, /* tp_base */ |
---|
339 | 0, /* tp_dict */ |
---|
340 | 0, /* tp_descr_get */ |
---|
341 | 0, /* tp_descr_set */ |
---|
342 | 0, /* tp_dictoffset */ |
---|
343 | (initproc)Migemo_init, /* tp_init */ |
---|
344 | 0, /* tp_alloc */ |
---|
345 | Migemo_new, /* tp_new */ |
---|
346 | }; |
---|
347 | |
---|
348 | static PyMethodDef module_methods[] = { |
---|
349 | {NULL} /* Sentinel */ |
---|
350 | }; |
---|
351 | |
---|
352 | #ifndef PyMODINIT_FUNC |
---|
353 | #define PyMODINIT_FUNC void |
---|
354 | #endif |
---|
355 | PyMODINIT_FUNC |
---|
356 | initmigemo(void) |
---|
357 | { |
---|
358 | PyObject* m; |
---|
359 | |
---|
360 | if (PyType_Ready(&MigemoType) < 0) |
---|
361 | return; |
---|
362 | |
---|
363 | m = Py_InitModule3("migemo", module_methods, "C/Migemo wrapper"); |
---|
364 | |
---|
365 | Py_INCREF(&MigemoType); |
---|
366 | PyModule_AddObject(m, "Migemo", (PyObject *)&MigemoType); |
---|
367 | PyModule_AddObject(m, "PYMIGEMO_VERSION", Py_BuildValue("s", PYMIGEMO_VERSION)); |
---|
368 | |
---|
369 | PyModule_AddObject(m, "MIGEMO_VERSION", Py_BuildValue("s", MIGEMO_VERSION)); |
---|
370 | |
---|
371 | PyModule_AddObject(m, "DICTID_INVALID", Py_BuildValue("i", MIGEMO_DICTID_INVALID)); |
---|
372 | PyModule_AddObject(m, "DICTID_MIGEMO", Py_BuildValue("i", MIGEMO_DICTID_MIGEMO)); |
---|
373 | PyModule_AddObject(m, "DICTID_ROMA2HIRA", Py_BuildValue("i", MIGEMO_DICTID_ROMA2HIRA)); |
---|
374 | PyModule_AddObject(m, "DICTID_HIRA2KATA", Py_BuildValue("i", MIGEMO_DICTID_HIRA2KATA)); |
---|
375 | PyModule_AddObject(m, "DICTID_HAN2ZEN", Py_BuildValue("i", MIGEMO_DICTID_HAN2ZEN)); |
---|
376 | PyModule_AddObject(m, "DICTID_ZEN2HAN", Py_BuildValue("i", MIGEMO_DICTID_ZEN2HAN)); |
---|
377 | |
---|
378 | PyModule_AddObject(m, "OPINDEX_OR", Py_BuildValue("i", MIGEMO_OPINDEX_OR)); |
---|
379 | PyModule_AddObject(m, "OPINDEX_NEST_IN", Py_BuildValue("i", MIGEMO_OPINDEX_NEST_IN)); |
---|
380 | PyModule_AddObject(m, "OPINDEX_NEST_OUT", Py_BuildValue("i", MIGEMO_OPINDEX_NEST_OUT)); |
---|
381 | PyModule_AddObject(m, "OPINDEX_SELECT_IN", Py_BuildValue("i", MIGEMO_OPINDEX_SELECT_IN)); |
---|
382 | PyModule_AddObject(m, "OPINDEX_SELECT_OUT", Py_BuildValue("i", MIGEMO_OPINDEX_SELECT_OUT)); |
---|
383 | PyModule_AddObject(m, "OPINDEX_NEWLINE", Py_BuildValue("i", MIGEMO_OPINDEX_NEWLINE)); |
---|
384 | } |
---|