patch for adjustable heuristics from Python

This commit is contained in:
kr.angelov
2013-06-26 07:36:03 +00:00
parent d94b6146f2
commit 3c2d1890d0
9 changed files with 48 additions and 183 deletions

View File

@@ -1638,7 +1638,8 @@ pgf_parsing_default_beam_size(PgfConcr* concr)
}
static PgfParsing*
pgf_new_parsing(PgfConcr* concr, GuPool* pool, GuPool* out_pool)
pgf_new_parsing(PgfConcr* concr, double heuristics,
GuPool* pool, GuPool* out_pool)
{
PgfParsing* ps = gu_new(PgfParsing, pool);
ps->concr = concr;
@@ -1654,7 +1655,7 @@ pgf_new_parsing(PgfConcr* concr, GuPool* pool, GuPool* out_pool)
ps->prod_full_count = 0;
#endif
ps->free_item = NULL;
ps->beam_size = pgf_parsing_default_beam_size(concr);
ps->beam_size = heuristics;
PgfExprMeta *expr_meta =
gu_new_variant(PGF_EXPR_META,
@@ -2214,6 +2215,7 @@ pgf_parse_print_chunks(PgfParseState* state)
// TODO: s/CId/Cat, add the cid to Cat, make Cat the key to CncCat
PgfParseState*
pgf_parser_init_state(PgfConcr* concr, PgfCId cat, size_t lin_idx,
double heuristics,
GuPool* pool, GuPool* out_pool)
{
PgfCncCat* cnccat =
@@ -2223,8 +2225,12 @@ pgf_parser_init_state(PgfConcr* concr, PgfCId cat, size_t lin_idx,
gu_assert(lin_idx < cnccat->n_lins);
if (heuristics < 0) {
heuristics = pgf_parsing_default_beam_size(concr);
}
PgfParsing* ps =
pgf_new_parsing(concr, pool, out_pool);
pgf_new_parsing(concr, heuristics, pool, out_pool);
PgfParseState* state =
pgf_new_parse_state(ps, NULL, NULL, pool);
@@ -2269,12 +2275,6 @@ pgf_parser_init_state(PgfConcr* concr, PgfCId cat, size_t lin_idx,
return state;
}
void
pgf_parser_set_beam_size(PgfParseState* state, double beam_size)
{
state->ps->beam_size = beam_size;
}
void
pgf_parser_add_literal(PgfConcr *concr, PgfCId cat,
PgfLiteralCallback* callback)

View File

@@ -34,6 +34,7 @@ typedef struct PgfParseState PgfParseState;
/// Begin parsing
PgfParseState*
pgf_parser_init_state(PgfConcr* concr, PgfCId cat, size_t lin_idx,
double heuristics,
GuPool* pool, GuPool* out_pool);
/**<
* @param parser The parser to use
@@ -69,9 +70,6 @@ pgf_parser_next_state(PgfParseState* prev, PgfToken tok);
GuEnum*
pgf_parser_completions(PgfParseState* prev, GuString prefix);
void
pgf_parser_set_beam_size(PgfParseState* state, double beam_size);
void
pgf_parser_add_literal(PgfConcr *concr, PgfCId cat,
PgfLiteralCallback* callback);

View File

@@ -157,7 +157,7 @@ pgf_parseval(PgfConcr* concr, PgfExpr expr, PgfCId cat,
PgfMetricsLznState state;
state.funcs = &pgf_metrics_lin_funcs1;
state.ps = pgf_parser_init_state(concr, cat, 0, pool, pool);
state.ps = pgf_parser_init_state(concr, cat, 0, -1, pool, pool);
state.marks = gu_new_buf(int, pool);
state.pos = 0;
state.phrases = gu_new_buf(PgfPhrase*, pool);

View File

@@ -210,10 +210,18 @@ pgf_linearize(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err)
GuEnum*
pgf_parse(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
GuPool* pool, GuPool* out_pool)
{
return pgf_parse_with_heuristics(concr, cat, lexer, -1.0, pool, out_pool);
}
GuEnum*
pgf_parse_with_heuristics(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
double heuristics,
GuPool* pool, GuPool* out_pool)
{
// Begin parsing a sentence of the specified category
PgfParseState* state =
pgf_parser_init_state(concr, cat, 0, pool, out_pool);
pgf_parser_init_state(concr, cat, 0, heuristics, pool, out_pool);
if (state == NULL) {
return NULL;
}
@@ -244,7 +252,7 @@ pgf_get_completions(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
{
// Begin parsing a sentence of the specified category
PgfParseState* state =
pgf_parser_init_state(concr, cat, 0, pool, pool);
pgf_parser_init_state(concr, cat, 0, -1, pool, pool);
if (state == NULL) {
return NULL;
}
@@ -268,31 +276,3 @@ pgf_get_completions(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
// Now begin enumerating the resulting syntax trees
return pgf_parser_completions(state, prefix);
}
void
pgf_print_chunks(PgfConcr* concr, PgfCId cat, PgfLexer *lexer, GuPool* pool)
{
// Begin parsing a sentence of the specified category
PgfParseState* state =
pgf_parser_init_state(concr, cat, 0, pool, pool);
if (state == NULL) {
printf("\n");
return;
}
// Tokenization
GuExn* lex_err = gu_new_exn(NULL, gu_kind(type), pool);
PgfToken tok = pgf_lexer_read_token(lexer, lex_err);
while (!gu_exn_is_raised(lex_err)) {
// feed the token to get a new parse state
state = pgf_parser_next_state(state, tok);
if (state == NULL) {
printf("\n");
return;
}
tok = pgf_lexer_read_token(lexer, lex_err);
}
pgf_parse_print_chunks(state);
}

View File

@@ -117,6 +117,11 @@ PgfExprEnum*
pgf_parse(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
GuPool* pool, GuPool* out_pool);
PgfExprEnum*
pgf_parse_with_heuristics(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
double heuristics,
GuPool* pool, GuPool* out_pool);
GuEnum*
pgf_get_completions(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
GuString prefix, GuPool* pool);
@@ -128,11 +133,6 @@ pgf_parseval(PgfConcr* concr, PgfExpr expr, PgfCId cat,
PgfExprEnum*
pgf_generate(PgfPGF* pgf, PgfCId cat, GuPool* pool);
// an experimental function. Please don't use it
void
pgf_print_chunks(PgfConcr* concr, PgfCId cat, PgfLexer *lexer, GuPool* pool);
/// @}
void