Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion link-grammar/dict-common/dict-structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef enum
} Exp_type;

#ifndef SWIG
#define COST_MAX_DEC_PLACES 3 /* Max. decimal places when printing. */
#define COST_MAX_DEC_PLACES 5 /* Max. decimal places when printing. */
static const float cost_epsilon = 1E-5f;

#define EXPTAG_SZ 100 /* Initial size for the Exptag array. */
Expand Down
55 changes: 55 additions & 0 deletions link-grammar/linkage/score.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <stdarg.h>
#include "api-structures.h" // Needed for Parse_Options
#include "connectors.h"
#include "disjunct-utils.h" // Needed for Disjunct
#include "linkage.h"
#include "score.h"
Expand Down Expand Up @@ -68,10 +69,64 @@ static float compute_disjunct_cost(Linkage lkg)
return lcost;
}

/// Compute the extra cost of a multi-connector being used multiple
/// times. Any cost on that multi-connector needs to be added for each
/// usage of it.
static float compute_multi_cost(Linkage lkg)
{
size_t lword = 0;
float mcost = 0.0;
for (size_t i = 0; i < lkg->num_words; i++)
{
Disjunct * dj = lkg->chosen_disjuncts[i];
if (NULL == dj) continue;

// Look at the right-going connectors.
for (Connector* rcon = dj->right; rcon; rcon=rcon->next)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the left connectors are ignored?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this was a prototype and I did not do those.

{
// No-op if not a multi-connector.
if (false == rcon->multi) continue;

// If the multi-connector does not have a cost, then skip.
// XXX at this time, there is no cncost in the condesc.
// if (0.0 == rcon->desc->cncost) continue;

// Initial offset; this will be immediately cancelled
// by the first use.
// mcost -= rcon->desc->cncost;
mcost -= 1.0;

// Find the links using this connector.
for (size_t l = 0; l < lkg->num_links; l++)
{
// The link is not even for this word. Ignore it.
if (lword != lkg->link_array[l].lw) continue;

// Find the links that are using our multi-connector.
for (Connector* wrc = lkg->link_array[l].rc; wrc; wrc=wrc->next)
{
// Skip if no match.
if (strcmp(rcon->desc->string, wrc->desc->string)) continue;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I wrote in the general response (in the wrong box...), there is 1-1 match between condesct_t elements and connector strings, disregarding if the connectors are multi or not. Thus the condesc_t strings don't include@.
So there is a need for a different comparison.


// Increment for each use.
// mcost += rcon->desc->cncost;
mcost += 1.0;
}
}
}
lword++; // increment only if disjunct is non-null.
}
mcost *= -0.00001;
return mcost;
}

/** Assign parse score (cost) to linkage, used for parse ranking. */
void linkage_score(Linkage lkg, Parse_Options opts)
{

lkg->lifo.unused_word_cost = unused_word_cost(lkg);
lkg->lifo.disjunct_cost = compute_disjunct_cost(lkg);
lkg->lifo.link_cost = compute_link_cost(lkg);

lkg->lifo.disjunct_cost += compute_multi_cost(lkg);
}
2 changes: 1 addition & 1 deletion link-parser/link-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ static const char *process_some_linkages(FILE *in, Sentence sent,
fprintf(stdout, "\tLinkage %d, ", num_displayed+1);
}

fprintf(stdout, "cost vector = (UNUSED=%d DIS=%5.2f LEN=%d)\n",
fprintf(stdout, "cost vector = (UNUSED=%d DIS=%5.5f LEN=%d)\n",
linkage_unused_word_cost(linkage),
linkage_disjunct_cost(linkage),
linkage_link_cost(linkage));
Expand Down