1 /*************************************************************************/ 2 /* Copyright (c) 2004 */ 3 /* Daniel Sleator, David Temperley, and John Lafferty */ 4 /* Copyright 2013 Linas Vepstas */ 5 /* All rights reserved */ 6 /* */ 7 /* Use of the link grammar parsing system is subject to the terms of the */ 8 /* license set forth in the LICENSE file included with this software. */ 9 /* This license allows free redistribution and use in source and binary */ 10 /* forms, with or without modification, subject to certain conditions. */ 11 /* */ 12 /*************************************************************************/ 13 #ifndef _LINKINCLUDESH_ 14 #define _LINKINCLUDESH_ 15 16 #ifndef SWIG 17 #include <stdbool.h> /* Needed for bool typedef */ 18 #include <stdio.h> /* Needed for FILE* below */ 19 #include <link-grammar/link-features.h> 20 21 LINK_BEGIN_DECLS 22 23 #ifndef __bool_true_false_are_defined 24 #ifdef _Bool 25 #define bool _Bool 26 #else 27 #define bool int 28 #endif 29 #define true 1 30 #define false 0 31 #define __bool_true_false_are_defined 1 32 #endif 33 #endif /* !SWIG */ 34 35 /********************************************************************** 36 * 37 * System initialization 38 * 39 ***********************************************************************/ 40 41 typedef struct Dictionary_s * Dictionary; 42 43 link_public_api(const char *) 44 linkgrammar_get_version(void); 45 46 link_public_api(const char *) 47 linkgrammar_get_dict_version(Dictionary); 48 49 link_public_api(const char *) 50 linkgrammar_get_configuration(void); 51 52 link_public_api(const char *) 53 linkgrammar_get_dict_locale(Dictionary); 54 55 /********************************************************************** 56 * 57 * Functions and definitions for the error handler. 58 * 59 ***********************************************************************/ 60 typedef enum 61 { 62 lg_Fatal = 1, 63 lg_Error, 64 lg_Warn, 65 lg_Info, 66 lg_Debug, 67 lg_Trace, 68 lg_None 69 } lg_error_severity; 70 71 /* Raw error message. */ 72 typedef struct 73 { 74 /* err_ctxt ec; */ 75 lg_error_severity severity; 76 const char *severity_label; 77 const char *text; 78 } lg_errinfo; 79 80 /* Error handler callback function. */ 81 typedef void (*lg_error_handler)(lg_errinfo *, void *); 82 83 link_public_api(lg_error_handler) 84 lg_error_set_handler(lg_error_handler, void *data); 85 link_public_api(const void *) 86 lg_error_set_handler_data(void * data); 87 link_public_api(char *) 88 lg_error_formatmsg(lg_errinfo *lge); 89 link_public_api(int) 90 lg_error_printall(lg_error_handler, void *data); 91 link_public_api(int) 92 lg_error_clearall(void); 93 link_public_api(bool) 94 lg_error_flush(void); 95 96 /********************************************************************** 97 * 98 * Functions to manipulate Dictionaries 99 * 100 ***********************************************************************/ 101 102 link_public_api(Dictionary) 103 dictionary_create_lang(const char * lang); 104 link_public_api(Dictionary) 105 dictionary_create_default_lang(void); 106 link_public_api(const char *) 107 dictionary_get_lang(Dictionary); 108 109 link_public_api(void) 110 dictionary_delete(Dictionary); 111 112 link_public_api(void) 113 dictionary_set_data_dir(const char * path); 114 link_public_api(char *) 115 dictionary_get_data_dir(void); 116 link_public_api(FILE *) 117 linkgrammar_open_data_file(const char *); 118 119 /********************************************************************** 120 * 121 * Functions to manipulate Parse Options 122 * 123 ***********************************************************************/ 124 125 typedef enum 126 { 127 VDAL=1, /* Sort by Violations, Disjunct cost, Link cost */ 128 } Cost_Model_type; 129 130 typedef struct Parse_Options_s * Parse_Options; 131 132 link_public_api(Parse_Options) 133 parse_options_create(void); 134 link_public_api(int) 135 parse_options_delete(Parse_Options opts); 136 link_public_api(void) 137 parse_options_set_verbosity(Parse_Options opts, int verbosity); 138 link_public_api(int) 139 parse_options_get_verbosity(Parse_Options opts); 140 link_public_api(void) 141 parse_options_set_debug(Parse_Options opts, const char * debug); 142 link_public_api(char *) 143 parse_options_get_debug(Parse_Options opts); 144 link_public_api(void) 145 parse_options_set_test(Parse_Options opts, const char * test); 146 link_public_api(char *) 147 parse_options_get_test(Parse_Options opts); 148 link_public_api(void) 149 parse_options_set_linkage_limit(Parse_Options opts, int linkage_limit); 150 link_public_api(int) 151 parse_options_get_linkage_limit(Parse_Options opts); 152 link_public_api(void) 153 parse_options_set_disjunct_cost(Parse_Options opts, double disjunct_cost); 154 link_public_api(double) 155 parse_options_get_disjunct_cost(Parse_Options opts); 156 link_public_api(void) 157 parse_options_set_min_null_count(Parse_Options opts, int null_count); 158 link_public_api(int) 159 parse_options_get_min_null_count(Parse_Options opts); 160 link_public_api(void) 161 parse_options_set_max_null_count(Parse_Options opts, int null_count); 162 link_public_api(int) 163 parse_options_get_max_null_count(Parse_Options opts); 164 link_public_api(void) 165 parse_options_set_islands_ok(Parse_Options opts, bool islands_ok); 166 link_public_api(bool) 167 parse_options_get_islands_ok(Parse_Options opts); 168 link_public_api(void) 169 parse_options_set_spell_guess(Parse_Options opts, int spell_guess); 170 link_public_api(int) 171 parse_options_get_spell_guess(Parse_Options opts); 172 link_public_api(void) 173 parse_options_set_short_length(Parse_Options opts, int short_length); 174 link_public_api(int) 175 parse_options_get_short_length(Parse_Options opts); 176 link_public_api(void) 177 parse_options_set_max_memory(Parse_Options opts, int mem); 178 link_public_api(int) 179 parse_options_get_max_memory(Parse_Options opts); 180 link_public_api(void) 181 parse_options_set_max_parse_time(Parse_Options opts, int secs); 182 link_public_api(int) 183 parse_options_get_max_parse_time(Parse_Options opts); 184 link_public_api(void) 185 parse_options_set_cost_model_type(Parse_Options opts, Cost_Model_type cm); 186 link_public_api(Cost_Model_type) 187 parse_options_get_cost_model_type(Parse_Options opts); 188 link_public_api(void) 189 parse_options_set_perform_pp_prune(Parse_Options opts, bool pp_prune); 190 link_public_api(bool) 191 parse_options_get_perform_pp_prune(Parse_Options opts); 192 link_public_api(void) 193 parse_options_set_use_sat_parser(Parse_Options opts, bool use_sat_solver); 194 link_public_api(bool) 195 parse_options_get_use_sat_parser(Parse_Options opts); 196 link_public_api(bool) 197 parse_options_timer_expired(Parse_Options opts); 198 link_public_api(bool) 199 parse_options_memory_exhausted(Parse_Options opts); 200 link_public_api(bool) 201 parse_options_resources_exhausted(Parse_Options opts); 202 link_public_api(void) 203 parse_options_set_all_short_connectors(Parse_Options opts, bool val); 204 link_public_api(bool) 205 parse_options_get_all_short_connectors(Parse_Options opts); 206 link_public_api(void) 207 parse_options_set_repeatable_rand(Parse_Options opts, bool val); 208 link_public_api(bool) 209 parse_options_get_repeatable_rand(Parse_Options opts); 210 link_public_api(void) 211 parse_options_reset_resources(Parse_Options opts); 212 213 link_public_api(char *) 214 parse_options_get_dialect(Parse_Options opts); 215 link_public_api(void) 216 parse_options_set_dialect(Parse_Options opts, const char *dialect); 217 218 /********************************************************************** 219 * 220 * The following Parse_Options functions do not directly affect the 221 * operation of the parser, but they can be useful for organizing the 222 * search, or displaying the results. They were included as switches for 223 * convenience in implementing the "standard" version of the link parser 224 * using the API. 225 * 226 ***********************************************************************/ 227 228 typedef enum 229 { 230 NO_DISPLAY = 0, /** Display is disabled */ 231 MULTILINE = 1, /** multi-line, indented display */ 232 BRACKET_TREE = 2, /** single-line, bracketed tree */ 233 SINGLE_LINE = 3, /** single line, round parenthesis */ 234 MAX_STYLES = 3 /* this must always be last, largest */ 235 } ConstituentDisplayStyle; 236 237 238 link_public_api(void) 239 parse_options_set_display_morphology(Parse_Options opts, int val); 240 link_public_api(int) 241 parse_options_get_display_morphology(Parse_Options opts); 242 243 /********************************************************************** 244 * 245 * Functions to manipulate Sentences 246 * 247 ***********************************************************************/ 248 249 typedef struct Sentence_s * Sentence; 250 typedef size_t LinkageIdx; 251 252 link_public_api(Sentence) 253 sentence_create(const char *input_string, Dictionary dict); 254 link_public_api(void) 255 sentence_delete(Sentence sent); 256 link_public_api(int) 257 sentence_split(Sentence sent, Parse_Options opts); 258 link_public_api(int) 259 sentence_parse(Sentence sent, Parse_Options opts); 260 link_public_api(int) 261 sentence_length(Sentence sent); 262 link_public_api(int) 263 sentence_null_count(Sentence sent); 264 link_public_api(int) 265 sentence_num_linkages_found(Sentence sent); 266 link_public_api(int) 267 sentence_num_valid_linkages(Sentence sent); 268 link_public_api(int) 269 sentence_num_linkages_post_processed(Sentence sent); 270 link_public_api(int) 271 sentence_num_violations(Sentence sent, LinkageIdx linkage_num); 272 link_public_api(double) 273 sentence_disjunct_cost(Sentence sent, LinkageIdx linkage_num); 274 link_public_api(int) 275 sentence_link_cost(Sentence sent, LinkageIdx linkage_num); 276 link_public_api(bool) 277 sentence_display_wordgraph(Sentence sent, const char *modestr); 278 279 /********************************************************************** 280 * 281 * Functions that create and manipulate Linkages. 282 * When a Linkage is requested, the user is given a 283 * copy of all of the necessary information, and is responsible 284 * for freeing up the storage when he/she is finished, using 285 * the routines provided below. 286 * 287 ***********************************************************************/ 288 289 typedef struct Linkage_s * Linkage; 290 typedef size_t WordIdx; 291 typedef size_t LinkIdx; 292 293 link_public_api(Linkage) 294 linkage_create(LinkageIdx linkage_num, Sentence sent, Parse_Options opts); 295 link_public_api(void) 296 linkage_delete(Linkage linkage); 297 link_public_api(size_t) 298 linkage_get_num_words(const Linkage linkage); 299 link_public_api(size_t) 300 linkage_get_num_links(const Linkage linkage); 301 link_public_api(WordIdx) 302 linkage_get_link_lword(const Linkage linkage, LinkIdx index); 303 link_public_api(WordIdx) 304 linkage_get_link_rword(const Linkage linkage, LinkIdx index); 305 link_public_api(int) 306 linkage_get_link_length(const Linkage linkage, LinkIdx index); 307 link_public_api(const char *) 308 linkage_get_link_label(const Linkage linkage, LinkIdx index); 309 link_public_api(const char *) 310 linkage_get_link_llabel(const Linkage linkage, LinkIdx index); 311 link_public_api(const char *) 312 linkage_get_link_rlabel(const Linkage linkage, LinkIdx index); 313 link_public_api(int) 314 linkage_get_link_num_domains(const Linkage linkage, LinkIdx index); 315 link_public_api(const char **) 316 linkage_get_link_domain_names(const Linkage linkage, LinkIdx index); 317 link_public_api(const char **) 318 linkage_get_words(const Linkage linkage); 319 link_public_api(const char *) 320 linkage_get_disjunct_str(const Linkage linkage, WordIdx word_num); 321 link_public_api(double) 322 linkage_get_disjunct_cost(const Linkage linkage, WordIdx word_num); 323 link_public_api(const char *) 324 linkage_get_word(const Linkage linkage, WordIdx word_num); 325 link_public_api(char *) 326 linkage_print_constituent_tree(Linkage linkage, ConstituentDisplayStyle mode); 327 link_public_api(void) 328 linkage_free_constituent_tree_str(char *str); 329 link_public_api(char *) 330 linkage_print_diagram(const Linkage linkage, bool display_walls, size_t screen_width); 331 link_public_api(void) 332 linkage_free_diagram(char * str); 333 link_public_api(char *) 334 linkage_print_postscript(const Linkage linkage, bool display_walls, bool print_ps_header); 335 link_public_api(void) 336 linkage_free_postscript(char * str); 337 link_public_api(char *) 338 linkage_print_disjuncts(const Linkage linkage); 339 link_public_api(void) 340 linkage_free_disjuncts(char *str); 341 link_public_api(char *) 342 linkage_print_links_and_domains(const Linkage linkage); 343 link_public_api(void) 344 linkage_free_links_and_domains(char *str); 345 link_public_api(char *) 346 linkage_print_pp_msgs(Linkage linkage); 347 link_public_api(void) 348 linkage_free_pp_msgs(char * str); 349 link_public_api(int) 350 linkage_unused_word_cost(const Linkage linkage); 351 link_public_api(double) 352 linkage_disjunct_cost(const Linkage linkage); 353 link_public_api(int) 354 linkage_link_cost(const Linkage linkage); 355 link_public_api(const char *) 356 linkage_get_violation_name(const Linkage linkage); 357 358 /* Experimental API - subject to changes. */ 359 link_public_api(WordIdx) 360 linkage_get_word_byte_start(const Linkage linkage, WordIdx w); 361 link_public_api(WordIdx) 362 linkage_get_word_byte_end(const Linkage linkage, WordIdx w); 363 link_public_api(WordIdx) 364 linkage_get_word_char_start(const Linkage linkage, WordIdx w); 365 link_public_api(WordIdx) 366 linkage_get_word_char_end(const Linkage linkage, WordIdx w); 367 368 /********************************************************************** 369 * 370 * Internal functions -- do not use these in new code! 371 * They are used in the link-parser code (which needs their signature to 372 * compile), but are not intended for general public use. 373 * 374 ***********************************************************************/ 375 376 link_public_api(size_t) 377 utf8_strwidth(const char *); 378 379 link_public_api(char *) 380 dict_display_word_expr(Dictionary dict, const char *, Parse_Options opts); 381 link_public_api(char *) 382 dict_display_word_info(Dictionary dict, const char *, Parse_Options opts); 383 384 /********************************************************************** 385 * 386 * Internal functions -- do not use these in new code! 387 * These are not intended for general public use, but are required to 388 * work around certain Microsoft Windows linking oddities 389 * (specifically, to be callable from the JNI bindings library.) 390 * 391 ***********************************************************************/ 392 393 link_public_api(void) 394 parse_options_print_total_time(Parse_Options opts); 395 396 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) 397 #define GNUC_PRINTF( format_idx, arg_idx ) \ 398 __attribute__((__format__ (__printf__, format_idx, arg_idx))) 399 #else 400 #define GNUC_PRINTF( format_idx, arg_idx ) 401 #endif 402 403 link_public_api(int) 404 prt_error(const char *fmt, ...) GNUC_PRINTF(1,2); 405 406 /******************************************************* 407 * 408 * Macros for marking obsolete functions -- but there aren't any, 409 * just right now. 410 * 411 ********************************************************/ 412 413 #if __GNUC__ > 2 414 #define GNUC_DEPRECATED __attribute__((deprecated)) 415 #else 416 #define GNUC_DEPRECATED 417 #endif 418 419 #if defined(_MSC_VER) 420 #define MS_DEPRECATED __declspec(deprecated) 421 #else 422 #define MS_DEPRECATED 423 #endif 424 425 #ifndef SWIG 426 LINK_END_DECLS 427 #endif /* !SWIG */ 428 429 #endif 430