static void put_opts_in_local_vars(Command_Options* copts) { Parse_Options opts = copts->popts; local.verbosity = parse_options_get_verbosity(opts); local.debug = parse_options_get_debug(opts); local.test = parse_options_get_test(opts); local.timeout = parse_options_get_max_parse_time(opts);; local.memory = parse_options_get_max_memory(opts);; local.linkage_limit = parse_options_get_linkage_limit(opts); local.islands_ok = parse_options_get_islands_ok(opts); local.spell_guess = parse_options_get_spell_guess(opts); local.short_length = parse_options_get_short_length(opts); local.cost_model = parse_options_get_cost_model_type(opts); local.max_cost = parse_options_get_disjunct_cost(opts); local.use_cluster_disjuncts = parse_options_get_use_cluster_disjuncts(opts); local.use_sat_solver = parse_options_get_use_sat_parser(opts); local.use_viterbi = parse_options_get_use_viterbi(opts); local.screen_width = copts->screen_width; local.echo_on = copts->echo_on; local.batch_mode = copts->batch_mode; local.panic_mode = copts->panic_mode; local.allow_null = copts->allow_null; local.display_on = copts->display_on; local.display_walls = copts->display_walls; local.display_postscript = copts->display_postscript; local.display_constituents = copts->display_constituents; local.display_bad = copts->display_bad; local.display_disjuncts = copts->display_disjuncts; local.display_links = copts->display_links; local.display_senses = copts->display_senses; local.display_morphology = parse_options_get_display_morphology(opts); }
static void put_opts_in_local_vars(Parse_Options opts) { local.verbosity = parse_options_get_verbosity(opts); local.timeout = parse_options_get_max_parse_time(opts);; local.memory = parse_options_get_max_memory(opts);; local.linkage_limit = parse_options_get_linkage_limit(opts); local.null_block = parse_options_get_null_block(opts); local.islands_ok = parse_options_get_islands_ok(opts); local.spell_guess = parse_options_get_spell_guess(opts); local.short_length = parse_options_get_short_length(opts); local.cost_model = parse_options_get_cost_model_type(opts); local.max_cost = parse_options_get_disjunct_costf(opts); local.echo_on = parse_options_get_echo_on(opts); local.batch_mode = parse_options_get_batch_mode(opts); local.panic_mode = parse_options_get_panic_mode(opts); local.screen_width = parse_options_get_screen_width(opts); local.allow_null = parse_options_get_allow_null(opts); local.use_cluster_disjuncts = parse_options_get_use_cluster_disjuncts(opts); #ifdef USE_FAT_LINKAGES local.use_fat_links = parse_options_get_use_fat_links(opts); local.display_union = parse_options_get_display_union(opts); #endif /* USE_FAT_LINKAGES */ local.use_sat_solver = parse_options_get_use_sat_parser(opts); local.use_viterbi = parse_options_get_use_viterbi(opts); local.screen_width = parse_options_get_screen_width(opts); local.display_on = parse_options_get_display_on(opts); local.display_postscript = parse_options_get_display_postscript(opts); local.display_constituents = parse_options_get_display_constituents(opts); local.max_sentence_length = parse_options_get_max_sentence_length(opts); local.display_bad = parse_options_get_display_bad(opts); local.display_disjuncts = parse_options_get_display_disjuncts(opts); local.display_links = parse_options_get_display_links(opts); local.display_senses = parse_options_get_display_senses(opts); local.display_walls = parse_options_get_display_walls(opts); }
int main(int argc, char * argv[]) { FILE *input_fh = stdin; Dictionary dict; const char *language="en"; /* default to english, and not locale */ int num_linkages, i; Label label = NO_LABEL; const char *codeset; const char *locale = NULL; Command_Options *copts; Parse_Options opts; bool batch_in_progress = false; #if LATER /* Try to catch the SIGWINCH ... except this is not working. */ struct sigaction winch_act; winch_act.sa_handler = winch_handler; winch_act.sa_sigaction = NULL; sigemptyset (&winch_act.sa_mask); winch_act.sa_flags = 0; sigaction (SIGWINCH, &winch_act, NULL); #endif i = 1; if ((argc > 1) && (argv[1][0] != '-')) { /* the dictionary is the first argument if it doesn't begin with "-" */ language = argv[1]; i++; } #if !defined(_MSC_VER) && !defined(__MINGW32__) /* Get the locale from the environment... * Perhaps we should someday get it from the dictionary ?? */ locale = setlocale(LC_ALL, ""); /* Check to make sure the current locale is UTF8; if its not, * then force-set this to the english utf8 locale */ codeset = nl_langinfo(CODESET); if (!strstr(codeset, "UTF") && !strstr(codeset, "utf")) { fprintf(stderr, "%s: Warning: locale %s was not UTF-8; force-setting to en_US.UTF-8\n", argv[0], codeset); locale = setlocale(LC_CTYPE, "en_US.UTF-8"); } #else #pragma message("WARNING: Windows console (cmd.exe) does not support unicode input!\nWill attempt to convert from the native encoding!"); fprintf(stderr, "%s: Warning: Windows console (cmd.exe) does not support unicode\n" "input! Will attempt to convert from the native encoding!", argv[0]); #endif for (; i<argc; i++) { if (argv[i][0] == '-' && strcmp("--version", argv[i]) == 0) { printf("Version: %s\n", linkgrammar_get_version()); exit(0); } } copts = command_options_create(); opts = copts->popts; if (copts == NULL || opts == NULL || copts->panic_opts == NULL) { fprintf(stderr, "%s: Fatal error: unable to create parse options\n", argv[0]); exit(-1); } if (language && *language) dict = dictionary_create_lang(language); else dict = dictionary_create_default_lang(); if (dict == NULL) { fprintf(stderr, "%s: Fatal error: Unable to open dictionary.\n", argv[0]); exit(-1); } setup_panic_parse_options(copts->panic_opts); copts->panic_mode = true; parse_options_set_max_parse_time(opts, 30); parse_options_set_linkage_limit(opts, 1000); parse_options_set_min_null_count(opts, 0); parse_options_set_max_null_count(opts, 0); parse_options_set_short_length(opts, 16); /* The English and Russian dicts use a cost of 2.7, which allows * regexes with a fractional cost of less than 1 to be used with * rules that have a cost of 2.0. */ parse_options_set_disjunct_cost(opts, 2.7); /* Process the command line commands */ for (i = 1; i<argc; i++) { if (argv[i][0] == '-') { int rc; if (argv[i][1] == '!' || argv[i][1] == '-') rc = issue_special_command(argv[i]+2, copts, dict); else rc = issue_special_command(argv[i]+1, copts, dict); if (rc) print_usage(argv[0]); } } check_winsize(copts); #if !defined(_MSC_VER) && !defined(__MINGW32__) prt_error("Info: Using locale %s.", locale); #endif prt_error("Info: Dictionary version %s.", linkgrammar_get_dict_version(dict)); prt_error("Info: Library version %s. Enter \"!help\" for help.", linkgrammar_get_version()); /* Main input loop */ while (1) { char *input_string; Sentence sent = NULL; verbosity = parse_options_get_verbosity(opts); debug = parse_options_get_debug(opts); test = parse_options_get_test(opts); input_string = fget_input_string(input_fh, stdout, copts); check_winsize(copts); if (NULL == input_string) { if (input_fh == stdin) break; fclose (input_fh); input_fh = stdin; continue; } if ((strcmp(input_string, "!quit") == 0) || (strcmp(input_string, "!exit") == 0)) break; /* We have to handle the !file command inline; its too hairy * otherwise ... */ if (strncmp(input_string, "!file", 5) == 0) { char * filename = &input_string[6]; input_fh = fopen(filename, "r"); if (NULL == input_fh) { int perr = errno; fprintf(stderr, "Error: %s (%d) %s\n", filename, perr, strerror(perr)); input_fh = stdin; continue; } continue; } /* If the input string is just whitespace, then ignore it. */ if (strspn(input_string, " \t\v") == strlen(input_string)) continue; if (special_command(input_string, copts, dict)) continue; if (!copts->batch_mode) batch_in_progress = false; if ('\0' != test[0]) { /* In batch mode warn only once. * In auto-next-linkage mode don't warn at all. */ if (!batch_in_progress && (NULL == strstr(test, ",auto-next-linkage,"))) { fflush(stdout); /* Remind the developer this is a test mode. */ fprintf(stderr, "Warning: Tests enabled: %s\n", test); if (copts->batch_mode) batch_in_progress = true; } } if (copts->echo_on) { printf("%s\n", input_string); } if (copts->batch_mode) { label = strip_off_label(input_string); } #ifdef USE_VITERBI /* Compile-time optional, for now, since it don't work yet. */ if (parse_options_get_use_viterbi(opts)) { viterbi_parse(input_string, dict); } else #endif { sent = sentence_create(input_string, dict); /* First parse with cost 0 or 1 and no null links */ // parse_options_set_disjunct_cost(opts, 2.7); parse_options_set_min_null_count(opts, 0); parse_options_set_max_null_count(opts, 0); parse_options_reset_resources(opts); num_linkages = sentence_parse(sent, opts); /* num_linkages is negative only on a hard-error; * typically, due to a zero-length sentence. */ if (num_linkages < 0) { sentence_delete(sent); sent = NULL; continue; } #if 0 /* Try again, this time omitting the requirement for * definite articles, etc. This should allow for the parsing * of newspaper headlines and other clipped speech. * * XXX Unfortunately, this also allows for the parsing of * all sorts of ungrammatical sentences which should not * parse, and leads to bad parses of many other unparsable * but otherwise grammatical sentences. Thus, this trick * pretty much fails; we leave it here to document the * experiment. */ if (num_linkages == 0) { parse_options_set_disjunct_cost(opts, 4.5); num_linkages = sentence_parse(sent, opts); if (num_linkages < 0) continue; } #endif /* Try using a larger list of disjuncts */ /* XXX fixme: the lg_expand_disjunct_list() routine is not * currently a part of the public API; it should be made so, * or this expansion idea should be abandoned... not sure which. */ if ((num_linkages == 0) && parse_options_get_use_cluster_disjuncts(opts)) { int expanded; if (verbosity > 0) fprintf(stdout, "No standard linkages, expanding disjunct set.\n"); parse_options_set_disjunct_cost(opts, 3.9); expanded = lg_expand_disjunct_list(sent); if (expanded) { num_linkages = sentence_parse(sent, opts); } if (0 < num_linkages) printf("Got One !!!!!!!!!!!!!!!!!\n"); } /* If asked to show bad linkages, then show them. */ if ((num_linkages == 0) && (!copts->batch_mode)) { if (copts->display_bad) { num_linkages = sentence_num_linkages_found(sent); } } /* Now parse with null links */ if (num_linkages == 0 && !copts->batch_mode) { if (verbosity > 0) fprintf(stdout, "No complete linkages found.\n"); if (copts->allow_null) { /* XXX should use expanded disjunct list here too */ parse_options_set_min_null_count(opts, 1); parse_options_set_max_null_count(opts, sentence_length(sent)); num_linkages = sentence_parse(sent, opts); } } if (verbosity > 0) { if (parse_options_timer_expired(opts)) fprintf(stdout, "Timer is expired!\n"); if (parse_options_memory_exhausted(opts)) fprintf(stdout, "Memory is exhausted!\n"); } if ((num_linkages == 0) && copts->panic_mode && parse_options_resources_exhausted(opts)) { /* print_total_time(opts); */ batch_errors++; if (verbosity > 0) fprintf(stdout, "Entering \"panic\" mode...\n"); parse_options_reset_resources(copts->panic_opts); parse_options_set_verbosity(copts->panic_opts, verbosity); num_linkages = sentence_parse(sent, copts->panic_opts); if (verbosity > 0) { if (parse_options_timer_expired(copts->panic_opts)) fprintf(stdout, "Panic timer is expired!\n"); } } /* print_total_time(opts); */ if (copts->batch_mode) { batch_process_some_linkages(label, sent, copts); } else { int c = process_some_linkages(sent, copts); if (c == EOF) { sentence_delete(sent); sent = NULL; break; } } fflush(stdout); sentence_delete(sent); sent = NULL; } } if (copts->batch_mode) { /* print_time(opts, "Total"); */ fprintf(stderr, "%d error%s.\n", batch_errors, (batch_errors==1) ? "" : "s"); } /* Free stuff, so that mem-leak detectors don't commplain. */ command_options_delete(copts); dictionary_delete(dict); fget_input_string(NULL, NULL, NULL); printf ("Bye.\n"); return 0; }