public
Description: PPM (Predict by Partial Matching) compression algorithm implementation for Python
Homepage:
Clone URL: git://github.com/pluskid/pyppm.git
pluskid (author)
Mon Nov 03 08:51:29 -0800 2008
commit  e94a82cbef67437ebc932276b2db7150459a12d8
tree    639a12e471e8742eb937ae2ea68e2d241ba3876b
parent  ea2cf61c65976ae2bc407d3c7b7e40ae6b1c317e
pyppm / pyppm.cpp
100644 159 lines (132 sloc) 3.821 kb
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <Python.h>
#include "ppm_model.h"
#include "io_adapter.h"
 
using namespace std;
 
extern "C" {
    
typedef struct
{
    PyObject_HEAD
    PPMModel *model;
} Model;
 
static void Model_dealloc(PyObject *self);
static PyObject * Model_GetAttr(PyObject *self, char *attrname);
    
static PyTypeObject Model_Type = {
    PyObject_HEAD_INIT(&PyType_Type)
    0,
    "Model",
    sizeof(Model),
    0,
    (destructor)Model_dealloc,
    0,
    (getattrfunc)Model_GetAttr,
    /* rest are NULLs */
};
 
#define Model_Check(v) ((v)->ob_type == &Model_Type)
#define Model_Ptr(v) (((Model *)(v))->model)
 
static PyObject *Model_New(PyObject *self, PyObject *args)
{
    PPMModel *pm;
    Model *model = NULL;
    char *path = NULL;
 
    if (PyArg_ParseTuple(args, "|s", &path)) {
        if (path != NULL) {
            FILE *fp = fopen(path, "rb");
            if (fp == NULL) {
                PyErr_SetString(PyExc_IOError, strerror(errno));
                return (PyObject *)model;
            } else {
                pm = PPMModel::load(fp);
                fclose(fp);
            }
        } else {
            pm = new PPMModel();
        }
        model = PyObject_New(Model, &Model_Type);
        model->model = pm;
    }
 
    return (PyObject *)model;
}
 
static void Model_dealloc(PyObject *self)
{
    Model_Ptr(self)->decref();
    PyObject_Del(self);
}
 
static PyObject *Model_dump(PyObject *self, PyObject *args)
{
    char *path = NULL;
    
    if (PyArg_ParseTuple(args, "s", &path)) {
        FILE *fp = fopen(path, "wb");
        if (fp == NULL) {
            PyErr_SetString(PyExc_IOError, strerror(errno));
        } else {
            PPMModel::dump(Model_Ptr(self), fp);
            fclose(fp);
        }
    }
    
    return Py_BuildValue("");
}
 
static PyObject *Model_train(PyObject *self, PyObject *args)
{
    char *path = NULL;
 
    if (PyArg_ParseTuple(args, "s", &path)) {
        FILE *fp = fopen(path, "rb");
        if (fp == NULL) {
            PyErr_SetString(PyExc_IOError, strerror(errno));
        } else {
            NullOutputAdapter nad;
 
            PPMEncoder<NullOutputAdapter, DefaultContextUpdater> penc(nad, Model_Ptr(self));
            penc.start_encoding();
            for (int ch = fgetc(fp); ch != EOF; ch = fgetc(fp)) {
                penc.encode(ch);
            }
            penc.finish_encoding();
            fclose(fp);
            
            return Py_BuildValue("i", nad.count());
        }
    }
    return Py_BuildValue("");
}
 
static PyObject *Model_predict(PyObject *self, PyObject *args)
{
    char *path = NULL;
 
    if (PyArg_ParseTuple(args, "s", &path)) {
        FILE *fp = fopen(path, "rb");
        if (fp == NULL) {
            PyErr_SetString(PyExc_IOError, strerror(errno));
        } else {
            NullOutputAdapter nad;
 
            PPMModel *pm = Model_Ptr(self);
            pm->m_buffer.reset();
            PPMEncoder<NullOutputAdapter, NopeContextUpdater> penc(nad, pm);
            penc.start_encoding();
            for (int ch = fgetc(fp); ch != EOF; ch = fgetc(fp))
                penc.encode(ch);
            penc.finish_encoding();
            fclose(fp);
 
            return Py_BuildValue("i", nad.count());
        }
    }
    return Py_BuildValue("");
}
 
static PyMethodDef Model_methods[] = {
    {"dump", Model_dump, METH_VARARGS},
    {"train", Model_train, METH_VARARGS},
    {"predict", Model_predict, METH_VARARGS},
    {NULL, NULL},
};
 
static PyObject * Model_GetAttr(PyObject *self, char *attrname)
{
    return Py_FindMethod(Model_methods, self, attrname);
}
    
static PyMethodDef methods[] = {
    {"Model", Model_New, METH_VARARGS},
    {NULL, NULL},
};
 
void initpyppm()
{
    Py_InitModule("pyppm", methods);
}
 
} // extern "C"