forked from GitHub/gf-core
Merge branch 'majestic' of github.com:GrammaticalFramework/gf-core into majestic
This commit is contained in:
@@ -36,6 +36,8 @@ main = do
|
|||||||
,TestCase (assertEqual "new function type" (Just ty) (functionType gr2 "foo"))
|
,TestCase (assertEqual "new function type" (Just ty) (functionType gr2 "foo"))
|
||||||
,TestCase (assertEqual "old function prob" (-log 0) (functionProbability gr1 "foo"))
|
,TestCase (assertEqual "old function prob" (-log 0) (functionProbability gr1 "foo"))
|
||||||
,TestCase (assertEqual "new function prob" pi (functionProbability gr2 "foo"))
|
,TestCase (assertEqual "new function prob" pi (functionProbability gr2 "foo"))
|
||||||
|
,TestCase (assertEqual "old category prob" (-log 0) (categoryProbability gr1 "Q"))
|
||||||
|
,TestCase (assertEqual "new category prob" pi (categoryProbability gr2 "Q"))
|
||||||
]
|
]
|
||||||
|
|
||||||
performMajorGC
|
performMajorGC
|
||||||
|
|||||||
@@ -28,28 +28,15 @@ Type_init(TypeObject *self, PyObject *args, PyObject *kwds)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Py_ssize_t i = 0; i < PyList_Size(hypos); i++) {
|
for (Py_ssize_t i = 0; i < PyList_Size(hypos); i++) {
|
||||||
PyObject *tup = PyList_GetItem(hypos, i);
|
if (!PyObject_TypeCheck(PyList_GetItem(hypos, i), &pgf_HypoType)) {
|
||||||
if (!PyObject_TypeCheck(tup, &PyTuple_Type)) {
|
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type initialisation");
|
||||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: not a tuple");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (!PyLong_Check(PyTuple_GetItem(tup, 0))) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: bind type not an integer");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (!PyUnicode_Check(PyTuple_GetItem(tup, 1))) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: variable not a string");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (!PyObject_TypeCheck(PyTuple_GetItem(tup, 2), &pgf_TypeType)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type_init: type not a type");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// Py_INCREF(&hypos[i]);
|
// Py_INCREF(&hypos[i]);
|
||||||
}
|
}
|
||||||
for (Py_ssize_t i = 0; i < PyList_Size(exprs); i++) {
|
for (Py_ssize_t i = 0; i < PyList_Size(exprs); i++) {
|
||||||
if (!PyObject_TypeCheck(PyList_GetItem(exprs, i), &pgf_ExprType)) {
|
if (!PyObject_TypeCheck(PyList_GetItem(exprs, i), &pgf_ExprType)) {
|
||||||
PyErr_SetString(PyExc_TypeError, "invalid expression in Type_init");
|
PyErr_SetString(PyExc_TypeError, "invalid expression in Type initialisation");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// Py_INCREF(&exprs[i]);
|
// Py_INCREF(&exprs[i]);
|
||||||
@@ -173,6 +160,124 @@ PyTypeObject pgf_TypeType = {
|
|||||||
(newfunc) Type_new, /*tp_new */
|
(newfunc) Type_new, /*tp_new */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// hypos
|
||||||
|
|
||||||
|
static HypoObject *
|
||||||
|
Hypo_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
|
||||||
|
{
|
||||||
|
HypoObject* self = (HypoObject *)subtype->tp_alloc(subtype, 0);
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
Hypo_init(HypoObject *self, PyObject *args, PyObject *kwds)
|
||||||
|
{
|
||||||
|
int bind_type;
|
||||||
|
PyObject* cid;
|
||||||
|
TypeObject* type;
|
||||||
|
if (!PyArg_ParseTuple(args, "iUO!", &bind_type, &cid, &pgf_TypeType, &type)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bind_type == 0 || bind_type == 1) {
|
||||||
|
self->bind_type = PyLong_FromLong(bind_type);
|
||||||
|
} else {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "invalid bind type in hypo initialisation");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->cid = cid;
|
||||||
|
self->type = type;
|
||||||
|
Py_INCREF(self->bind_type);
|
||||||
|
Py_INCREF(self->cid);
|
||||||
|
Py_INCREF(self->type);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
Hypo_dealloc(HypoObject *self)
|
||||||
|
{
|
||||||
|
Py_XDECREF(self->bind_type);
|
||||||
|
Py_XDECREF(self->cid);
|
||||||
|
Py_XDECREF(self->type);
|
||||||
|
Py_TYPE(self)->tp_free(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
Hypo_richcompare(HypoObject *t1, PyObject *p2, int op)
|
||||||
|
{
|
||||||
|
bool same = false;
|
||||||
|
if (!PyObject_TypeCheck(p2, &pgf_HypoType)) goto done;
|
||||||
|
HypoObject *t2 = (HypoObject *)p2;
|
||||||
|
|
||||||
|
if (!PyObject_RichCompareBool(t1->bind_type, t2->bind_type, Py_EQ)) goto done;
|
||||||
|
if (PyUnicode_Compare(t1->cid, t2->cid) != 0) goto done;
|
||||||
|
if (!PyObject_RichCompareBool((PyObject *)t1->type, (PyObject *)t2->type, Py_EQ)) goto done;
|
||||||
|
|
||||||
|
same = true;
|
||||||
|
done:
|
||||||
|
|
||||||
|
if (op == Py_EQ) {
|
||||||
|
if (same) Py_RETURN_TRUE; else Py_RETURN_FALSE;
|
||||||
|
} else if (op == Py_NE) {
|
||||||
|
if (same) Py_RETURN_FALSE; else Py_RETURN_TRUE;
|
||||||
|
} else {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "comparison operation not supported");
|
||||||
|
Py_RETURN_NOTIMPLEMENTED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyMemberDef Hypo_members[] = {
|
||||||
|
{"bind_type", T_OBJECT_EX, offsetof(HypoObject, bind_type), READONLY, "bind type (explicit or implicit)"},
|
||||||
|
{"cid", T_OBJECT_EX, offsetof(HypoObject, cid), READONLY, "category name"},
|
||||||
|
{"type", T_OBJECT_EX, offsetof(HypoObject, type), READONLY, "type"},
|
||||||
|
{NULL} /* Sentinel */
|
||||||
|
};
|
||||||
|
|
||||||
|
PyTypeObject pgf_HypoType = {
|
||||||
|
PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
|
//0, /*ob_size*/
|
||||||
|
"pgf.Hypo", /*tp_name*/
|
||||||
|
sizeof(HypoObject), /*tp_basicsize*/
|
||||||
|
0, /*tp_itemsize*/
|
||||||
|
(destructor) Hypo_dealloc, /*tp_dealloc*/
|
||||||
|
0, /*tp_print*/
|
||||||
|
0, /*tp_getattr*/
|
||||||
|
0, /*tp_setattr*/
|
||||||
|
0, /*tp_compare*/
|
||||||
|
0, /*tp_repr*/
|
||||||
|
0, /*tp_as_number*/
|
||||||
|
0, /*tp_as_sequence*/
|
||||||
|
0, /*tp_as_mapping*/
|
||||||
|
0, /*tp_hash */
|
||||||
|
0, /*tp_call*/
|
||||||
|
0, /*tp_str*/
|
||||||
|
0, /*tp_getattro*/
|
||||||
|
0, /*tp_setattro*/
|
||||||
|
0, /*tp_as_buffer*/
|
||||||
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
||||||
|
"hypothesis in a type", /*tp_doc*/
|
||||||
|
0, /*tp_traverse */
|
||||||
|
0, /*tp_clear */
|
||||||
|
(richcmpfunc) Hypo_richcompare, /*tp_richcompare */
|
||||||
|
0, /*tp_weaklistoffset */
|
||||||
|
0, /*tp_iter */
|
||||||
|
0, /*tp_iternext */
|
||||||
|
0, /*tp_methods */
|
||||||
|
Hypo_members, /*tp_members */
|
||||||
|
0, /*tp_getset */
|
||||||
|
0, /*tp_base */
|
||||||
|
0, /*tp_dict */
|
||||||
|
0, /*tp_descr_get */
|
||||||
|
0, /*tp_descr_set */
|
||||||
|
0, /*tp_dictoffset */
|
||||||
|
(initproc) Hypo_init, /*tp_init */
|
||||||
|
0, /*tp_alloc */
|
||||||
|
(newfunc) Hypo_new, /*tp_new */
|
||||||
|
};
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// expressions
|
// expressions
|
||||||
|
|
||||||
@@ -252,7 +357,7 @@ Expr_unpack(ExprObject *self, PyObject *fargs)
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
if (PyObject_TypeCheck(expr, &pgf_ExprAbsType)) {
|
if (PyObject_TypeCheck(expr, &pgf_ExprAbsType)) {
|
||||||
ExprAbsObject *eabs = (ExprAbsObject *) expr;
|
ExprAbsObject *eabs = (ExprAbsObject *) expr;
|
||||||
PyObject* res =
|
PyObject* res =
|
||||||
Py_BuildValue("OOOO", eabs->bind_type, eabs->name, eabs->body, args);
|
Py_BuildValue("OOOO", eabs->bind_type, eabs->name, eabs->body, args);
|
||||||
Py_DECREF(args);
|
Py_DECREF(args);
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -8,13 +8,22 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
PyObject *hypos; // PyListObject of PyTupleObject: (bind_type: int, cid: string, type: TypeObject)
|
PyObject *hypos; // PyListObject of HypoObject
|
||||||
PyObject *name; // PyUnicodeObject
|
PyObject *name; // PyUnicodeObject
|
||||||
PyObject *exprs; // PyListObject of ExprObject
|
PyObject *exprs; // PyListObject of ExprObject
|
||||||
} TypeObject;
|
} TypeObject;
|
||||||
|
|
||||||
PyTypeObject pgf_TypeType;
|
PyTypeObject pgf_TypeType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PyObject_HEAD
|
||||||
|
PyObject *bind_type; // PyLongObject
|
||||||
|
PyObject *cid; // PyUnicodeObject
|
||||||
|
TypeObject *type;
|
||||||
|
} HypoObject;
|
||||||
|
|
||||||
|
PyTypeObject pgf_HypoType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
} ExprObject;
|
} ExprObject;
|
||||||
|
|||||||
@@ -72,32 +72,15 @@ PyList_AsHypos(PyObject *pylist, Py_ssize_t *n_hypos)
|
|||||||
PgfTypeHypo *hypos = PyMem_RawMalloc(sizeof(PgfTypeHypo)*n);
|
PgfTypeHypo *hypos = PyMem_RawMalloc(sizeof(PgfTypeHypo)*n);
|
||||||
|
|
||||||
for (Py_ssize_t i = 0; i < n; i++) {
|
for (Py_ssize_t i = 0; i < n; i++) {
|
||||||
PyObject *tup = PyList_GetItem(pylist, i);
|
PyObject *item = PyList_GetItem(pylist, i);
|
||||||
if (!PyTuple_Check(tup)) {
|
if (!PyObject_TypeCheck(item, &pgf_HypoType)) {
|
||||||
PyErr_SetString(PyExc_TypeError, "hypo must be a tuple");
|
PyErr_SetString(PyExc_TypeError, "hypothesis must be of type Hypo");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
HypoObject *hypo = (HypoObject *)item;
|
||||||
PyObject *t0 = PyTuple_GetItem(tup, 0);
|
hypos[i].bind_type = PyLong_AsLong(hypo->bind_type);
|
||||||
if (!PyLong_Check(t0)) {
|
hypos[i].cid = PyUnicode_AsPgfText(hypo->cid);
|
||||||
PyErr_SetString(PyExc_TypeError, "first element of hypo must be an integer");
|
hypos[i].type = (PgfType) hypo->type;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
hypos[i].bind_type = PyLong_AsLong(t0);
|
|
||||||
|
|
||||||
PyObject *t1 = PyTuple_GetItem(tup, 1);
|
|
||||||
if (!PyUnicode_Check(t1)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "second element of hypo must be a string");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
hypos[i].cid = PyUnicode_AsPgfText(t1);
|
|
||||||
|
|
||||||
PyObject *t2 = PyTuple_GetItem(tup, 2);
|
|
||||||
if (!PyObject_TypeCheck(t2, &pgf_TypeType)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "third element of hypo must be a Type");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
hypos[i].type = (PgfType) t2;
|
|
||||||
Py_INCREF(hypos[i].type);
|
Py_INCREF(hypos[i].type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,12 +96,12 @@ PyList_FromHypos(PgfTypeHypo *hypos, const size_t n_hypos)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < n_hypos; i++) {
|
for (size_t i = 0; i < n_hypos; i++) {
|
||||||
PyObject *tup = PyTuple_New(3);
|
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(hypos[i].bind_type));
|
hypo->bind_type = PyLong_FromLong(hypos[i].bind_type);
|
||||||
PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size));
|
hypo->cid = PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size);
|
||||||
PyTuple_SetItem(tup, 2, (PyObject *)hypos[i].type);
|
hypo->type = (TypeObject *)hypos[i].type;
|
||||||
Py_INCREF(hypos[i].type);
|
// Py_INCREF(hypo->type);
|
||||||
PyList_SetItem(pylist, i, tup);
|
PyList_SetItem(pylist, i, (PyObject *)hypo);
|
||||||
}
|
}
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(pylist);
|
Py_DECREF(pylist);
|
||||||
|
|||||||
@@ -18,6 +18,22 @@ PGF_dealloc(PGFObject *self)
|
|||||||
Py_TYPE(self)->tp_free(self);
|
Py_TYPE(self)->tp_free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
PGF_writeToFile(PGFObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
const char *fpath;
|
||||||
|
if (!PyArg_ParseTuple(args, "s", &fpath))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
PgfExn err;
|
||||||
|
pgf_write_pgf(fpath, self->db, self->revision, &err);
|
||||||
|
if (handleError(err) != PGF_EXN_NONE) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PgfItor fn;
|
PgfItor fn;
|
||||||
PGFObject *grammar;
|
PGFObject *grammar;
|
||||||
@@ -262,6 +278,26 @@ PGF_functionIsConstructor(PGFObject *self, PyObject *args)
|
|||||||
return PyBool_FromLong(isCon);
|
return PyBool_FromLong(isCon);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
PGF_categoryProbability(PGFObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
const char *s;
|
||||||
|
Py_ssize_t size;
|
||||||
|
if (!PyArg_ParseTuple(args, "s#", &s, &size))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
PgfText *catname = CString_AsPgfText(s, size);
|
||||||
|
|
||||||
|
PgfExn err;
|
||||||
|
prob_t prob = pgf_category_prob(self->db, self->revision, catname, &err);
|
||||||
|
FreePgfText(catname);
|
||||||
|
if (handleError(err) != PGF_EXN_NONE) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return PyFloat_FromDouble((double)prob);
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
PGF_functionProbability(PGFObject *self, PyObject *args)
|
PGF_functionProbability(PGFObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
@@ -307,6 +343,9 @@ static PyMemberDef PGF_members[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static PyMethodDef PGF_methods[] = {
|
static PyMethodDef PGF_methods[] = {
|
||||||
|
{"writeToFile", (PyCFunction)PGF_writeToFile, METH_VARARGS,
|
||||||
|
"Writes PGF to file"},
|
||||||
|
|
||||||
{"categoryContext", (PyCFunction)PGF_categoryContext, METH_VARARGS,
|
{"categoryContext", (PyCFunction)PGF_categoryContext, METH_VARARGS,
|
||||||
"Returns the context for a given category"
|
"Returns the context for a given category"
|
||||||
},
|
},
|
||||||
@@ -319,6 +358,9 @@ static PyMethodDef PGF_methods[] = {
|
|||||||
{"functionIsConstructor", (PyCFunction)PGF_functionIsConstructor, METH_VARARGS,
|
{"functionIsConstructor", (PyCFunction)PGF_functionIsConstructor, METH_VARARGS,
|
||||||
"Checks whether a function is a constructor"
|
"Checks whether a function is a constructor"
|
||||||
},
|
},
|
||||||
|
{"categoryProbability", (PyCFunction)PGF_categoryProbability, METH_VARARGS,
|
||||||
|
"Returns the probability of a category"
|
||||||
|
},
|
||||||
{"functionProbability", (PyCFunction)PGF_functionProbability, METH_VARARGS,
|
{"functionProbability", (PyCFunction)PGF_functionProbability, METH_VARARGS,
|
||||||
"Returns the probability of a function"
|
"Returns the probability of a function"
|
||||||
},
|
},
|
||||||
@@ -532,56 +574,56 @@ pgf_showType(PyObject *self, PyObject *args)
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static HypoObject *
|
||||||
pgf_mkHypo(PyObject *self, PyObject *args)
|
pgf_mkHypo(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *type;
|
TypeObject *type;
|
||||||
if (!PyArg_ParseTuple(args, "O!", &pgf_TypeType, &type))
|
if (!PyArg_ParseTuple(args, "O!", &pgf_TypeType, &type))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
PyObject *tup = PyTuple_New(3);
|
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(0)); // explicit
|
hypo->bind_type = PyLong_FromLong(0); // explicit
|
||||||
PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize("_", 1));
|
hypo->cid = PyUnicode_FromStringAndSize("_", 1);
|
||||||
PyTuple_SetItem(tup, 2, type);
|
hypo->type = type;
|
||||||
Py_INCREF(type);
|
Py_INCREF(hypo->type);
|
||||||
|
|
||||||
return tup;
|
return hypo;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static HypoObject *
|
||||||
pgf_mkDepHypo(PyObject *self, PyObject *args)
|
pgf_mkDepHypo(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *var;
|
PyObject *var;
|
||||||
PyObject *type;
|
TypeObject *type;
|
||||||
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
|
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
PyObject *tup = PyTuple_New(3);
|
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(0)); // explicit
|
hypo->bind_type = PyLong_FromLong(0); // explicit
|
||||||
PyTuple_SetItem(tup, 1, var);
|
hypo->cid = var;
|
||||||
PyTuple_SetItem(tup, 2, type);
|
hypo->type = type;
|
||||||
Py_INCREF(var);
|
Py_INCREF(hypo->cid);
|
||||||
Py_INCREF(type);
|
Py_INCREF(hypo->type);
|
||||||
|
|
||||||
return tup;
|
return hypo;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static HypoObject *
|
||||||
pgf_mkImplHypo(PyObject *self, PyObject *args)
|
pgf_mkImplHypo(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *var;
|
PyObject *var;
|
||||||
PyObject *type;
|
TypeObject *type;
|
||||||
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
|
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
PyObject *tup = PyTuple_New(3);
|
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
|
||||||
PyTuple_SetItem(tup, 0, PyLong_FromLong(1)); // implicit
|
hypo->bind_type = PyLong_FromLong(1); // implicit
|
||||||
PyTuple_SetItem(tup, 1, var);
|
hypo->cid = var;
|
||||||
PyTuple_SetItem(tup, 2, type);
|
hypo->type = type;
|
||||||
Py_INCREF(var);
|
Py_INCREF(hypo->cid);
|
||||||
Py_INCREF(type);
|
Py_INCREF(hypo->type);
|
||||||
|
|
||||||
return tup;
|
return hypo;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef module_methods[] = {
|
static PyMethodDef module_methods[] = {
|
||||||
@@ -668,6 +710,9 @@ MOD_INIT(pgf)
|
|||||||
if (PyType_Ready(&pgf_TypeType) < 0)
|
if (PyType_Ready(&pgf_TypeType) < 0)
|
||||||
return MOD_ERROR_VAL;
|
return MOD_ERROR_VAL;
|
||||||
|
|
||||||
|
if (PyType_Ready(&pgf_HypoType) < 0)
|
||||||
|
return MOD_ERROR_VAL;
|
||||||
|
|
||||||
MOD_DEF(m, "pgf", "The Runtime for Portable Grammar Format in Python", module_methods);
|
MOD_DEF(m, "pgf", "The Runtime for Portable Grammar Format in Python", module_methods);
|
||||||
if (m == NULL)
|
if (m == NULL)
|
||||||
return MOD_ERROR_VAL;
|
return MOD_ERROR_VAL;
|
||||||
@@ -712,6 +757,9 @@ MOD_INIT(pgf)
|
|||||||
PyModule_AddObject(m, "Type", (PyObject *) &pgf_TypeType);
|
PyModule_AddObject(m, "Type", (PyObject *) &pgf_TypeType);
|
||||||
// Py_INCREF(&pgf_TypeType);
|
// Py_INCREF(&pgf_TypeType);
|
||||||
|
|
||||||
|
PyModule_AddObject(m, "Hypo", (PyObject *) &pgf_HypoType);
|
||||||
|
// Py_INCREF(&pgf_TypeType);
|
||||||
|
|
||||||
PyModule_AddIntConstant(m, "BIND_TYPE_EXPLICIT", 0);
|
PyModule_AddIntConstant(m, "BIND_TYPE_EXPLICIT", 0);
|
||||||
|
|
||||||
PyModule_AddIntConstant(m, "BIND_TYPE_IMPLICIT", 1);
|
PyModule_AddIntConstant(m, "BIND_TYPE_IMPLICIT", 1);
|
||||||
|
|||||||
@@ -77,6 +77,12 @@ def test_newNGF_existing(NGF):
|
|||||||
with pytest.raises(FileExistsError):
|
with pytest.raises(FileExistsError):
|
||||||
newNGF("empty", "./basic.ngf")
|
newNGF("empty", "./basic.ngf")
|
||||||
|
|
||||||
|
# writePGF
|
||||||
|
|
||||||
|
def test_writePGF(PGF):
|
||||||
|
PGF.writeToFile("./copy.pgf")
|
||||||
|
os.remove("./copy.pgf") # cleanup
|
||||||
|
|
||||||
# abstract syntax
|
# abstract syntax
|
||||||
|
|
||||||
def test_abstractName(PGF):
|
def test_abstractName(PGF):
|
||||||
@@ -106,10 +112,11 @@ def test_categoryContext_2(PGF):
|
|||||||
def test_categoryContext_3(PGF):
|
def test_categoryContext_3(PGF):
|
||||||
cxt = PGF.categoryContext("P")
|
cxt = PGF.categoryContext("P")
|
||||||
assert len(cxt) == 1
|
assert len(cxt) == 1
|
||||||
tup = cxt[0]
|
hypo = cxt[0]
|
||||||
assert tup[0] == 0 # explicit
|
assert isinstance(hypo, Hypo)
|
||||||
assert tup[1] == "_" # cid
|
assert hypo.bind_type == BIND_TYPE_EXPLICIT
|
||||||
assert tup[2] == readType("N")
|
assert hypo.cid == "_"
|
||||||
|
assert hypo.type == readType("N")
|
||||||
|
|
||||||
def test_categoryContext_4(PGF):
|
def test_categoryContext_4(PGF):
|
||||||
assert PGF.categoryContext("X") == None
|
assert PGF.categoryContext("X") == None
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def gr2(gr1):
|
|||||||
gr = gr1
|
gr = gr1
|
||||||
t = gr.newTransaction()
|
t = gr.newTransaction()
|
||||||
t.createFunction("foo", ty, 0, prob)
|
t.createFunction("foo", ty, 0, prob)
|
||||||
t.createCategory("Q", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
t.createCategory("Q", [Hypo(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
||||||
t.commit()
|
t.commit()
|
||||||
yield gr
|
yield gr
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ def gr3(gr1):
|
|||||||
gr = gr1
|
gr = gr1
|
||||||
with gr.newTransaction("bar_branch") as t:
|
with gr.newTransaction("bar_branch") as t:
|
||||||
t.createFunction("bar", ty, 0, prob)
|
t.createFunction("bar", ty, 0, prob)
|
||||||
t.createCategory("R", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
t.createCategory("R", [Hypo(BIND_TYPE_EXPLICIT, "x", ty)], prob)
|
||||||
yield gr
|
yield gr
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
@@ -70,6 +70,12 @@ def test_original_function_prob(gr1):
|
|||||||
# gr1.functionProbability("foo")
|
# gr1.functionProbability("foo")
|
||||||
assert gr1.functionProbability("foo") == float('inf')
|
assert gr1.functionProbability("foo") == float('inf')
|
||||||
|
|
||||||
|
@pytest.mark.skip(reason="failing")
|
||||||
|
def test_original_category_prob(gr1):
|
||||||
|
# with pytest.raises(KeyError):
|
||||||
|
# gr1.categoryProbability("Q")
|
||||||
|
assert gr1.categoryProbability("Q") == float('inf')
|
||||||
|
|
||||||
# gr2
|
# gr2
|
||||||
|
|
||||||
def test_extended_functions(gr2):
|
def test_extended_functions(gr2):
|
||||||
@@ -79,7 +85,7 @@ def test_extended_categories(gr2):
|
|||||||
assert gr2.categories == ["Float","Int","N","P","Q","S","String"]
|
assert gr2.categories == ["Float","Int","N","P","Q","S","String"]
|
||||||
|
|
||||||
def test_extended_category_context(gr2):
|
def test_extended_category_context(gr2):
|
||||||
assert gr2.categoryContext("Q") == [(BIND_TYPE_EXPLICIT, "x", ty)]
|
assert gr2.categoryContext("Q") == [Hypo(BIND_TYPE_EXPLICIT, "x", ty)]
|
||||||
|
|
||||||
def test_extended_function_type(gr2):
|
def test_extended_function_type(gr2):
|
||||||
assert gr2.functionType("foo") == ty
|
assert gr2.functionType("foo") == ty
|
||||||
@@ -89,6 +95,10 @@ def test_extended_function_prob(gr2):
|
|||||||
# assert gr2.functionProbability("foo") == prob
|
# assert gr2.functionProbability("foo") == prob
|
||||||
assert math.isclose(gr2.functionProbability("foo"), prob, rel_tol=1e-06)
|
assert math.isclose(gr2.functionProbability("foo"), prob, rel_tol=1e-06)
|
||||||
|
|
||||||
|
@pytest.mark.skip(reason="failing")
|
||||||
|
def test_extended_category_prob(gr2):
|
||||||
|
assert gr2.categoryProbability("Q") == prob
|
||||||
|
|
||||||
# gr3
|
# gr3
|
||||||
|
|
||||||
def test_branched_functions(gr3):
|
def test_branched_functions(gr3):
|
||||||
@@ -98,7 +108,7 @@ def test_branched_categories(gr3):
|
|||||||
assert gr3.categories == ["Float","Int","N","P","R","S","String"]
|
assert gr3.categories == ["Float","Int","N","P","R","S","String"]
|
||||||
|
|
||||||
def test_branched_category_context(gr3):
|
def test_branched_category_context(gr3):
|
||||||
assert gr3.categoryContext("R") == [(BIND_TYPE_EXPLICIT, "x", ty)]
|
assert gr3.categoryContext("R") == [Hypo(BIND_TYPE_EXPLICIT, "x", ty)]
|
||||||
|
|
||||||
def test_branched_function_type(gr3):
|
def test_branched_function_type(gr3):
|
||||||
assert gr3.functionType("bar") == ty
|
assert gr3.functionType("bar") == ty
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ Transaction_createFunction(TransactionObject *self, PyObject *args)
|
|||||||
Py_ssize_t size;
|
Py_ssize_t size;
|
||||||
TypeObject *type;
|
TypeObject *type;
|
||||||
Py_ssize_t arity = 0;
|
Py_ssize_t arity = 0;
|
||||||
float prob = 0.0;
|
prob_t prob = 0.0;
|
||||||
if (!PyArg_ParseTuple(args, "s#O!nf", &s, &size, &pgf_TypeType, &type, &arity, &prob))
|
if (!PyArg_ParseTuple(args, "s#O!nf", &s, &size, &pgf_TypeType, &type, &arity, &prob))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -136,15 +136,9 @@ Transaction_createCategory(TransactionObject *self, PyObject *args)
|
|||||||
const char *s;
|
const char *s;
|
||||||
Py_ssize_t size;
|
Py_ssize_t size;
|
||||||
PyObject *hypos;
|
PyObject *hypos;
|
||||||
float prob = 0.0;
|
prob_t prob = 0.0;
|
||||||
// if (!PyArg_ParseTuple(args, "s#O!f", &s, &size, &PyList_Type, &hypos, prob)) // segfaults in Python 3.8 but not 3.7
|
if (!PyArg_ParseTuple(args, "s#O!f", &s, &size, &PyList_Type, &hypos, &prob))
|
||||||
// return NULL;
|
|
||||||
if (!PyArg_ParseTuple(args, "s#Of", &s, &size, &hypos, prob))
|
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!PyObject_TypeCheck(hypos, &PyList_Type)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "hypos must be a list");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
PgfText *catname = CString_AsPgfText(s, size);
|
PgfText *catname = CString_AsPgfText(s, size);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user