From a33f64d23695dd2b0d0ad2cb4e85b2380839276e Mon Sep 17 00:00:00 2001 From: Krasimir Angelov Date: Thu, 31 Aug 2023 19:51:51 +0200 Subject: [PATCH] added lookupMorpho --- src/runtime/python/pypgf.c | 56 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index acd73deb8..1b96d713a 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -384,6 +384,58 @@ Concr_bracketedLinearize(ConcrObject* self, PyObject *args) return lin_out.bs; } +typedef struct { + PgfMorphoCallback fn; + PyObject* analyses; +} PyMorphoCallback; + +static void +pypgf_collect_morpho(PgfMorphoCallback* self, + PgfText *lemma, PgfText *analysis, prob_t prob, + PgfExn* err) +{ + PyMorphoCallback* callback = (PyMorphoCallback*) self; + + PyObject* py_lemma = PyUnicode_FromStringAndSize(lemma->text, lemma->size); + PyObject* py_analysis = PyUnicode_FromStringAndSize(analysis->text, analysis->size); + PyObject* res = + Py_BuildValue("OOf", py_lemma, py_analysis, prob); + + if (PyList_Append(callback->analyses, res) != 0) { + err->type = PGF_EXN_OTHER_ERROR; + } + + Py_DECREF(py_lemma); + Py_DECREF(py_analysis); + Py_DECREF(res); +} + +static PyObject* +Concr_lookupMorpho(ConcrObject* self, PyObject *args) { + const char *s; + Py_ssize_t size; + if (!PyArg_ParseTuple(args, "s#", &s, &size)) + return NULL; + + PgfText *sent = CString_AsPgfText(s, size); + + PyObject* analyses = PyList_New(0); + + PgfExn err; + err.type = PGF_EXN_NONE; + PyMorphoCallback callback = { { pypgf_collect_morpho }, analyses }; + pgf_lookup_morpho(self->grammar->db, self->concr, sent, &callback.fn, &err); + if (err.type != PGF_EXN_NONE) { + if (err.type == PGF_EXN_PGF_ERROR) { + PyErr_SetString(PGFError, err.msg); + } + Py_DECREF(analyses); + analyses = NULL; + } + + return analyses; +} + static PyGetSetDef Concr_getseters[] = { {"name", (getter)Concr_getName, NULL, @@ -447,10 +499,10 @@ static PyMethodDef Concr_methods[] = { }, {"graphvizParseTree", (PyCFunction)Concr_graphvizParseTree, METH_VARARGS, "Renders an abstract syntax tree as a parse tree in Graphviz format" - }, + },*/ {"lookupMorpho", (PyCFunction)Concr_lookupMorpho, METH_VARARGS, "Looks up a word in the lexicon of the grammar" - }, + },/* {"lookupCohorts", (PyCFunction)Concr_lookupCohorts, METH_VARARGS, "Takes a sentence and returns all matches for lexical items from the grammar in that sentence" },