Пример #1
0
static char * print_flat_constituents(con_context_t *ctxt, Linkage linkage)
{
	int num_words;
	Sentence sent;
	Postprocessor * pp;
	int s, numcon_total, numcon_subl, num_subl;
	char * q;

	sent = linkage_get_sentence(linkage);
	ctxt->phrase_ss = string_set_create();
	pp = linkage->sent->dict->constituent_pp;
	numcon_total = 0;

	count_words_used(ctxt, linkage);

	num_subl = linkage->num_sublinkages;
	if(num_subl > MAXSUBL) {
	  num_subl=MAXSUBL;
	  if(verbosity>=2) printf("Number of sublinkages exceeds maximum: only considering first %d sublinkages\n", MAXSUBL);
	}
	if(linkage->unionized==1 && num_subl>1) num_subl--;
	for (s=0; s<num_subl; s++) {
		linkage_set_current_sublinkage(linkage, s);
		linkage_post_process(linkage, pp);
		num_words = linkage_get_num_words(linkage);
		generate_misc_word_info(ctxt, linkage);
		numcon_subl = read_constituents_from_domains(ctxt, linkage, numcon_total, s);
		numcon_total = numcon_total + numcon_subl;
	}
	numcon_total = merge_constituents(ctxt, linkage, numcon_total);
	numcon_total = last_minute_fixes(ctxt, linkage, numcon_total);
	q = exprint_constituent_structure(ctxt, linkage, numcon_total);
	string_set_delete(ctxt->phrase_ss);
	ctxt->phrase_ss = NULL;
	return q;
}
Пример #2
0
/// This is the basic sentence dissection
static PyObject *sentence(PyObject *self, PyObject *args) {
    Dictionary    dict;
    Parse_Options opts;
    Sentence      sent;
    Linkage       linkage;
    Linkage       sub_linkage;
    char *        diagram;

    /// Link counts
    int   num_linkages;
    int   links;

    ///  Index's for the iterators
    int   link_idx;
    int   word_idx;
    int   num_words;
    long   span;
    long   sub_linkages;

    const char *text;
    const char *d_output;

    PyObject *output_list;
    PyObject *word_list;
    PyObject *word2_list;
    PyObject *span_list;
    PyObject *temp;
    PyObject *sublinkage_list;
    PyObject *_diagram;

    output_list = PyList_New(0);
    word_list   = PyList_New(0);
    word2_list  = PyList_New(0);
    sublinkage_list = PyList_New(0);

    span_list = PyList_New(0);

    if (!PyArg_ParseTuple(args, "s", &text))
        return NULL;

    opts = parse_options_create();
    parse_options_set_verbosity(opts, -1);
    parse_options_set_screen_width(opts, 50);

    setlocale(LC_ALL, "");
    dict = dictionary_create_default_lang();

    if (!dict) {
        PyErr_SetString(PyExc_RuntimeError, "Fatal error: Unable to open the dictionary");
        Py_INCREF(Py_None);
        return Py_None;
    }

    sent = sentence_create(text, dict);
    sentence_split(sent, opts);
    num_linkages = sentence_parse(sent, opts);

    if (num_linkages > 0) {
        linkage = linkage_create(0, sent, opts);
        /// Get the lengths of everything
        num_words = linkage_get_num_words(linkage);
        links = linkage_get_num_links(linkage);

        for(link_idx=0; link_idx < links; link_idx++) {
            PyObject *temp_subLen;

            diagram = linkage_print_diagram(linkage);
            _diagram = PyString_FromString(diagram);

            sub_linkage = linkage_create(link_idx, sent, opts);
            sub_linkages = linkage_get_num_sublinkages(linkage);

            temp_subLen = PyLong_FromLong(sub_linkages);
            linkage_delete(sub_linkage);
            PyList_Append(sublinkage_list, temp_subLen);

            span = linkage_get_link_length(linkage, link_idx);
            PyList_Append(span_list, PyInt_FromLong(span));

            PyObject *temp_list;
            temp_list = PyList_New(0);
            /// Sub Group these (left and right labels)
            const char *t1 = linkage_get_link_llabel(linkage, link_idx);
            temp = PyString_FromString(t1);
            PyList_Append(temp_list, temp);

            const char *t2 = linkage_get_link_rlabel(linkage, link_idx);
            temp = PyString_FromString(t2);
            PyList_Append(temp_list, temp);
            /// Then add to the main list
            PyList_Append(output_list, temp_list);


            /// Just the label
            const char *t3 = linkage_get_link_label(linkage, link_idx);
            temp = PyString_FromString(t3);
            PyList_Append(word2_list, temp);
        }

        for(word_idx=0; word_idx < num_words; word_idx++) {
            d_output = linkage_get_word(linkage, word_idx);
            PyObject *word;

            word = PyString_FromString(d_output);
            PyList_Append(word_list, word);
        }

        linkage_free_diagram(diagram);
        linkage_delete(linkage);

    } else {
        sentence_delete(sent);
        dictionary_delete(dict);
        parse_options_delete(opts);

        Py_INCREF(Py_None);
        return Py_None;
    }

    sentence_delete(sent);
    dictionary_delete(dict);
    parse_options_delete(opts);

    return Py_BuildValue("SSSSSS", word_list, span_list, output_list, word2_list, sublinkage_list, _diagram);
}
Пример #3
0
VALUE linkage_count_num_words(const VALUE self, VALUE link) {
	LinkagePtr *link_ptr = retrieve_linkage(link);
	int cnt = linkage_get_num_words(link_ptr->linkage);
	return INT2FIX(cnt);
}
Пример #4
0
/*
 * Class:      LinkGrammar
 * Method:     numWords
 * Signature: ()I
 */
JNIEXPORT jint JNICALL
Java_org_linkgrammar_LinkGrammar_getNumWords(JNIEnv *env, jclass cls)
{
	per_thread_data *ptd = get_ptd(env, cls);;
	return linkage_get_num_words(ptd->linkage);
}