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