source: trunk/pymigemo/pymigemo.c @ 30

Revision 30, 10.0 KB checked in by atzm, 14 years ago (diff)

modified indentation

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