mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 19:22:50 -06:00
Added CId and categories to py-bindings.
This commit is contained in:
@@ -69,12 +69,28 @@ checkType(void* obj, PyTypeObject* tp)
|
|||||||
|
|
||||||
/* new types and declarations */
|
/* new types and declarations */
|
||||||
|
|
||||||
|
NEWGF(CId,GF_CId,CIdType,"gf.cid","c identifier")
|
||||||
NEWGF(Lang,GF_Language,LangType,"gf.lang","language")
|
NEWGF(Lang,GF_Language,LangType,"gf.lang","language")
|
||||||
NEWGF(gfType,GF_Type,gfTypeType,"gf.type","gf type")
|
NEWGF(gfType,GF_Type,gfTypeType,"gf.type","gf type")
|
||||||
NEWGF(PGFModule,GF_PGF,PGFType,"gf.pgf","PGF module")
|
NEWGF(PGFModule,GF_PGF,PGFType,"gf.pgf","PGF module")
|
||||||
NEWGF(Expr,GF_Expr,ExprType,"gf.expr","gf expression")
|
NEWGF(Expr,GF_Expr,ExprType,"gf.expr","gf expression")
|
||||||
NEWGF(Tree,GF_Tree,TreeType,"gf.tree","gf tree")
|
NEWGF(Tree,GF_Tree,TreeType,"gf.tree","gf tree")
|
||||||
|
|
||||||
|
/* CId methods, constructor and destructor */
|
||||||
|
|
||||||
|
|
||||||
|
DEALLOCFN(CId_dealloc, CId, gf_freeCId, "freeCId")
|
||||||
|
|
||||||
|
static PyObject*
|
||||||
|
CId_repr(CId *self)
|
||||||
|
{
|
||||||
|
char* str_cid = gf_showCId(self->obj);
|
||||||
|
PyObject* repr = PyString_FromString(str_cid);
|
||||||
|
free(str_cid);
|
||||||
|
return repr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* PGF methods, constructor and destructor */
|
/* PGF methods, constructor and destructor */
|
||||||
|
|
||||||
@@ -101,6 +117,20 @@ startCategory(PyObject *self, PyObject *noarg)
|
|||||||
return cat;
|
return cat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject*
|
||||||
|
categories(PGFModule* self)
|
||||||
|
{
|
||||||
|
PyObject* cats = PyList_New(0);
|
||||||
|
GF_CId *p = gf_categories(self->obj);
|
||||||
|
while (*p) {
|
||||||
|
CId* c = (CId*)CIdType.tp_new(&CIdType,NULL,NULL);
|
||||||
|
c->obj = *(p++);
|
||||||
|
PyList_Append(cats, (PyObject*)c);
|
||||||
|
Py_DECREF(c); //?
|
||||||
|
}
|
||||||
|
return cats;
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
languages(PGFModule* self)
|
languages(PGFModule* self)
|
||||||
{
|
{
|
||||||
@@ -197,6 +227,7 @@ static PyMethodDef pgf_methods[] = {
|
|||||||
{"parse", (PyCFunction)parse, METH_VARARGS|METH_KEYWORDS,"Parse a string."},
|
{"parse", (PyCFunction)parse, METH_VARARGS|METH_KEYWORDS,"Parse a string."},
|
||||||
{"lin", (PyCFunction)linearize, METH_VARARGS,"Linearize tree."},
|
{"lin", (PyCFunction)linearize, METH_VARARGS,"Linearize tree."},
|
||||||
{"startcat", (PyCFunction)startCategory, METH_NOARGS,"Get the start category."},
|
{"startcat", (PyCFunction)startCategory, METH_NOARGS,"Get the start category."},
|
||||||
|
{"categories", (PyCFunction)categories, METH_NOARGS,"Get all categories."},
|
||||||
{"abstract", (PyCFunction)abstractName, METH_NOARGS,"Get the module abstract name."},
|
{"abstract", (PyCFunction)abstractName, METH_NOARGS,"Get the module abstract name."},
|
||||||
{"languages", (PyCFunction)languages, METH_NOARGS,"Get the module languages."},
|
{"languages", (PyCFunction)languages, METH_NOARGS,"Get the module languages."},
|
||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||||
@@ -267,6 +298,7 @@ initgf(void)
|
|||||||
t.tp_dealloc = (destructor)tdealloc; \
|
t.tp_dealloc = (destructor)tdealloc; \
|
||||||
if (PyType_Ready(&t) < 0) return;
|
if (PyType_Ready(&t) < 0) return;
|
||||||
|
|
||||||
|
READYTYPE(CIdType, CId_repr, CId_dealloc)
|
||||||
PGFType.tp_methods = pgf_methods;
|
PGFType.tp_methods = pgf_methods;
|
||||||
READYTYPE(PGFType, pgf_repr, PGF_dealloc)
|
READYTYPE(PGFType, pgf_repr, PGF_dealloc)
|
||||||
READYTYPE(LangType, lang_repr, Lang_dealloc)
|
READYTYPE(LangType, lang_repr, Lang_dealloc)
|
||||||
|
|||||||
@@ -23,22 +23,37 @@ def rmprefix(obj):
|
|||||||
# s = `obj`
|
# s = `obj`
|
||||||
# m = hexre.match(s)
|
# m = hexre.match(s)
|
||||||
# return m and s[m.end(0):]
|
# return m and s[m.end(0):]
|
||||||
|
|
||||||
|
class TestPgfInfo(unittest.TestCase):
|
||||||
|
def pgf(self):
|
||||||
|
return gf.read_pgf(self.path)
|
||||||
|
def setUp(self):
|
||||||
|
self.path = 'Query.pgf'
|
||||||
|
def test_readPgf(self):
|
||||||
|
pgf = self.pgf()
|
||||||
|
self.assertNotEqual(pgf,None)
|
||||||
|
def test_startcat(self):
|
||||||
|
pgf = self.pgf()
|
||||||
|
cat = pgf.startcat()
|
||||||
|
self.assertEqual(rmprefix(cat),'Question')
|
||||||
|
def test_categories(self):
|
||||||
|
pgf = self.pgf()
|
||||||
|
cats = [`c` for c in pgf.categories()]
|
||||||
|
self.failUnless('Float' in cats)
|
||||||
|
self.failUnless('Int' in cats)
|
||||||
|
self.failUnless('String' in cats)
|
||||||
|
def test_createLanguage(self):
|
||||||
|
pgf = self.pgf()
|
||||||
|
for lang in 'QueryEng QuerySpa'.split():
|
||||||
|
l = gf.read_language(lang)
|
||||||
|
self.assertEqual(rmprefix(l),lang)
|
||||||
|
|
||||||
class TestParsing(unittest.TestCase):
|
class TestParsing(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.lexed = samples
|
self.lexed = samples
|
||||||
self.lang = 'QueryEng'
|
self.lang = 'QueryEng'
|
||||||
self.pgf = "Query.pgf"
|
self.pgf = "Query.pgf"
|
||||||
def test_createPgf(self):
|
|
||||||
q = gf.read_pgf(self.pgf)
|
|
||||||
self.assertNotEqual(q,None)
|
|
||||||
def test_startcat(self):
|
|
||||||
pgf = gf.read_pgf(self.pgf)
|
|
||||||
cat = pgf.startcat()
|
|
||||||
self.assertEqual(rmprefix(cat),'Question')
|
|
||||||
def test_createLanguage(self):
|
|
||||||
l = gf.read_language(self.lang)
|
|
||||||
self.assertEqual(rmprefix(l),self.lang)
|
|
||||||
def test_parse(self):
|
def test_parse(self):
|
||||||
s = self.lexed[0]
|
s = self.lexed[0]
|
||||||
pgf = gf.read_pgf(self.pgf)
|
pgf = gf.read_pgf(self.pgf)
|
||||||
|
|||||||
Reference in New Issue
Block a user