Skip to content

Commit

Permalink
elaborate the tGrammarUpdate mechanism to also support shadowing of s…
Browse files Browse the repository at this point in the history
…ettings that are not recorded as part of the main tGrammar object; furthermore, create a 'lexical parsing only' mode, i.e. skip the syntactic parsing phase (to then output the lexical chart, i.e. a lattice of lexical and non-syntactic phrasal items)---this is activated by an extension the 'tsdb_mode' option, which now has a bit-coded part

git-svn-id: https://pet.opendfki.de/repos/pet/main@780 4200e16c-5112-0410-ac55-d7fb557a720a
  • Loading branch information
oe committed Jul 31, 2011
1 parent 6b4222c commit a1bb24d
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 23 deletions.
22 changes: 18 additions & 4 deletions cheap/cheap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ void print_derivation_as(char format, tItem *item, ostream &out) {
switch (format) {
case 'd': { tCompactDerivationPrinter cdpr(out); cdpr.print(item); break; }
case 't': {
int protocol_version = get_opt_int("opt_tsdb");
if (protocol_version == 0)
protocol_version = 1;
int protocol_version = get_opt_int("opt_tsdb") & 31;
if (protocol_version == 0) protocol_version = 1;
tTSDBDerivationPrinter tdpr(out, protocol_version);
tdpr.print(item);
break;
Expand Down Expand Up @@ -696,9 +695,24 @@ static void init_main_options() {
* parsing mode.
*/
//@{
//
// _fix_me_
// as of mid-2011, we (re-)interpret this option as partially bit-coded: the
// lower five bits (up to a maximum value of 31) encode the protocal version
// used in communication with the [incr tsdb()] server (as it used to be),
// whereas higher bits are available to further customize cheap behavior in
// [incr tsdb()] mode. in this scheme, bit six (32) activates lexical-only
// parsing. while the general mechanism of bit-coded [incr tsdb()] options
// seems desirable, possibly we should consult with other PET developers to
// make lexical-only parsing a separate option. however, this mode needs to
// be toggled at run-time, i.e. under [incr tsdb()] control. hence, only a
// command-line option would be insufficient, though one could maybe build
// on the 'grammar update' mechanism, i.e. dynamic and temporary adjustments
// to a pre-defined sub-set of settings. (1-jul-11; oe)
//
managed_opt("opt_tsdb",
"enable [incr tsdb()] slave mode (protocol version = n)",
0);
1);
managed_opt("opt_server",
"go into server mode, bind to port `n' (default: 4711)",
0);
Expand Down
13 changes: 10 additions & 3 deletions cheap/grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,12 +1041,13 @@ tGrammar::clear_dynamic_stems()


tGrammarUpdate::tGrammarUpdate(tGrammar *grammar, std::string &input)
: _grammar(grammar), _original_roots(grammar->_root_insts)
: _grammar(grammar), _original_roots(grammar->_root_insts), _update(0)
{

if(!input.empty()) {
settings foo(input);
setting *set = foo.lookup("start-symbols");
_update = new settings(input);
cheap_settings->install(_update);
setting *set = _update->lookup("start-symbols");
if(set != 0) {
grammar->_root_insts = 0;
for(int i = set->n-1; i >= 0; --i) {
Expand All @@ -1065,11 +1066,17 @@ tGrammarUpdate::tGrammarUpdate(tGrammar *grammar, std::string &input)

} // tGrammarUpdate::tGrammarUpdate()


tGrammarUpdate::~tGrammarUpdate() {

if(_original_roots != NULL) {
free_list(_grammar->_root_insts);
_grammar->_root_insts = _original_roots;
} // if

if(_update != 0) {
cheap_settings->uninstall(_update);
delete _update;
} // if

} // tGrammarUpdate::~tGrammarUpdate()
3 changes: 2 additions & 1 deletion cheap/grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ class tGrammarUpdate {
private:
tGrammar *_grammar;
list_int *_original_roots;

settings *_update;

}; // class tGrammarUpdate

#endif
3 changes: 2 additions & 1 deletion cheap/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ char* parse_options(int argc, char* argv[])
if(optarg != NULL)
{
opt_tsdb = strtoint(optarg, "as argument to -tsdb");
if(opt_tsdb < 0 || opt_tsdb > 2)
int foo = opt_tsdb & 31;
if(foo < 1 || foo > 2)
{
LOG(logAppl, FATAL,
"parse_options(): invalid tsdb++ protocol version");
Expand Down
25 changes: 21 additions & 4 deletions cheap/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,26 @@ parse_finish(fs_alloc_state &FSAS, list<tError> &errors, clock_t timeout) {

LOG(logParse, DEBUG, *Chart);

Chart->readings() = collect_readings(FSAS, errors,
pedgelimit, memlimit, opt_nsolutions,
Chart->trees());
if(get_opt_int("opt_tsdb") & 32) {
//
// in lexical-only mode, the parser has halted after lexical parsing, and
// what remains to be done is a mimicry of finding actual results; in this
// mode, all edges that could have fed into syntactic rules count as valid
// results, i.e. in effect we output a lexical lattice. (2-jul-11; oe)
//
for(chart_iter item(Chart); item.valid(); ++item) {
if(passive_unblocked_non_input(item.current())
&& item.current()->inflrs_complete_p())
Chart->readings().push_back(item.current());
} // for
Chart->trees() = Chart->readings();
stats.trees = Chart->trees().size();
}
else
Chart->readings()
= collect_readings(FSAS, errors, pedgelimit, memlimit,
opt_nsolutions, Chart->trees());

stats.readings = Chart->readings().size();

}
Expand Down Expand Up @@ -698,7 +715,7 @@ analyze(string input, chart *&C, fs_alloc_state &FSAS

// during lexical processing, the appropriate tasks for the syntactic stage
// are already created
parse_loop(FSAS, errors, timeout);
if(!(get_opt_int("opt_tsdb") & 32)) parse_loop(FSAS, errors, timeout);

ParseTime.stop();
TotalParseTime.stop();
Expand Down
23 changes: 18 additions & 5 deletions cheap/tsdb++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,23 @@ initialize_version()

#ifdef TSDBAPI

static tGrammarUpdate *run_grammar_update = NULL;

int
cheap_create_test_run(const char *data, int run_id, const char *comment,
int interactive, int protocol_version,
char const *custom)
{
if(protocol_version > 0 && protocol_version <= 2)
set_opt("opt_tsdb", protocol_version);
int foo = protocol_version & 31;
if(foo >= 1 && foo <= 2) set_opt("opt_tsdb", protocol_version);

cheap_tsdb_summarize_run();
return 0;
if(custom != NULL) {
std::string foo(custom);
run_grammar_update = new tGrammarUpdate(Grammar, foo);
} // if

cheap_tsdb_summarize_run();
return 0;
}

void
Expand Down Expand Up @@ -368,6 +375,11 @@ cheap_complete_test_run(int run_id, const char *custom)
compute_qc_paths(qc, get_opt_int("opt_packing"));
}

if(run_grammar_update != NULL) {
delete run_grammar_update;
run_grammar_update = NULL;
} // if

return 0;
}

Expand Down Expand Up @@ -398,7 +410,7 @@ tsdb_result::capi_print()
if(scored)
capi_printf("(:score . %.4g) ", score);

if(get_opt_int("opt_tsdb") == 1)
if((get_opt_int("opt_tsdb") & 31) == 1)
{
capi_printf("(:derivation . \"%s\") ",
escape_string(derivation).c_str());
Expand Down Expand Up @@ -606,6 +618,7 @@ cheap_tsdb_summarize_item(chart &Chart, int length,
{
int tsdb_mode;
get_opt("opt_tsdb", tsdb_mode);
tsdb_mode &= 31;
if(get_opt_bool("opt_derivation"))
{
int nres = 0;
Expand Down
1 change: 1 addition & 0 deletions common/list-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <list>
#include <functional>
#include <cstddef>

/** Minimum overhead single linked lists of integers */
struct list_int {
Expand Down
46 changes: 41 additions & 5 deletions common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
using std::string;

settings::settings(string name, string base_dir, const char *message)
: _lloc(0), _li_cache() {
: _lloc(0), _li_cache(), _updates() {
_n = 0;
_set = new setting*[SET_TABLE_SIZE];

Expand All @@ -51,7 +51,7 @@ settings::settings(string name, string base_dir, const char *message)


settings::settings(const std::string &input)
: _lloc(0), _li_cache() {
: _lloc(0), _li_cache(), _updates() {
_n = 0;
_set = new setting*[SET_TABLE_SIZE];

Expand All @@ -77,23 +77,42 @@ settings::~settings() {
it != _li_cache.end(); ++it) {
free_list(it->second);
}

}

/** Do a linear search for \a name in the settings and return the first
* matching setting.
*/
setting *settings::lookup(const char *name) {
for(int i = 0; i < _n; ++i)

//
// first, in case there are settings updates (which should shadow any values
// in the top-level .settings. object), check these in order. thus, multiple
// updates can shadow each other, in the order determined by install() calls.
//
setting *match;
for(std::list<settings *>::iterator update = _updates.begin();
update != _updates.end();
++update) {
match = (*update)->lookup(name);
if(match != NULL) {
_lloc = match->loc;
return match;
} // if
} // for

for(int i = 0; i < _n; ++i) {
if(strcmp(_set[i]->name, name) == 0) {
if(i != 0) {
// put to front, so further lookup is faster
setting *tmp;
tmp = _set[i]; _set[i] = _set[0]; _set[0] = tmp;
}
} // if

_lloc = _set[0]->loc;
return _set[0];
}
} // if
} // for

_lloc = 0;
return 0;
Expand Down Expand Up @@ -324,3 +343,20 @@ void settings::parse() {

consume(1);
}


void
settings::install(settings *update) {

_updates.push_front(update);

} // settings::uninstall()

bool
settings::uninstall(settings *update) {

unsigned int n = _updates.size();
_updates.remove(update);
return n != _updates.size();

} // settings::uninstall()
16 changes: 16 additions & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "types.h"
#include <set>
#include <map>
#include <list>

#define SET_EXT ".set"
#define SET_TABLE_SIZE 1024
Expand Down Expand Up @@ -72,6 +73,12 @@ class settings

struct lex_location *lloc() { return _lloc; }

//
// to (temporarily) install or uninstall 'extensions' (acting as overlays)
//
void install(settings *);
bool uninstall(settings *);

private:
int _n;
setting **_set;
Expand All @@ -81,6 +88,15 @@ class settings
/** cache for settings converted to lists of integers (e.g. status values) */
std::map<std::string, struct list_int *> _li_cache;

//
// settings can be dynamically 'extended' (see tGrammarUpdate for details),
// where additional settings are read from a file (or [incr tsdb()]) and go
// into effect temporarily, returning to the original configuration once the
// update goes out of scope (typically, as controlled by [incr tsdb()]).
// note that memory ownership for these overlays is /not/ delegatd to the
// .settings. class, i.e. needs to be addressed elsewhere.
//
std::list<settings *> _updates;
void parse();
void parse_one();
};
Expand Down

0 comments on commit a1bb24d

Please sign in to comment.