static void finish(per_thread_data *ptd) { if (ptd->sent) sentence_delete(ptd->sent); ptd->sent = NULL; #if DO_PHRASE_TREE if (tree) linkage_free_constituent_tree(tree); tree = NULL; #endif if (ptd->linkage) linkage_delete(ptd->linkage); ptd->linkage = NULL; dictionary_delete(ptd->dict); ptd->dict = NULL; parse_options_delete(ptd->opts); ptd->opts = NULL; parse_options_delete(ptd->panic_parse_opts); ptd->panic_parse_opts = NULL; #ifdef USE_PTHREADS pthread_setspecific(java_key, NULL); #else global_ptd = NULL; #endif free(ptd); }
static PyObject *constituents(PyObject *self, PyObject *args) { Dictionary dict; Parse_Options opts; Sentence sent; Linkage linkage; CNode * cn; /// Link counts int num_linkages; const char *text; PyObject *output_list; if (!PyArg_ParseTuple(args, "s", &text)) return NULL; opts = parse_options_create(); parse_options_set_verbosity(opts, -1); 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); cn = linkage_constituent_tree(linkage); output_list = build_tree(cn, linkage); if(output_list == Py_None) { Py_INCREF(output_list); return output_list; } linkage_free_constituent_tree(cn); 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("S", output_list); }
/** * Print out the constituent tree. * mode 1: treebank-style constituent tree * mode 2: flat, bracketed tree [A like [B this B] A] * mode 3: flat, treebank-style tree (A like (B this)) */ char * linkage_print_constituent_tree(Linkage linkage, ConstituentDisplayStyle mode) { String * cs; CNode * root; char * p; if (!linkage) return NULL; if (mode == NO_DISPLAY) { return NULL; } else if (mode == MULTILINE || mode == SINGLE_LINE) { cs = string_new(); root = linkage_constituent_tree(linkage); print_tree(cs, (mode==1), root, 0, 0); linkage_free_constituent_tree(root); append_string(cs, "\n"); p = string_copy(cs); string_delete(cs); return p; } else if (mode == BRACKET_TREE) { return print_flat_constituents(linkage); } prt_error("Warning: Illegal mode %d for printing constituents\n" "Allowed values: %d to %d\n", mode, NO_DISPLAY, MAX_STYLES); return NULL; }
void linkage_free_constituent_tree(CNode * n) { CNode *m, *x; for (m=n->child; m!=NULL; m=x) { x=m->next; linkage_free_constituent_tree(m); } exfree(n->label, sizeof(char)*(strlen(n->label)+1)); exfree(n, sizeof(CNode)); }
std::map<int, EdgeDescription> LinkParserAdapterImpl::parseSentence(std::string sentenceStr) { Parse_Options parseOptions = parse_options_create(); int verbosity = psi_logger.getLoggingPriority() / 100 - 4; parse_options_set_verbosity(parseOptions, verbosity); starts_.clear(); ends_.clear(); edgeDescriptions_.clear(); freeSentence(); sentence_ = sentence_create(sentenceStr.c_str(), dictionary_); if (!sentence_) { std::stringstream errorSs; errorSs << "Link-parser failed to tokenize the input text."; throw ParserException(errorSs.str()); } boost::algorithm::to_lower(sentenceStr); if (sentence_parse(sentence_, parseOptions)) { size_t currentPos = 0; size_t foundPos = 0; int wordNo = 0; while (wordNo < sentence_length(sentence_)) { std::string word(sentence_get_word(sentence_, wordNo)); boost::algorithm::to_lower(word); foundPos = sentenceStr.find(word, currentPos); if (foundPos != std::string::npos) { starts_[wordNo] = foundPos; ends_[wordNo] = currentPos = foundPos + word.length(); } ++wordNo; } Linkage linkage = linkage_create(0, sentence_, parseOptions); CNode * ctree = linkage_constituent_tree(linkage); extractEdgeDescriptions(ctree, linkage); linkage_free_constituent_tree(ctree); linkage_delete(linkage); } else { std::stringstream errorSs; errorSs << "Link-parser failed to parse the input text.\n" << "Your input text is probably not a correct sentence."; WARN(errorSs.str()); } return edgeDescriptions_; }
static void makeLinkage(per_thread_data *ptd) { if (ptd->cur_linkage < ptd->num_linkages) { if (ptd->linkage) linkage_delete(ptd->linkage); ptd->linkage = linkage_create(ptd->cur_linkage,ptd->sent,ptd->opts); linkage_compute_union(ptd->linkage); linkage_set_current_sublinkage(ptd->linkage, linkage_get_num_sublinkages(ptd->linkage)-1); #if DO_PHRASE_TREE if (tree) linkage_free_constituent_tree(tree); tree = linkage_constituent_tree(linkage); printTree(tree); #endif } }
/** * Print out the constituent tree. * mode 1: treebank-style constituent tree * mode 2: flat, bracketed tree [A like [B this B] A] * mode 3: flat, treebank-style tree (A like (B this) ) */ char * linkage_print_constituent_tree(Linkage linkage, int mode) { String * cs; CNode * root; char * p; if ((mode == 0) || (linkage->sent->dict->constituent_pp == NULL)) { return NULL; } else if (mode == 1 || mode == 3) { cs = String_create(); root = linkage_constituent_tree(linkage); print_tree(cs, (mode==1), root, 0, 0); linkage_free_constituent_tree(root); append_string(cs, "\n"); p = exalloc(strlen(cs->p)+1); strcpy(p, cs->p); exfree(cs->p, sizeof(char)*cs->allocated); exfree(cs, sizeof(String)); return p; } else if (mode == 2) { char * str; con_context_t *ctxt; ctxt = (con_context_t *) malloc(sizeof(con_context_t)); str = print_flat_constituents(ctxt, linkage); free(ctxt); return str; } assert(0, "Illegal mode in linkage_print_constituent_tree"); return NULL; }