[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG/src - normalizer.c:1.1.2.1

Andreas Jung andreas@digicool.com
Sun, 3 Feb 2002 14:08:49 -0500


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG/src
In directory cvs.zope.org:/tmp/cvs-serv626/src

Added Files:
      Tag: ajung-textindexng-branch
	normalizer.c 
Log Message:
added Normalizer


=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexNG/src/normalizer.c ===
#include "Python.h"


typedef struct
{
    PyObject_HEAD
    PyObject * table;
}
normalizer;


static void
normalizer_dealloc(normalizer *self)
{
    Py_XDECREF(self->table);
    PyMem_DEL(self);
}

static PyObject *normalize(normalizer *self, PyObject*args)
{
    int i,j;
    PyObject * data=NULL, *key=NULL, *value=NULL, *item=NULL;

    if (! (PyArg_ParseTuple(args,"O", &data)))
        return NULL;


    if (PyList_Check(data)) {

        PyObject *word,*list;

        list = PyList_New(0);

        for (j=0; j<PyList_Size(data); j++) {
            word = PyList_GetItem(data,j);


            if (! (PyUnicode_Check(word) ||
                    PyString_Check(word))) {

                PyErr_SetString(PyExc_TypeError,"argument must be string or unicode");
                return NULL;
            }

            for (i=0; i<PyList_GET_SIZE(self->table); i++) {
                PyObject *s;

                item = PyList_GetItem(self->table, i);

                key   = PyTuple_GetItem(item,0);
                value = PyTuple_GetItem(item,1);

                s = PyUnicode_Replace(word, key, value, -1);

                word = s;
                Py_INCREF(data);

            }

            PyList_Append(list,word);

        }

        return list;
    } else if (PyUnicode_Check(data) || PyString_Check(data) ) {


        for (i=0; i<PyList_GET_SIZE(self->table); i++) {
            PyObject *s;

            item = PyList_GetItem(self->table, i);

            key   = PyTuple_GetItem(item,0);
            value = PyTuple_GetItem(item,1);

            s = PyUnicode_Replace(data, key, value, -1);

            data = s;
            Py_INCREF(data);

        }
    } else {
        PyErr_SetString(PyExc_TypeError,"argument must be unicode or string");
        return NULL;
    }



    return data;
}

int checkList(PyObject *o)
{
    int i;
    PyObject *item,*key,*value;

    if ( !( PyList_Check(o) || PyTuple_Check(o) )) {
        PyErr_SetString(PyExc_TypeError, "argument must be list or tuple of 2-tuples of strings");
        return 0;
    }

    for (i=0;i<PySequence_Size(o); i++) {
        item = PySequence_GetItem(o,i);

        if (! PyTuple_Check(item)) {
            PyErr_SetString(PyExc_TypeError,"nested arguments must be tuples");
            Py_DECREF(item);
            return 0;
        }

        if (PyTuple_Size(item) != 2) {

            PyErr_SetString(PyExc_TypeError,"nested arguments must be 2-tuples of strings/unicode strings");
            Py_DECREF(item);
            return 0;
        }


        key = PyTuple_GetItem(item,0);
        value = PyTuple_GetItem(item,1);

        if (! (PyString_Check(key) || PyUnicode_Check(key))) {
            PyErr_SetString(PyExc_TypeError, "arg 1 or 2-tuple must be string or unicode");
            Py_DECREF(key);
            Py_DECREF(value);
            Py_DECREF(item);
            return 0;
        }

        if (! (PyString_Check(value) || PyUnicode_Check(value))) {
            PyErr_SetString(PyExc_TypeError, "arg 2 or 2-tuple must be string or unicode");
            Py_DECREF(key);
            Py_DECREF(value);
            Py_DECREF(item);
            return 0;
        }

        Py_DECREF(key);
        Py_DECREF(value);
        Py_DECREF(item);
    }

    return 1;
}

static struct PyMethodDef normalizer_methods[] =
    {
        { "normalize", (PyCFunction)normalize,
            METH_VARARGS, "normalize([string],[or list]) "
            "-- normalize a string or a list of strings"
        },
        { NULL, NULL }		/* sentinel */
    };

static  PyObject *
normalizer_getattr(normalizer *self, char *name)
{
    return Py_FindMethod(normalizer_methods, (PyObject *)self, name);
}

static char normalizerType__doc__[] = "normalizer object";

static PyTypeObject normalizerType = {
                                         PyObject_HEAD_INIT(NULL)
                                         0,                            /*ob_size*/
                                         "normalizer",                 /*tp_name*/
                                         sizeof(normalizer),           /*tp_basicsize*/
                                         0,                            /*tp_itemsize*/
                                         /* methods */
                                         (destructor)normalizer_dealloc,  /*tp_dealloc*/
                                         (printfunc)0,                 /*tp_print*/
                                         (getattrfunc)normalizer_getattr, /*tp_getattr*/
                                         (setattrfunc)0,               /*tp_setattr*/
                                         (cmpfunc)0,                   /*tp_compare*/
                                         (reprfunc)0,                  /*tp_repr*/
                                         0,                            /*tp_as_number*/
                                         0,                            /*tp_as_sequence*/
                                         0,                            /*tp_as_mapping*/
                                         (hashfunc)0,                  /*tp_hash*/
                                         (ternaryfunc)0,               /*tp_call*/
                                         (reprfunc)0,                  /*tp_str*/

                                         /* Space for future expansion */
                                         0L,0L,0L,0L,
                                         normalizerType__doc__            /* Documentation string */
                                     };


static PyObject *
newnormalizer(PyObject *modinfo, PyObject *args, PyObject *keywds)
{
    normalizer *self=NULL;
    PyObject *table;


    if (! (PyArg_ParseTuple(args,"O", &table)))
        return NULL;
    if (! checkList(table))
        return NULL;

    if (! (self = PyObject_NEW(normalizer, &normalizerType)))
        return NULL;

    self->table = table;
    Py_INCREF(self->table);

    return (PyObject*)self;
}

static struct PyMethodDef normalizer_module_methods[] =
    {
        { "Normalizer", (PyCFunction)newnormalizer, METH_VARARGS,
            "Normalizer(list) " "-- Normalizer module"
        },
        { NULL, NULL }
    };

static char normalizer_module_documentation[] =
    "Normalizer module .\n"
    "\n"
    "$Id: normalizer.c,v 1.1.2.1 2002/02/03 19:08:48 andreasjung Exp $\n"
    ;


void
initnormalizer(void)
{
    PyObject *m, *d;
    char *rev="$Revision: 1.1.2.1 $";

    /* Create the module and add the functions */
    m = Py_InitModule4("normalizer", normalizer_module_methods,
                       normalizer_module_documentation,
                       (PyObject*)NULL,PYTHON_API_VERSION);

    /* Add some symbolic constants to the module */
    d = PyModule_GetDict(m);
    PyDict_SetItemString(d, "__version__",
                         PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));

    if (PyErr_Occurred())
        Py_FatalError("can't initialize module normalizer");
}