Skip to content

Commit

Permalink
added lookupMorpho
Browse files Browse the repository at this point in the history
  • Loading branch information
krangelov committed Aug 31, 2023
1 parent e0c820b commit a33f64d
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions src/runtime/python/pypgf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
},
Expand Down

0 comments on commit a33f64d

Please sign in to comment.