From 7a485dfc5c54dc3e050b7f61ca5e983b68dfc85f Mon Sep 17 00:00:00 2001 From: "jordi.saludes" Date: Thu, 17 Jun 2010 07:59:00 +0000 Subject: [PATCH] Added linearization to py-bindings. --- contrib/py-bindings/gfmodule.c | 21 ++++++++++++++++++--- contrib/py-bindings/test.py | 28 +++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/contrib/py-bindings/gfmodule.c b/contrib/py-bindings/gfmodule.c index 5ed4f1851..2f9410d46 100644 --- a/contrib/py-bindings/gfmodule.c +++ b/contrib/py-bindings/gfmodule.c @@ -58,9 +58,9 @@ cbid(t *self) { \ /* utilities */ int -checkType(PyObject* obj, PyTypeObject* tp) +checkType(void* obj, PyTypeObject* tp) { - int isRight = PyObject_TypeCheck(obj, tp); + int isRight = PyObject_TypeCheck((PyObject*)obj, tp); if (!isRight) PyErr_Format(PyExc_TypeError, "Expected a %s", tp->tp_doc); return isRight; @@ -90,6 +90,20 @@ startCategory(PyObject *self, PyObject *noarg) return cat; } +static PyObject* +linearize(PGFModule *self, PyObject *args) +{ + Lang *lang; + Tree *tree; + if (!checkType(self,&PGFType)) return NULL; + if (!PyArg_ParseTuple(args, "OO", &lang, &tree)) + return NULL; + if (!checkType(lang,&LangType)) return NULL; + if (!checkType(tree,&TreeType)) return NULL; + char* c_lin = gf_linearize(self->obj, lang->obj, tree->obj); + return PyString_FromString(c_lin); +} + static PyObject* parse(PyObject *self, PyObject *args, PyObject *kws) { @@ -100,7 +114,7 @@ parse(PyObject *self, PyObject *args, PyObject *kws) char *lexed; static char *kwlist[] = {"lexed", "lang", "cat", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kws, "sO|O", kwlist, - &lexed, &lang_pyob, &cat_pyob)) + &lexed, &lang_pyob, &cat_pyob)) return NULL; if (!checkType(self, &PGFType)) return NULL; if (!checkType(lang_pyob, &LangType)) return NULL; @@ -146,6 +160,7 @@ readPGF(PyObject *self, PyObject *args) static PyMethodDef pgf_methods[] = { {"parse", (PyCFunction)parse, METH_VARARGS|METH_KEYWORDS,"Parse a string."}, + {"lin", (PyCFunction)linearize, METH_VARARGS,"Linearize tree."}, {"startcat", (PyCFunction)startCategory, METH_NOARGS,"Get the start category."}, {NULL, NULL, 0, NULL} /* Sentinel */ }; diff --git a/contrib/py-bindings/test.py b/contrib/py-bindings/test.py index a4e8edf3b..4a8f5bf3e 100644 --- a/contrib/py-bindings/test.py +++ b/contrib/py-bindings/test.py @@ -2,6 +2,11 @@ import gf import unittest + +samples = [ + ('is 89 odd',"Odd (Number 89)"), + ('is 21 prime',"Prime (Number 21)")] + import re hexre = re.compile('0x[0-9a-f]+:[ ]*') def rmprefix(obj): @@ -11,9 +16,7 @@ def rmprefix(obj): class TestParsing(unittest.TestCase): def setUp(self): - self.lexed = [ - ('is 89 odd',"Odd (Number 89)"), - ('is 21 prime',"Prime (Number 21)")] + self.lexed = samples self.lang = 'QueryEng' self.pgf = "Query.pgf" def test_createPgf(self): @@ -36,5 +39,24 @@ class TestParsing(unittest.TestCase): pt = rmprefix(ps[0]) self.assertEqual(pt,t) + +class TestLinearize(unittest.TestCase): + def setUp(self): + self.samples = samples + self.pgf = gf.read_pgf('Query.pgf') + self.lang = gf.read_language('QueryEng') + + def test_Linearize(self): + l = self.lang + for s,t in self.samples: + t = self.pgf.parse(s, l)[0] + self.assertEqual(s,self.pgf.lin(l,t)) + if __name__ == '__main__': unittest.main() + if 0: + q = gf.read_pgf('Query.pgf') + l = gf.read_language('QueryEng') + ts = q.parse('is 10 prime', l) + print ts[0] + print q.lin(l,ts[0])