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

Andreas Jung andreas@digicool.com
Sun, 17 Mar 2002 19:48:11 -0500


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

Modified Files:
      Tag: ajung-textindexng-branch
	indexsupport.c 
Log Message:
- replaced main loop inside the vocabulary objects to assign
  wordIds by C implementation (50% speedup)
- LexiconNG code cleanup


=== Zope/lib/python/Products/PluginIndexes/TextIndexNG/src/indexsupport.c 1.1.2.3 => 1.1.2.4 ===
 
 #include "Python.h"
+#include <stdlib.h>
 
 
 static PyObject *
@@ -63,10 +64,60 @@
     return list;
 }
 
+/*
+    Arguments:
+
+    - fwdIdx  --  forward index (OIBTree)
+    - reverseIdx -- reverse index (IOBtree)
+    - words -- sequence of words to be inserted 
+*/
+
+PyObject *vocabularyBatchInsert(PyObject *self,PyObject *args) {
+
+    PyObject *fwdIdx,*revIdx, *lst, *word, *wid;
+    PyObject *ids;
+    int i;
+
+    ids = PyList_New(0);
+
+    if (PyArg_ParseTuple(args, "OOO", &fwdIdx,&revIdx,&lst)<0) return NULL;
+
+    if (! PySequence_Check(lst)) {
+        PyErr_SetString(PyExc_TypeError, "sequence expected"); \
+        return NULL;
+    }
+
+
+    for (i=0; i<PySequence_Length(lst);i++) {
+        word = PySequence_GetItem(lst,i);
+
+        if (PyMapping_HasKey(fwdIdx,word)) {
+            wid = PyObject_GetItem(fwdIdx,word);
+        }
+        else {
+            
+            do {
+                wid = PyInt_FromLong( rand() );
+            } while(PyMapping_HasKey(revIdx, wid));
+           
+
+            PyObject_SetItem(fwdIdx, word, wid);
+            PyObject_SetItem(revIdx, wid,word);
+        }
+
+        PyList_Append(ids, wid);
+    }    
+    
+    return ids;
+}
+
 static struct PyMethodDef indexsupport_module_methods[] =
     {
         { "stopwordfilter", (PyCFunction) stopwordfilter, METH_VARARGS,
             "stopwordfilter(wordslist,stopword dict') " "-- filters words from wordslist that are stopwords"
+        },
+        { "vocabBatchInsert", (PyCFunction) vocabularyBatchInsert, METH_VARARGS,
+            "vocabBatchInsert(fwdIdx,revIdx,wordLst') " "-- inserts forward and backward entries for vocabularies"
         },
         { "listIndexes", (PyCFunction) listIndexes, METH_VARARGS,
             "listIndexes(list of words',word) " "-- find all positions of word in a list of words"