Python and Tokyo Dystopia

So . . . . after some back and forth with Qing, here is what is ready so far. I know it needs work, but I should be able to get some stuff ready for N-GRAM soon.

update: (Feb 2011) I have chosen to abandon this project some time ago. All of the 1978th products are great and Qing and Mikio are still outstanding. They just no longer suit my use cases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Python.h"
#include <dystopia.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
 
static PyObject *
put(PyObject *self,PyObject *args){
    const char *dbname;
    const char *stext;
    const int *kid;
    int ecode;
    bool result;
    TCIDB *idb;
    if (!PyArg_ParseTuple(args, "sis", &amp;dbname, &amp;kid, &amp;stext))
        return NULL;
    /* create the object */
    idb = tcidbnew();
    /* open the database */
    if(!tcidbopen(idb, dbname, IDBOCREAT | IDBOWRITER)){
        ecode = tcidbecode(idb);
        fprintf(stderr, "open error: %s\n", tcidberrmsg(ecode));
    }
    result = tcidbput(idb,(int64_t)kid,stext);
    /* close the database */
    if(!tcidbclose(idb)){
        ecode = tcidbecode(idb);
        fprintf(stderr, "close error: %s\n", tcidberrmsg(ecode));
    }
    /* delete the object */
    tcidbdel(idb);
    return Py_BuildValue("b",result);
}
 
static PyObject *
search(PyObject *self, PyObject *args){
    const char *stext;
    const char *dbname;
    TCIDB *idb;
    int ecode, rnum, i;
    uint64_t *result;
    char *text;
    PyObject* pList;
 
    if (!PyArg_ParseTuple(args, "ss", &amp;dbname, &amp;stext))
        return NULL;
 
    /* create the object */
    idb = tcidbnew();
 
    /* open the database */
    if(!tcidbopen(idb, dbname, IDBOREADER | IDBONOLCK)){
        ecode = tcidbecode(idb);
        fprintf(stderr, "open error: %s\n", tcidberrmsg(ecode));
    }
    /* search records */
    result = tcidbsearch2(idb, stext, &amp;rnum);
    pList = PyList_New(rnum);
    if(result){
        for(i = 0; i < rnum; i++){
            // printf("r[i]:%lld\n",result[i]);
            PyList_SetItem(pList, i, Py_BuildValue("i", (int)result[i]));
        }
        tcfree(result);
    } else {
        ecode = tcidbecode(idb);
        fprintf(stderr, "search error: %s\n", tcidberrmsg(ecode));
    }
 
    /* close the database */
    if(!tcidbclose(idb)){
        ecode = tcidbecode(idb);
        fprintf(stderr, "close error: %s\n", tcidberrmsg(ecode));
    }
 
    /* delete the object */
    tcidbdel(idb);
 
    return Py_BuildValue("O",pList);
}
 
PyMethodDef methods[] = {
  {"search", search, METH_VARARGS},
  {"put", put, METH_VARARGS},
  {NULL, NULL},
};
 
void initpykhufu(){
    PyObject* m;
    m = Py_InitModule("pykhufu", methods);
}