const char * linkage_get_disjunct_str(const Linkage linkage, WordIdx w) { Disjunct *dj; if (NULL == linkage) return ""; if (NULL == linkage->disjunct_list_str) { lg_compute_disjunct_strings(linkage); } if (linkage->num_words <= w) return NULL; /* bounds-check */ /* dj will be null if the word wasn't used in the parse. */ dj = linkage->chosen_disjuncts[w]; if (NULL == dj) return ""; return linkage->disjunct_list_str[w]; }
void lg_corpus_linkage_senses(Linkage linkage) { const char * infword; Sentence sent = linkage->sent; Dictionary dict = sent->dict; Corpus *corp = dict->corpus; int nwords = sent->length; Linkage_info *lifo = linkage->info; int w; if (lifo->sense_list) return; /* Set up the disjunct strings first */ lg_compute_disjunct_strings(sent, lifo); lifo->nwords = nwords; lifo->sense_list = (Sense **) malloc(nwords * sizeof (Sense *)); memset(lifo->sense_list, 0, nwords * sizeof (Sense *)); /* Decrement nwords, so as to ignore the RIGHT-WALL */ nwords --; /* Loop over each word in the sentence (skipping LEFT-WALL, which is * word 0. */ for (w=1; w<nwords; w++) { Disjunct *disj = sent->parse_info->chosen_disjuncts[w]; /* disj is NULL if word did not participate in parse */ if (NULL == disj) { continue; } infword = disj->string; lifo->sense_list[w] = lg_corpus_senses(corp, infword, lifo->disjunct_list_str[w], w); } }
/** * lg_corpus_score -- compute parse-ranking score for sentence. * * Given a parsed sentence, this routine will compute a parse ranking * score, based on the probabilites of observing the indicated set of * disjuncts in the statistics database. * * The score is stored in the Linkage_info->corpus_cost struct member. * * The score is currently computed as the average -log_2 conditional * probability p(d|w) of observing disjunct 'd', given word 'w'. * Lower scores are better -- they indicate more likely parses. */ void lg_corpus_score(Sentence sent, Linkage_info *lifo) { const char *infword, *djstr; double tot_score = 0.0f; Corpus *corp = sent->dict->corpus; int nwords = sent->length; int w; /* No-op if the database is not open */ if (NULL == corp->dbconn) return; lg_compute_disjunct_strings(sent, lifo); /* Decrement nwords, so as to ignore the RIGHT-WALL */ nwords --; /* Loop over each word in the sentence (skipping LEFT-WALL, which is * word 0. */ for (w=1; w<nwords; w++) { Disjunct *disj = sent->parse_info->chosen_disjuncts[w]; /* disj is NULL if word did not participate in parse */ if (NULL == disj) { tot_score += LOW_SCORE; continue; } infword = disj->string; djstr = lifo->disjunct_list_str[w]; tot_score += get_disjunct_score(corp, infword, djstr); } /* Decrement nwords, so as to ignore the LEFT-WALL */ --nwords; tot_score /= nwords; lifo->corpus_cost = tot_score; }
double lg_corpus_disjunct_score(Linkage linkage, int w) { double score; const char *infword, *djstr; Sentence sent = linkage->sent; Linkage_info *lifo = linkage->info; Corpus *corp = sent->dict->corpus; Disjunct *disj; /* No-op if the database is not open */ if (NULL == corp->dbconn) return LOW_SCORE; /* disj is NULL if word did not participate in parse */ disj = sent->parse_info->chosen_disjuncts[w]; if (NULL == disj) return LOW_SCORE; lg_compute_disjunct_strings(sent, lifo); infword = disj->string; djstr = lifo->disjunct_list_str[w]; score = get_disjunct_score(corp, infword, djstr); return score; }