Merge branch 'majestic' of github.com:GrammaticalFramework/gf-core into majestic

This commit is contained in:
krangelov
2021-09-27 13:29:11 +02:00
8 changed files with 250 additions and 92 deletions

View File

@@ -36,6 +36,8 @@ main = do
,TestCase (assertEqual "new function type" (Just ty) (functionType gr2 "foo"))
,TestCase (assertEqual "old function prob" (-log 0) (functionProbability gr1 "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

View File

@@ -28,28 +28,15 @@ Type_init(TypeObject *self, PyObject *args, PyObject *kwds)
}
for (Py_ssize_t i = 0; i < PyList_Size(hypos); i++) {
PyObject *tup = PyList_GetItem(hypos, i);
if (!PyObject_TypeCheck(tup, &PyTuple_Type)) {
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");
if (!PyObject_TypeCheck(PyList_GetItem(hypos, i), &pgf_HypoType)) {
PyErr_SetString(PyExc_TypeError, "invalid hypo in Type initialisation");
return -1;
}
// Py_INCREF(&hypos[i]);
}
for (Py_ssize_t i = 0; i < PyList_Size(exprs); i++) {
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;
}
// Py_INCREF(&exprs[i]);
@@ -173,6 +160,124 @@ PyTypeObject pgf_TypeType = {
(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
@@ -252,7 +357,7 @@ Expr_unpack(ExprObject *self, PyObject *fargs)
for (;;) {
if (PyObject_TypeCheck(expr, &pgf_ExprAbsType)) {
ExprAbsObject *eabs = (ExprAbsObject *) expr;
PyObject* res =
PyObject* res =
Py_BuildValue("OOOO", eabs->bind_type, eabs->name, eabs->body, args);
Py_DECREF(args);
return res;

View File

@@ -8,13 +8,22 @@
typedef struct {
PyObject_HEAD
PyObject *hypos; // PyListObject of PyTupleObject: (bind_type: int, cid: string, type: TypeObject)
PyObject *name; // PyUnicodeObject
PyObject *hypos; // PyListObject of HypoObject
PyObject *name; // PyUnicodeObject
PyObject *exprs; // PyListObject of ExprObject
} TypeObject;
PyTypeObject pgf_TypeType;
typedef struct {
PyObject_HEAD
PyObject *bind_type; // PyLongObject
PyObject *cid; // PyUnicodeObject
TypeObject *type;
} HypoObject;
PyTypeObject pgf_HypoType;
typedef struct {
PyObject_HEAD
} ExprObject;

View File

@@ -72,32 +72,15 @@ PyList_AsHypos(PyObject *pylist, Py_ssize_t *n_hypos)
PgfTypeHypo *hypos = PyMem_RawMalloc(sizeof(PgfTypeHypo)*n);
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *tup = PyList_GetItem(pylist, i);
if (!PyTuple_Check(tup)) {
PyErr_SetString(PyExc_TypeError, "hypo must be a tuple");
PyObject *item = PyList_GetItem(pylist, i);
if (!PyObject_TypeCheck(item, &pgf_HypoType)) {
PyErr_SetString(PyExc_TypeError, "hypothesis must be of type Hypo");
return NULL;
}
PyObject *t0 = PyTuple_GetItem(tup, 0);
if (!PyLong_Check(t0)) {
PyErr_SetString(PyExc_TypeError, "first element of hypo must be an integer");
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;
HypoObject *hypo = (HypoObject *)item;
hypos[i].bind_type = PyLong_AsLong(hypo->bind_type);
hypos[i].cid = PyUnicode_AsPgfText(hypo->cid);
hypos[i].type = (PgfType) hypo->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++) {
PyObject *tup = PyTuple_New(3);
PyTuple_SetItem(tup, 0, PyLong_FromLong(hypos[i].bind_type));
PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size));
PyTuple_SetItem(tup, 2, (PyObject *)hypos[i].type);
Py_INCREF(hypos[i].type);
PyList_SetItem(pylist, i, tup);
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
hypo->bind_type = PyLong_FromLong(hypos[i].bind_type);
hypo->cid = PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size);
hypo->type = (TypeObject *)hypos[i].type;
// Py_INCREF(hypo->type);
PyList_SetItem(pylist, i, (PyObject *)hypo);
}
if (PyErr_Occurred()) {
Py_DECREF(pylist);

View File

@@ -18,6 +18,22 @@ PGF_dealloc(PGFObject *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 {
PgfItor fn;
PGFObject *grammar;
@@ -262,6 +278,26 @@ PGF_functionIsConstructor(PGFObject *self, PyObject *args)
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 *
PGF_functionProbability(PGFObject *self, PyObject *args)
{
@@ -307,6 +343,9 @@ static PyMemberDef PGF_members[] = {
};
static PyMethodDef PGF_methods[] = {
{"writeToFile", (PyCFunction)PGF_writeToFile, METH_VARARGS,
"Writes PGF to file"},
{"categoryContext", (PyCFunction)PGF_categoryContext, METH_VARARGS,
"Returns the context for a given category"
},
@@ -319,6 +358,9 @@ static PyMethodDef PGF_methods[] = {
{"functionIsConstructor", (PyCFunction)PGF_functionIsConstructor, METH_VARARGS,
"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,
"Returns the probability of a function"
},
@@ -532,56 +574,56 @@ pgf_showType(PyObject *self, PyObject *args)
return str;
}
static PyObject *
static HypoObject *
pgf_mkHypo(PyObject *self, PyObject *args)
{
PyObject *type;
TypeObject *type;
if (!PyArg_ParseTuple(args, "O!", &pgf_TypeType, &type))
return NULL;
PyObject *tup = PyTuple_New(3);
PyTuple_SetItem(tup, 0, PyLong_FromLong(0)); // explicit
PyTuple_SetItem(tup, 1, PyUnicode_FromStringAndSize("_", 1));
PyTuple_SetItem(tup, 2, type);
Py_INCREF(type);
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
hypo->bind_type = PyLong_FromLong(0); // explicit
hypo->cid = PyUnicode_FromStringAndSize("_", 1);
hypo->type = type;
Py_INCREF(hypo->type);
return tup;
return hypo;
}
static PyObject *
static HypoObject *
pgf_mkDepHypo(PyObject *self, PyObject *args)
{
PyObject *var;
PyObject *type;
TypeObject *type;
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
return NULL;
PyObject *tup = PyTuple_New(3);
PyTuple_SetItem(tup, 0, PyLong_FromLong(0)); // explicit
PyTuple_SetItem(tup, 1, var);
PyTuple_SetItem(tup, 2, type);
Py_INCREF(var);
Py_INCREF(type);
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
hypo->bind_type = PyLong_FromLong(0); // explicit
hypo->cid = var;
hypo->type = type;
Py_INCREF(hypo->cid);
Py_INCREF(hypo->type);
return tup;
return hypo;
}
static PyObject *
static HypoObject *
pgf_mkImplHypo(PyObject *self, PyObject *args)
{
PyObject *var;
PyObject *type;
TypeObject *type;
if (!PyArg_ParseTuple(args, "UO!", &var, &pgf_TypeType, &type))
return NULL;
PyObject *tup = PyTuple_New(3);
PyTuple_SetItem(tup, 0, PyLong_FromLong(1)); // implicit
PyTuple_SetItem(tup, 1, var);
PyTuple_SetItem(tup, 2, type);
Py_INCREF(var);
Py_INCREF(type);
HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType);
hypo->bind_type = PyLong_FromLong(1); // implicit
hypo->cid = var;
hypo->type = type;
Py_INCREF(hypo->cid);
Py_INCREF(hypo->type);
return tup;
return hypo;
}
static PyMethodDef module_methods[] = {
@@ -668,6 +710,9 @@ MOD_INIT(pgf)
if (PyType_Ready(&pgf_TypeType) < 0)
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);
if (m == NULL)
return MOD_ERROR_VAL;
@@ -712,6 +757,9 @@ MOD_INIT(pgf)
PyModule_AddObject(m, "Type", (PyObject *) &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_IMPLICIT", 1);

View File

@@ -77,6 +77,12 @@ def test_newNGF_existing(NGF):
with pytest.raises(FileExistsError):
newNGF("empty", "./basic.ngf")
# writePGF
def test_writePGF(PGF):
PGF.writeToFile("./copy.pgf")
os.remove("./copy.pgf") # cleanup
# abstract syntax
def test_abstractName(PGF):
@@ -106,10 +112,11 @@ def test_categoryContext_2(PGF):
def test_categoryContext_3(PGF):
cxt = PGF.categoryContext("P")
assert len(cxt) == 1
tup = cxt[0]
assert tup[0] == 0 # explicit
assert tup[1] == "_" # cid
assert tup[2] == readType("N")
hypo = cxt[0]
assert isinstance(hypo, Hypo)
assert hypo.bind_type == BIND_TYPE_EXPLICIT
assert hypo.cid == "_"
assert hypo.type == readType("N")
def test_categoryContext_4(PGF):
assert PGF.categoryContext("X") == None

View File

@@ -15,7 +15,7 @@ def gr2(gr1):
gr = gr1
t = gr.newTransaction()
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()
yield gr
@@ -24,7 +24,7 @@ def gr3(gr1):
gr = gr1
with gr.newTransaction("bar_branch") as t:
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
@pytest.fixture(scope="function")
@@ -70,6 +70,12 @@ def test_original_function_prob(gr1):
# gr1.functionProbability("foo")
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
def test_extended_functions(gr2):
@@ -79,7 +85,7 @@ def test_extended_categories(gr2):
assert gr2.categories == ["Float","Int","N","P","Q","S","String"]
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):
assert gr2.functionType("foo") == ty
@@ -89,6 +95,10 @@ def test_extended_function_prob(gr2):
# assert gr2.functionProbability("foo") == prob
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
def test_branched_functions(gr3):
@@ -98,7 +108,7 @@ def test_branched_categories(gr3):
assert gr3.categories == ["Float","Int","N","P","R","S","String"]
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):
assert gr3.functionType("bar") == ty

View File

@@ -94,7 +94,7 @@ Transaction_createFunction(TransactionObject *self, PyObject *args)
Py_ssize_t size;
TypeObject *type;
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))
return NULL;
@@ -136,15 +136,9 @@ Transaction_createCategory(TransactionObject *self, PyObject *args)
const char *s;
Py_ssize_t size;
PyObject *hypos;
float 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
// return NULL;
if (!PyArg_ParseTuple(args, "s#Of", &s, &size, &hypos, prob))
prob_t prob = 0.0;
if (!PyArg_ParseTuple(args, "s#O!f", &s, &size, &PyList_Type, &hypos, &prob))
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);