From ce0200aa6be6b3b049400d368c1a03687249825b Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Thu, 23 Feb 2023 21:46:45 -0600 Subject: [PATCH] Attempt to issue linakges in sorted order. Something like this could almost work, if we had left and right cumulative costs. See issue #1451 --- link-grammar/parse/extract-links.c | 27 +++++++++++++++++++++++++++ link-grammar/parse/parse.c | 3 +++ 2 files changed, 30 insertions(+) diff --git a/link-grammar/parse/extract-links.c b/link-grammar/parse/extract-links.c index 4eb9266ce..8db20ca18 100644 --- a/link-grammar/parse/extract-links.c +++ b/link-grammar/parse/extract-links.c @@ -122,9 +122,18 @@ static void record_choice( { Parse_choice *pc = make_choice(lset, lrc, rset, rlc, md, pex); +// Place in sorted order. +if (s->first) { + Parse_choice *last = s->first; + while (last->next && pc->md->cost > last->md->cost) last=last->next; + pc->next = last->next; + last->next = pc; +printf("duuuude chained last ccost=%f nextcost=%f\n", pc->md->cost, last->md->cost); +} else { // Chain it into the parse set. pc->next = s->first; s->first = pc; +} s->num_pc++; } @@ -838,8 +847,26 @@ static void list_links(Linkage lkg, Parse_set * set, int index) } assert(pc != NULL, "walked off the end in list_links"); issue_links_for_choice(lkg, pc, set); + +if (NULL == pc->set[0]->first) { + list_links(lkg, pc->set[1], index); + return; +} + +if (NULL == pc->set[1]->first) { + list_links(lkg, pc->set[0], index); + return; +} + +float lcost = pc->set[0]->first->md->cost; +float rcost = pc->set[1]->first->md->cost; +if (lcost < rcost) { list_links(lkg, pc->set[0], index % pc->set[0]->count); list_links(lkg, pc->set[1], index / pc->set[0]->count); +} else { + list_links(lkg, pc->set[0], index / pc->set[1]->count); + list_links(lkg, pc->set[1], index % pc->set[1]->count); +} } static void list_random_links(Linkage lkg, unsigned int *rand_state, diff --git a/link-grammar/parse/parse.c b/link-grammar/parse/parse.c index df0c0250a..31a3d7851 100644 --- a/link-grammar/parse/parse.c +++ b/link-grammar/parse/parse.c @@ -519,6 +519,9 @@ static void sort_linkages(Sentence sent, Parse_Options opts) for (uint32_t i=0; inum_linkages_alloced; i++) sent->lnkages[i].dupe = false; +for (uint32_t i=0; inum_linkages_alloced; i++) +printf("duuude i=%u cost=%f\n", i, sent->lnkages[i].lifo.disjunct_cost); + /* Sorting will also mark some of them as being duplicates */ qsort((void *)sent->lnkages, sent->num_linkages_alloced, sizeof(struct Linkage_s),