| 1 |
/* This file is part of GNU Dico. |
| 2 |
Copyright (C) 2012-2024 Sergey Poznyakoff |
| 3 |
|
| 4 |
GNU Dico is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 3, or (at your option) |
| 7 |
any later version. |
| 8 |
|
| 9 |
GNU Dico is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#include <config.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include <sys/stat.h> |
| 21 |
#include <sys/wait.h> |
| 22 |
#include <dico.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <appi18n.h> |
| 25 |
#include <wn.h> |
| 26 |
|
| 27 |
#define WNDB_MERGE_DEFS 0x01 |
| 28 |
|
| 29 |
struct wndb { |
| 30 |
char *dbname; |
| 31 |
int flags; |
| 32 |
int pos; |
| 33 |
int optc; |
| 34 |
struct wn_option **optv; |
| 35 |
}; |
| 36 |
|
| 37 |
static int |
| 38 |
_dico_display_message(char *msg) |
| 39 |
{ |
| 40 |
dico_log(L_ERR, 0, "WordNet: %s", msg); |
| 41 |
return -1; |
| 42 |
} |
| 43 |
|
| 44 |
static int |
| 45 |
wn_free_db(dico_handle_t hp) |
| 46 |
{ |
| 47 |
struct wndb *wndb = (struct wndb *)hp; |
| 48 |
free(wndb->dbname); |
| 49 |
free(wndb->optv); |
| 50 |
free(wndb); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
static void wn_register_strategies(void); |
| 55 |
static void wn_free_result(dico_result_t rp); |
| 56 |
|
| 57 |
static int |
| 58 |
wn_init(int argc, char **argv) |
| 59 |
{ |
| 60 |
char *wnsearchdir = NULL; |
| 61 |
char *wnhome = NULL; |
| 62 |
|
| 63 |
struct dico_option init_option[] = { |
| 64 |
{ DICO_OPTSTR(wnsearchdir), dico_opt_string, &wnsearchdir }, |
| 65 |
{ DICO_OPTSTR(wnhome), dico_opt_string, &wnhome }, |
| 66 |
{ NULL } |
| 67 |
}; |
| 68 |
|
| 69 |
if (dico_parseopt(init_option, argc, argv, 0, NULL)) |
| 70 |
return 1; |
| 71 |
|
| 72 |
display_message = _dico_display_message; |
| 73 |
/* FIXME: Control lengths! libWN does not bother to check boundaries */ |
| 74 |
if (wnhome) |
| 75 |
setenv("WNHOME", wnhome, 1); |
| 76 |
if (wnsearchdir) |
| 77 |
setenv("WNSEARCHDIR", wnsearchdir, 1); |
| 78 |
if (wninit()) { |
| 79 |
dico_log(L_ERR, 0, _("cannot open wordnet database")); |
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
wn_register_strategies(); |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
static const char *pos_choice[] = { |
| 89 |
"all", |
| 90 |
"noun", |
| 91 |
"verb", |
| 92 |
"adj", |
| 93 |
"adjective", |
| 94 |
"adv", |
| 95 |
"adverb", |
| 96 |
"satellite", |
| 97 |
"adjsat", |
| 98 |
NULL |
| 99 |
}; |
| 100 |
static int pos_trans[] = { |
| 101 |
ALL_POS, |
| 102 |
NOUN, |
| 103 |
VERB, |
| 104 |
ADJ, |
| 105 |
ADJ, |
| 106 |
ADV, |
| 107 |
ADV, |
| 108 |
ADJSAT, |
| 109 |
ADJSAT |
| 110 |
}; |
| 111 |
|
| 112 |
/* Forward declarations */ |
| 113 |
struct result; |
| 114 |
static void _wn_print_overview(struct wn_option *, SynsetPtr, |
| 115 |
struct result *res, dico_stream_t); |
| 116 |
static void _wn_print_definition(struct wn_option *opt, SynsetPtr sp, |
| 117 |
struct result *res, dico_stream_t str); |
| 118 |
static void _wn_print_hypernym(struct wn_option *, SynsetPtr, |
| 119 |
struct result *res, dico_stream_t); |
| 120 |
|
| 121 |
struct wn_option { |
| 122 |
char *option; /* user's search request */ |
| 123 |
int search; /* search to pass findtheinfo() */ |
| 124 |
int posmask; /* for what parts of speech it is defined */ |
| 125 |
char *label; /* text for search header message */ |
| 126 |
void (*tracer) (SynsetPtr); |
| 127 |
void (*printer) (struct wn_option *, SynsetPtr, struct result *, |
| 128 |
dico_stream_t); |
| 129 |
}; |
| 130 |
|
| 131 |
#define POS_MASK(n) bit(n) |
| 132 |
#define PM_ALL 0xffffffff |
| 133 |
|
| 134 |
static struct wn_option wn_optlist[] = { |
| 135 |
{ "hypernym", HYPERPTR, POS_MASK(NOUN)|POS_MASK(VERB), |
| 136 |
"Synonyms/Hypernyms (Ordered by Estimated Frequency)", |
| 137 |
NULL, _wn_print_hypernym }, |
| 138 |
{ "synonym", }, |
| 139 |
#if 0 |
| 140 |
/* These are some thoughts only */ |
| 141 |
{ "similar", SIMPTR, POS_MASK(ADJ), "Similarity" }, |
| 142 |
{ "synonym", SYNS, POS_MASK(ADV), "Synonyms" }, |
| 143 |
{ "antonym", ANTPTR, PM_ALL, "Antonyms" }, |
| 144 |
{ "pertainym", PERTPTR, POS_MASK(ADV)|POS_MASK(ADV), "Pertainyms" }, |
| 145 |
{ "attribute", ATTRIBUTE, POS_MASK(NOUN)|POS_MASK(ADJ), "Attributes" }, |
| 146 |
{ "domain", CLASSIFICATION, PM_ALL, "Domain" }, |
| 147 |
{ "class", CLASS, PM_ALL, "Domain Terms" }, |
| 148 |
{ "familarity", FREQ, PM_ALL, "Familiarity" }, |
| 149 |
{ "hypernym", HYPERPTR, POS_MASK(NOUN)|POS_MASK(VERB), |
| 150 |
"Synonyms/Hypernyms (Ordered by Estimated Frequency)" }, |
| 151 |
{ "hypernym/rec", -HYPERPTR, POS_MASK(NOUN)|POS_MASK(VERB), |
| 152 |
"Synonyms/Hypernyms (Ordered by Estimated Frequency)" }, |
| 153 |
{ "hyponym", HYPOPTR, POS_MASK(NOUN)|POS_MASK(VERB), "Hyponyms" }, |
| 154 |
{ "hyponym/rec", -HYPOPTR, POS_MASK(NOUN), "Hyponyms" }, |
| 155 |
{ "holonym", HOLONYM, POS_MASK(NOUN), "Holonyms" }, |
| 156 |
{ "holonym/rec", -HHOLONYM, POS_MASK(NOUN), "Holonyms" }, |
| 157 |
{ "holonym/part", ISPARTPTR, POS_MASK(NOUN), "Part Holonyms" }, |
| 158 |
{ "holonym/member", ISMEMBERPTR, POS_MASK(NOUN), "Member Holonyms" }, |
| 159 |
{ "holonym/substance", ISSTUFFPTR, POS_MASK(NOUN), "Substance Holonyms" }, |
| 160 |
{ "meronym", MERONYM, POS_MASK(NOUN), "Meronyms" }, |
| 161 |
{ "meronym/rec", -HMERONYM, POS_MASK(NOUN), "Meronyms" }, |
| 162 |
{ "meronym/substance", HASSTUFFPTR, POS_MASK(NOUN), "Substance Meronyms" }, |
| 163 |
{ "meronym/part", HASPARTPTR, POS_MASK(NOUN), "Part Meronyms" }, |
| 164 |
{ "meronym/member", HASMEMBERPTR, POS_MASK(NOUN), "Member Meronyms" }, |
| 165 |
{ "derivation", DERIVATION, POS_MASK(NOUN)|POS_MASK(VERB), |
| 166 |
"Derived Forms" }, |
| 167 |
{ "relatives", RELATIVES, POS_MASK(VERB), |
| 168 |
"Synonyms (Grouped by Similarity of Meaning)" }, |
| 169 |
{ "coords", COORDS, POS_MASK(NOUN)|POS_MASK(VERB), |
| 170 |
"Coordinate Terms (sisters)" }, |
| 171 |
{ "entail", ENTAILPTR, POS_MASK(VERB), "Entailment" }, |
| 172 |
{ "cause", CAUSETO, POS_MASK(VERB), "\'Cause To\'" }, |
| 173 |
{ "frames", FRAMES, POS_MASK(VERB), "Sample Sentences" }, |
| 174 |
#endif |
| 175 |
{ NULL } |
| 176 |
}; |
| 177 |
|
| 178 |
static struct wn_option * |
| 179 |
find_option(const char *text) |
| 180 |
{ |
| 181 |
struct wn_option *p; |
| 182 |
|
| 183 |
for (p = wn_optlist; p->option; p++) { |
| 184 |
if (strcmp(p->option, text) == 0) |
| 185 |
return p; |
| 186 |
} |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
static dico_handle_t |
| 191 |
wn_init_db(const char *dbname, int argc, char **argv) |
| 192 |
{ |
| 193 |
struct wndb *wndb; |
| 194 |
int pos = 0; |
| 195 |
int idx, i, j; |
| 196 |
struct wn_option **optv; |
| 197 |
int optc; |
| 198 |
int flags = 0; |
| 199 |
static struct wn_option overview[2] = { |
| 200 |
{ "overview", OVERVIEW, PM_ALL, "Overview", NULL, _wn_print_overview }, |
| 201 |
{ "overview", OVERVIEW, PM_ALL, "Overview", NULL, _wn_print_definition } |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
struct dico_option init_db_option[] = { |
| 206 |
{ DICO_OPTSTR(pos), dico_opt_enum, &pos, { .enumstr = pos_choice } }, |
| 207 |
{ DICO_OPTSTR(merge-defs), dico_opt_bool, &flags, |
| 208 |
{ .value = WNDB_MERGE_DEFS } }, |
| 209 |
{ NULL } |
| 210 |
}; |
| 211 |
|
| 212 |
if (dico_parseopt(init_db_option, argc, argv, DICO_PARSEOPT_PERMUTE, |
| 213 |
&idx)) |
| 214 |
return NULL; |
| 215 |
|
| 216 |
argc -= idx; |
| 217 |
argv += idx; |
| 218 |
|
| 219 |
optc = argc + 1; |
| 220 |
optv = calloc(optc, sizeof(optv[0])); |
| 221 |
if (!optv) { |
| 222 |
DICO_LOG_ERRNO(); |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
|
| 226 |
optv[0] = overview + ((flags & WNDB_MERGE_DEFS) ? 1 : 0); |
| 227 |
|
| 228 |
for (i = 0, j = 1; i < argc; i++) { |
| 229 |
struct wn_option *p; |
| 230 |
|
| 231 |
if ((p = find_option(argv[i])) == NULL) { |
| 232 |
dico_log(L_ERR, 0, _("wordnet: unknown option %s"), argv[i]); |
| 233 |
free(optv); |
| 234 |
return NULL; |
| 235 |
} |
| 236 |
while (p->search == 0 && p > wn_optlist) |
| 237 |
p--; |
| 238 |
if (!p->printer) { |
| 239 |
dico_log(L_WARN, 0, _("wordnet: option %s is not yet supported"), |
| 240 |
argv[i]); |
| 241 |
continue; |
| 242 |
} |
| 243 |
optv[j] = p; |
| 244 |
} |
| 245 |
|
| 246 |
wndb = calloc(1, sizeof(*wndb)); |
| 247 |
if (!wndb) { |
| 248 |
DICO_LOG_ERRNO(); |
| 249 |
free(optv); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
wndb->dbname = strdup(dbname); |
| 253 |
if (!wndb->dbname) { |
| 254 |
DICO_LOG_ERRNO(); |
| 255 |
free(wndb); |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
wndb->flags = flags; |
| 259 |
wndb->pos = pos_trans[pos]; |
| 260 |
wndb->optc = optc; |
| 261 |
wndb->optv = optv; |
| 262 |
|
| 263 |
return (dico_handle_t)wndb; |
| 264 |
} |
| 265 |
|
| 266 |
static char * |
| 267 |
wn_descr(dico_handle_t hp) |
| 268 |
{ |
| 269 |
return strdup("WordNet dictionary"); |
| 270 |
} |
| 271 |
|
| 272 |
static char * |
| 273 |
wn_info(dico_handle_t hp) |
| 274 |
{ |
| 275 |
return strdup(license); |
| 276 |
} |
| 277 |
|
| 278 |
static int |
| 279 |
wn_lang(dico_handle_t hp, dico_list_t list[2]) |
| 280 |
{ |
| 281 |
if ((list[0] = dico_list_create()) == NULL) |
| 282 |
return -1; |
| 283 |
if ((list[1] = dico_list_create()) == NULL) { |
| 284 |
dico_list_destroy(&list[0]); |
| 285 |
return -1; |
| 286 |
} |
| 287 |
dico_list_append(list[0], strdup("en")); |
| 288 |
dico_list_append(list[1], strdup("en")); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
struct wordbuf { |
| 293 |
char *word; |
| 294 |
size_t len; |
| 295 |
size_t size; |
| 296 |
}; |
| 297 |
#define WORDBUFINC 16 |
| 298 |
#define INIT_WORDBUF { NULL, 0, 0 } |
| 299 |
#define wordbuf_start(wb) ((wb)->len=0) |
| 300 |
|
| 301 |
static int |
| 302 |
wordbuf_expand(struct wordbuf *wb, size_t len) |
| 303 |
{ |
| 304 |
if (len >= wb->size) { |
| 305 |
size_t size = ((len + WORDBUFINC - 1) / WORDBUFINC ) * WORDBUFINC; |
| 306 |
char *newword = realloc(wb->word, size); |
| 307 |
if (!newword) { |
| 308 |
DICO_LOG_ERRNO(); |
| 309 |
return 1; |
| 310 |
} |
| 311 |
wb->word = newword; |
| 312 |
wb->size = size; |
| 313 |
} |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
static int |
| 318 |
wordbuf_grow(struct wordbuf *wb, int c) |
| 319 |
{ |
| 320 |
if (wordbuf_expand(wb, wb->len + 1)) |
| 321 |
return 1; |
| 322 |
wb->word[wb->len++] = c; |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
static int |
| 327 |
wordbuf_finish(struct wordbuf *wb) |
| 328 |
{ |
| 329 |
if (wordbuf_expand(wb, wb->len + 1)) |
| 330 |
return 1; |
| 331 |
wb->word[wb->len] = 0; |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
static void |
| 336 |
wordbuf_reverse(struct wordbuf *wb) |
| 337 |
{ |
| 338 |
int i, j; |
| 339 |
|
| 340 |
if (wb->len == 0) |
| 341 |
return; |
| 342 |
for (i = 0, j = wb->len - 1; i < j; i++, j--) { |
| 343 |
int c = wb->word[j]; |
| 344 |
wb->word[j] = wb->word[i]; |
| 345 |
wb->word[i] = c; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
enum result_type { |
| 351 |
result_match, |
| 352 |
result_define |
| 353 |
}; |
| 354 |
|
| 355 |
struct defn { |
| 356 |
int pos; /* Part of speach */ |
| 357 |
SynsetPtr *synset; /* wndb->optc elements, first one always being |
| 358 |
the overview. */ |
| 359 |
}; |
| 360 |
|
| 361 |
struct result { |
| 362 |
enum result_type type; |
| 363 |
size_t compare_count; |
| 364 |
struct wndb *wndb; |
| 365 |
dico_list_t list; |
| 366 |
dico_iterator_t itr; |
| 367 |
/* For definitions only: */ |
| 368 |
char *searchword; |
| 369 |
dico_list_t rootlist; /* List of root synsets */ |
| 370 |
}; |
| 371 |
|
| 372 |
static int |
| 373 |
free_defn(void *item, void *data) |
| 374 |
{ |
| 375 |
struct defn *dp = item; |
| 376 |
free(dp->synset); |
| 377 |
free(dp); |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
static int |
| 382 |
free_root_synset(void *item, void *data) |
| 383 |
{ |
| 384 |
free_syns(item); |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
static int |
| 389 |
free_string(void *item, void *data) |
| 390 |
{ |
| 391 |
free(item); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
static int |
| 396 |
compare_words(const void *a, const void *b, void *closure) |
| 397 |
{ |
| 398 |
return utf8_strcasecmp((char*)a, (char*)b); |
| 399 |
} |
| 400 |
|
| 401 |
static struct result * |
| 402 |
wn_create_match_result(struct wndb *wndb) |
| 403 |
{ |
| 404 |
struct result *res; |
| 405 |
|
| 406 |
res = calloc(1, sizeof(*res)); |
| 407 |
if (!res) { |
| 408 |
DICO_LOG_ERRNO(); |
| 409 |
return NULL; |
| 410 |
} |
| 411 |
res->type = result_match; |
| 412 |
res->wndb = wndb; |
| 413 |
res->list = dico_list_create(); |
| 414 |
if (!res) { |
| 415 |
DICO_LOG_ERRNO(); |
| 416 |
free(res); |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
dico_list_set_free_item(res->list, free_string, NULL); |
| 420 |
dico_list_set_comparator(res->list, compare_words, NULL); |
| 421 |
dico_list_set_flags(res->list, DICO_LIST_COMPARE_TAIL); |
| 422 |
return res; |
| 423 |
} |
| 424 |
|
| 425 |
static struct result * |
| 426 |
wn_create_define_result(struct wndb *wndb, const char *searchword) |
| 427 |
{ |
| 428 |
struct result *res; |
| 429 |
|
| 430 |
res = calloc(1, sizeof(*res)); |
| 431 |
if (!res) { |
| 432 |
DICO_LOG_ERRNO(); |
| 433 |
return NULL; |
| 434 |
} |
| 435 |
res->type = result_define; |
| 436 |
res->wndb = wndb; |
| 437 |
res->list = dico_list_create(); |
| 438 |
if (!res) { |
| 439 |
DICO_LOG_ERRNO(); |
| 440 |
free(res); |
| 441 |
return NULL; |
| 442 |
} |
| 443 |
dico_list_set_free_item(res->list, free_defn, NULL); |
| 444 |
|
| 445 |
res->searchword = strdup(searchword); |
| 446 |
if (!res->searchword) { |
| 447 |
DICO_LOG_ERRNO(); |
| 448 |
wn_free_result((dico_result_t) res); |
| 449 |
} |
| 450 |
|
| 451 |
res->rootlist = dico_list_create(); |
| 452 |
if (!res->rootlist) { |
| 453 |
DICO_LOG_ERRNO(); |
| 454 |
wn_free_result((dico_result_t) res); |
| 455 |
} |
| 456 |
dico_list_set_free_item(res->rootlist, free_root_synset, NULL); |
| 457 |
|
| 458 |
return res; |
| 459 |
} |
| 460 |
|
| 461 |
static int |
| 462 |
wn_match_result_add(struct result *res, const char *hw) |
| 463 |
{ |
| 464 |
int rc; |
| 465 |
char *s = strdup(hw); |
| 466 |
|
| 467 |
if (!s) { |
| 468 |
DICO_LOG_ERRNO(); |
| 469 |
return -1; |
| 470 |
} |
| 471 |
rc = dico_list_insert_sorted(res->list, s); |
| 472 |
if (rc) { |
| 473 |
free(s); |
| 474 |
if (rc != EEXIST) { |
| 475 |
DICO_LOG_MEMERR(); |
| 476 |
return -1; |
| 477 |
} |
| 478 |
} |
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
skipeol(FILE *fp) |
| 484 |
{ |
| 485 |
int c; |
| 486 |
while ((c = getc(fp)) != '\n' && c != EOF) |
| 487 |
; |
| 488 |
} |
| 489 |
|
| 490 |
static void |
| 491 |
skipheader(FILE *fp) |
| 492 |
{ |
| 493 |
int c; |
| 494 |
|
| 495 |
while ((c = getc(fp)) == ' ') |
| 496 |
skipeol(fp); |
| 497 |
ungetc(c, fp); |
| 498 |
} |
| 499 |
|
| 500 |
static int |
| 501 |
getword(FILE *fp, struct wordbuf *wb) |
| 502 |
{ |
| 503 |
int c; |
| 504 |
size_t i; |
| 505 |
|
| 506 |
wordbuf_start(wb); |
| 507 |
while ((c = getc(fp)) != EOF) { |
| 508 |
if (c == ' ') |
| 509 |
break; |
| 510 |
if (wordbuf_grow(wb, c)) |
| 511 |
return -1; |
| 512 |
} |
| 513 |
if (wb->len == 0 && c == EOF) |
| 514 |
return -1; |
| 515 |
if (wordbuf_finish(wb)) |
| 516 |
return -1; |
| 517 |
for (i = 0; wb->word[i]; i++) |
| 518 |
if (wb->word[i] == '_') |
| 519 |
wb->word[i] = ' '; |
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
static int |
| 524 |
lineback(FILE *fp, struct wordbuf *wb) |
| 525 |
{ |
| 526 |
int c, i; |
| 527 |
|
| 528 |
wordbuf_start(wb); |
| 529 |
while (fseek(fp, -2, SEEK_CUR) == 0) { |
| 530 |
if ((c = getc(fp)) == '\n') |
| 531 |
break; |
| 532 |
if (wordbuf_grow(wb, c)) |
| 533 |
return -1; |
| 534 |
} |
| 535 |
if (wordbuf_finish(wb)) |
| 536 |
return -1; |
| 537 |
wordbuf_reverse(wb); |
| 538 |
for (i = 0; wb->word[i]; i++) { |
| 539 |
if (wb->word[i] == ' ') |
| 540 |
break; |
| 541 |
else if (wb->word[i] == '_') |
| 542 |
wb->word[i] = ' '; |
| 543 |
} |
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
static int |
| 548 |
wn_is_defined(struct wndb *wndb, char *searchword) |
| 549 |
{ |
| 550 |
int i, j; |
| 551 |
unsigned int search; |
| 552 |
|
| 553 |
for (i = 1; i <= NUMPARTS; i++) { |
| 554 |
if ((search = is_defined(searchword, i)) != 0) { |
| 555 |
for (j = 0; j < wndb->optc; j++) { |
| 556 |
int n; |
| 557 |
|
| 558 |
if (!(POS_MASK(i) & wndb->optv[j]->posmask)) |
| 559 |
continue; |
| 560 |
|
| 561 |
n = wndb->optv[j]->search; |
| 562 |
if (n < 0) |
| 563 |
n = -n; |
| 564 |
if (bit(n) & search) |
| 565 |
return 1; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
return 0; |
| 570 |
} |
| 571 |
|
| 572 |
static void |
| 573 |
wn_foreach_db(struct wndb *wndb, int dbn, char *searchword, |
| 574 |
struct dico_key *key, struct result *res) |
| 575 |
{ |
| 576 |
FILE *fp = indexfps[dbn]; |
| 577 |
struct wordbuf wb = INIT_WORDBUF; |
| 578 |
dico_iterator_t itr; |
| 579 |
char *word; |
| 580 |
|
| 581 |
fseek(fp, 0, SEEK_SET); |
| 582 |
for (skipheader(fp); getword(fp, &wb) == 0; skipeol(fp)) { |
| 583 |
res->compare_count++; |
| 584 |
if (dico_key_match(key, wb.word)) { |
| 585 |
if (wn_match_result_add(res, wb.word)) |
| 586 |
break; |
| 587 |
} |
| 588 |
} |
| 589 |
free(wb.word); |
| 590 |
|
| 591 |
itr = dico_list_iterator(res->list); |
| 592 |
for (word = dico_iterator_first(itr); word; |
| 593 |
word = dico_iterator_next(itr)) { |
| 594 |
if (!wn_is_defined(wndb, word)) |
| 595 |
dico_iterator_remove_current(itr, NULL); |
| 596 |
} |
| 597 |
dico_iterator_destroy(&itr); |
| 598 |
} |
| 599 |
|
| 600 |
static struct result * |
| 601 |
wn_foreach(struct wndb *wndb, const dico_strategy_t strat, const char *word) |
| 602 |
{ |
| 603 |
struct result *res = wn_create_match_result(wndb); |
| 604 |
struct dico_key key; |
| 605 |
char *searchword; |
| 606 |
|
| 607 |
searchword = strdup(word); |
| 608 |
strtolower(strsubst(searchword, ' ', '_')); |
| 609 |
|
| 610 |
if (!searchword) { |
| 611 |
DICO_LOG_MEMERR(); |
| 612 |
wn_free_result((dico_result_t) res); |
| 613 |
return NULL; |
| 614 |
} |
| 615 |
|
| 616 |
if (dico_key_init(&key, strat, word)) { |
| 617 |
dico_log(L_ERR, 0, _("%s: key initialization failed"), __func__); |
| 618 |
wn_free_result((dico_result_t) res); |
| 619 |
free(searchword); |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
|
| 623 |
if (wndb->pos == ALL_POS) { |
| 624 |
int i; |
| 625 |
|
| 626 |
for (i = 1; i <= NUMPARTS; i++) |
| 627 |
wn_foreach_db(wndb, i, searchword, &key, res); |
| 628 |
} else |
| 629 |
wn_foreach_db(wndb, wndb->pos, searchword, &key, res); |
| 630 |
|
| 631 |
dico_key_deinit(&key); |
| 632 |
free(searchword); |
| 633 |
|
| 634 |
if (dico_list_count(res->list) == 0) { |
| 635 |
wn_free_result((dico_result_t) res); |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
return res; |
| 639 |
} |
| 640 |
|
| 641 |
static off_t |
| 642 |
wn_bsearch(FILE *fp, void *key, int (*cmp)(const char *a, void *b)) |
| 643 |
{ |
| 644 |
long top, mid, bot, diff; |
| 645 |
struct wordbuf wb = INIT_WORDBUF; |
| 646 |
off_t last_match = -1; |
| 647 |
|
| 648 |
fseek(fp, 0L, SEEK_END); |
| 649 |
top = 0; |
| 650 |
bot = ftell(fp); |
| 651 |
mid = (bot - top) / 2; |
| 652 |
|
| 653 |
do { |
| 654 |
fseek(fp, mid - 1, SEEK_SET); |
| 655 |
if (mid != 1) |
| 656 |
skipeol(fp); |
| 657 |
if (getword(fp, &wb)) |
| 658 |
break; |
| 659 |
if (cmp(wb.word, key) < 0) { |
| 660 |
top = mid; |
| 661 |
diff = (bot - top) / 2; |
| 662 |
mid = top + diff; |
| 663 |
} else if (cmp(wb.word, key) > 0) { |
| 664 |
bot = mid; |
| 665 |
diff = (bot - top) / 2; |
| 666 |
mid = top + diff; |
| 667 |
} else { |
| 668 |
/* Move back until first non-matching headword is found */ |
| 669 |
do |
| 670 |
last_match = ftell(fp); |
| 671 |
while (lineback(fp, &wb) == 0 && cmp(wb.word, key) == 0); |
| 672 |
break; |
| 673 |
} |
| 674 |
} while (diff); |
| 675 |
|
| 676 |
free(wb.word); |
| 677 |
return last_match; |
| 678 |
} |
| 679 |
|
| 680 |
static struct result * |
| 681 |
wn_exact_match(struct wndb *db, const char *hw) |
| 682 |
{ |
| 683 |
struct result *res; |
| 684 |
|
| 685 |
if (!wn_is_defined(db, (char*)hw)) |
| 686 |
return NULL; |
| 687 |
res = wn_create_match_result(db); |
| 688 |
dico_list_append(res->list, strdup(hw)); |
| 689 |
return res; |
| 690 |
} |
| 691 |
|
| 692 |
struct prefix { |
| 693 |
const char *str; |
| 694 |
size_t len; |
| 695 |
}; |
| 696 |
|
| 697 |
static int |
| 698 |
cmp_pref(const char *hw, void *key) |
| 699 |
{ |
| 700 |
struct prefix *pref = key; |
| 701 |
return strncasecmp(hw, pref->str, pref->len); |
| 702 |
} |
| 703 |
|
| 704 |
static struct result * |
| 705 |
wn_prefix_match(struct wndb *db, const char *hw) |
| 706 |
{ |
| 707 |
struct result *res; |
| 708 |
int i; |
| 709 |
struct prefix pfx; |
| 710 |
struct wordbuf wb = INIT_WORDBUF; |
| 711 |
|
| 712 |
res = wn_create_match_result(db); |
| 713 |
if (!res) |
| 714 |
return NULL; |
| 715 |
pfx.str = hw; |
| 716 |
pfx.len = strlen(hw); |
| 717 |
|
| 718 |
for (i = 1; i <= NUMPARTS; i++) { |
| 719 |
FILE *fp = indexfps[i]; |
| 720 |
off_t off; |
| 721 |
|
| 722 |
off = wn_bsearch(fp, &pfx, cmp_pref); |
| 723 |
if (off != -1) { |
| 724 |
fseek(fp, off, SEEK_SET); |
| 725 |
for (;getword(fp, &wb) == 0 && cmp_pref(wb.word, &pfx) == 0; |
| 726 |
skipeol(fp)) { |
| 727 |
if (wn_match_result_add(res, wb.word)) |
| 728 |
break; |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
free(wb.word); |
| 733 |
if (dico_list_count(res->list) == 0) { |
| 734 |
wn_free_result((dico_result_t) res); |
| 735 |
return NULL; |
| 736 |
} |
| 737 |
return res; |
| 738 |
} |
| 739 |
|
| 740 |
typedef struct result *(*wn_matcher_t)(struct wndb *db, const char *hw); |
| 741 |
|
| 742 |
struct strategy_def { |
| 743 |
struct dico_strategy strat; |
| 744 |
wn_matcher_t matcher; |
| 745 |
}; |
| 746 |
|
| 747 |
static struct strategy_def stratdef[] = { |
| 748 |
{ { "exact", "Match words exactly" }, wn_exact_match }, |
| 749 |
{ { "prefix", "Match word prefixes" }, wn_prefix_match }, |
| 750 |
}; |
| 751 |
|
| 752 |
static wn_matcher_t |
| 753 |
wn_find_matcher(const char *strat) |
| 754 |
{ |
| 755 |
int i; |
| 756 |
for (i = 0; i < DICO_ARRAY_SIZE(stratdef); i++) |
| 757 |
if (strcmp(strat, stratdef[i].strat.name) == 0) |
| 758 |
return stratdef[i].matcher; |
| 759 |
return NULL; |
| 760 |
} |
| 761 |
|
| 762 |
static void |
| 763 |
wn_register_strategies(void) |
| 764 |
{ |
| 765 |
int i; |
| 766 |
for (i = 0; i < DICO_ARRAY_SIZE(stratdef); i++) |
| 767 |
dico_strategy_add(&stratdef[i].strat); |
| 768 |
} |
| 769 |
|
| 770 |
#define ISSPACE(c) ((c) == ' ' || (c) == '\t') |
| 771 |
|
| 772 |
static char * |
| 773 |
nornmalize_search_word(const char *word) |
| 774 |
{ |
| 775 |
char *copy = NULL; |
| 776 |
char *p; |
| 777 |
const char *q; |
| 778 |
|
| 779 |
copy = malloc(strlen(word) + 1); |
| 780 |
if (!copy) { |
| 781 |
DICO_LOG_ERRNO(); |
| 782 |
return NULL; |
| 783 |
} |
| 784 |
for (p = copy, q = word; *q; ) { |
| 785 |
if (ISSPACE(*q)) { |
| 786 |
*p++ = '_'; |
| 787 |
do |
| 788 |
q++; |
| 789 |
while (*q && ISSPACE(*q)); |
| 790 |
} else |
| 791 |
*p++ = *q++; |
| 792 |
} |
| 793 |
*p = 0; |
| 794 |
strtolower(copy); |
| 795 |
return copy; |
| 796 |
} |
| 797 |
|
| 798 |
static dico_result_t |
| 799 |
wn_match(dico_handle_t hp, const dico_strategy_t strat, const char *word) |
| 800 |
{ |
| 801 |
struct wndb *wndb = (struct wndb *)hp; |
| 802 |
wn_matcher_t match = wn_find_matcher(strat->name); |
| 803 |
|
| 804 |
if (match) |
| 805 |
return (dico_result_t) match(wndb, word); |
| 806 |
else if (strat->sel) |
| 807 |
return (dico_result_t) wn_foreach(wndb, strat, word); |
| 808 |
return NULL; |
| 809 |
} |
| 810 |
|
| 811 |
static struct defn * |
| 812 |
create_defn(struct wndb *wndb, int pos) |
| 813 |
{ |
| 814 |
struct defn *p = malloc(sizeof(*p)); |
| 815 |
if (!p) { |
| 816 |
DICO_LOG_ERRNO(); |
| 817 |
return NULL; |
| 818 |
} |
| 819 |
p->synset = calloc(wndb->optc, sizeof(p->synset[0])); |
| 820 |
if (!p->synset) { |
| 821 |
DICO_LOG_ERRNO(); |
| 822 |
free(p); |
| 823 |
return NULL; |
| 824 |
} |
| 825 |
p->pos = pos; |
| 826 |
return p; |
| 827 |
} |
| 828 |
|
| 829 |
static int |
| 830 |
search_defns(struct wndb *wndb, int pos, struct result *res, |
| 831 |
const char *searchword) |
| 832 |
{ |
| 833 |
SynsetPtr sp; |
| 834 |
int i; |
| 835 |
struct defn *dp; |
| 836 |
int sense = 0; |
| 837 |
|
| 838 |
sp = findtheinfo_ds((char*)searchword, pos, OVERVIEW, ALLSENSES); |
| 839 |
|
| 840 |
if (!sp) |
| 841 |
return 0; |
| 842 |
dico_list_append(res->rootlist, sp); |
| 843 |
|
| 844 |
do { |
| 845 |
dp = create_defn(wndb, pos); |
| 846 |
if (!dp) |
| 847 |
return 0; |
| 848 |
dp->synset[0] = sp; |
| 849 |
|
| 850 |
++sense; |
| 851 |
for (i = 1; i < wndb->optc; i++) { |
| 852 |
SynsetPtr ssp; |
| 853 |
if (!(POS_MASK(pos) & wndb->optv[i]->posmask)) |
| 854 |
continue; |
| 855 |
ssp = findtheinfo_ds((char*)searchword, pos, wndb->optv[i]->search, |
| 856 |
sense); |
| 857 |
if (ssp) |
| 858 |
dp->synset[i] = ssp; |
| 859 |
} |
| 860 |
dico_list_append(res->list, dp); |
| 861 |
} while ((sp = sp->nextss)); |
| 862 |
|
| 863 |
return 1; |
| 864 |
} |
| 865 |
|
| 866 |
static dico_result_t |
| 867 |
wn_define(dico_handle_t hp, const char *word) |
| 868 |
{ |
| 869 |
struct wndb *wndb = (struct wndb *)hp; |
| 870 |
struct result *res; |
| 871 |
int i; |
| 872 |
int found = 0; |
| 873 |
char *copy; |
| 874 |
|
| 875 |
res = wn_create_define_result(wndb, word); |
| 876 |
|
| 877 |
copy = nornmalize_search_word(word); |
| 878 |
if (!copy) { |
| 879 |
wn_free_result((dico_result_t) res); |
| 880 |
return NULL; |
| 881 |
} |
| 882 |
|
| 883 |
res->wndb = wndb; |
| 884 |
res->type = result_define; |
| 885 |
if (wndb->pos == ALL_POS) { |
| 886 |
for (i = 1; i <= NUMPARTS; i++) |
| 887 |
found += search_defns(wndb, i, res, copy); |
| 888 |
} else |
| 889 |
found = search_defns(wndb, wndb->pos, res, copy); |
| 890 |
|
| 891 |
if (!found) { |
| 892 |
free(copy); |
| 893 |
wn_free_result((dico_result_t) res); |
| 894 |
return NULL; |
| 895 |
} |
| 896 |
free(copy); |
| 897 |
|
| 898 |
return (dico_result_t)res; |
| 899 |
} |
| 900 |
|
| 901 |
static void |
| 902 |
format_word(const char *word, dico_stream_t str) |
| 903 |
{ |
| 904 |
for (;;) { |
| 905 |
size_t len = strcspn(word, "_"); |
| 906 |
dico_stream_write(str, word, len); |
| 907 |
if (word[len] == 0) |
| 908 |
break; |
| 909 |
dico_stream_write(str, " ", 1); |
| 910 |
word += len + 1; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
static void |
| 915 |
format_defn_string(const char *defn, dico_stream_t str) |
| 916 |
{ |
| 917 |
size_t len = strlen(defn); |
| 918 |
|
| 919 |
if (len == 0) |
| 920 |
return; |
| 921 |
if (defn[0] == '(' && defn[len-1] == ')') { |
| 922 |
defn++; |
| 923 |
len -= 2; |
| 924 |
} |
| 925 |
dico_stream_write(str, defn, len); |
| 926 |
dico_stream_write(str, "\n", 1); |
| 927 |
} |
| 928 |
|
| 929 |
static void |
| 930 |
_wn_print_overview(struct wn_option *opt, SynsetPtr sp, struct result *res, |
| 931 |
dico_stream_t str) |
| 932 |
{ |
| 933 |
int i; |
| 934 |
|
| 935 |
/* Print words: */ |
| 936 |
for (i = 0; i < sp->wcount; i++) { |
| 937 |
if (i) |
| 938 |
dico_stream_write(str, ", ", 2); |
| 939 |
format_word(sp->words[i], str); |
| 940 |
} |
| 941 |
|
| 942 |
/* Print pos */ |
| 943 |
dico_stream_write(str, "; ", 2); |
| 944 |
dico_stream_write(str, sp->pos, strlen(sp->pos)); |
| 945 |
dico_stream_write(str, ".\n\n", 3); |
| 946 |
|
| 947 |
/* Print definition */ |
| 948 |
format_defn_string(sp->defn, str); |
| 949 |
} |
| 950 |
|
| 951 |
static void |
| 952 |
_wn_print_definition(struct wn_option *opt, SynsetPtr sp, struct result *res, |
| 953 |
dico_stream_t str) |
| 954 |
{ |
| 955 |
/* Print definition */ |
| 956 |
format_defn_string(sp->defn, str); |
| 957 |
|
| 958 |
if (sp->wcount > 1) { |
| 959 |
int i, j; |
| 960 |
static char const *sym = "Synonyms: "; |
| 961 |
|
| 962 |
dico_stream_write(str, sym, strlen(sym)); |
| 963 |
for (i = j = 0; i < sp->wcount; i++) { |
| 964 |
if (strcmp(sp->words[i], res->searchword) == 0) |
| 965 |
continue; |
| 966 |
if (j) |
| 967 |
dico_stream_write(str, ", ", 2); |
| 968 |
dico_stream_write(str, "{", 1); |
| 969 |
format_word(sp->words[i], str); |
| 970 |
dico_stream_write(str, "}", 1); |
| 971 |
j++; |
| 972 |
} |
| 973 |
dico_stream_write(str, "\n", 1); |
| 974 |
} |
| 975 |
|
| 976 |
} |
| 977 |
|
| 978 |
static void |
| 979 |
_wn_print_hypernym(struct wn_option *opt, SynsetPtr ptr, struct result *res, |
| 980 |
dico_stream_t str) |
| 981 |
{ |
| 982 |
int i; |
| 983 |
SynsetPtr sp; |
| 984 |
|
| 985 |
dico_stream_write(str, opt->label, strlen(opt->label)); |
| 986 |
dico_stream_write(str, ":\n\n", 3); |
| 987 |
|
| 988 |
for (sp = ptr->ptrlist; sp; sp = sp->nextss) { |
| 989 |
for (i = 0; i < sp->wcount; i++) { |
| 990 |
if (i) |
| 991 |
dico_stream_write(str, ", ", 2); |
| 992 |
format_word(sp->words[i], str); |
| 993 |
} |
| 994 |
dico_stream_write(str, "\n", 1); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
static int |
| 999 |
format_defn(struct defn *defn, struct result *res, dico_stream_t str) |
| 1000 |
{ |
| 1001 |
int i; |
| 1002 |
struct wndb *wndb = res->wndb; |
| 1003 |
|
| 1004 |
for (i = 0; i < wndb->optc; i++) |
| 1005 |
wndb->optv[i]->printer(wndb->optv[i], defn->synset[i], res, str); |
| 1006 |
return 0; |
| 1007 |
} |
| 1008 |
|
| 1009 |
static void |
| 1010 |
print_num(dico_stream_t str, unsigned num) |
| 1011 |
{ |
| 1012 |
char buf[128]; |
| 1013 |
char *p = buf + sizeof(buf); |
| 1014 |
|
| 1015 |
*--p = 0; |
| 1016 |
while (p > buf && num) { |
| 1017 |
*--p = num % 10 + '0'; |
| 1018 |
num /= 10; |
| 1019 |
} |
| 1020 |
dico_stream_write(str, p, strlen(p)); |
| 1021 |
dico_stream_write(str, ". ", 2); |
| 1022 |
} |
| 1023 |
|
| 1024 |
static void |
| 1025 |
format_all_defns(struct result *res, dico_stream_t str) |
| 1026 |
{ |
| 1027 |
struct defn *defn; |
| 1028 |
int pos = 0; |
| 1029 |
unsigned num; |
| 1030 |
|
| 1031 |
format_word(res->searchword, str); |
| 1032 |
dico_stream_write(str, "\n", 1); |
| 1033 |
|
| 1034 |
for (defn = dico_iterator_first(res->itr); defn; |
| 1035 |
defn = dico_iterator_next(res->itr)) { |
| 1036 |
if (defn->pos != pos) { |
| 1037 |
pos = defn->pos; |
| 1038 |
num = 1; |
| 1039 |
|
| 1040 |
dico_stream_write(str, defn->synset[0]->pos, |
| 1041 |
strlen(defn->synset[0]->pos)); |
| 1042 |
dico_stream_write(str, ". ", 2); |
| 1043 |
} |
| 1044 |
|
| 1045 |
print_num(str, num); |
| 1046 |
format_defn(defn, res, str); |
| 1047 |
num++; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
int |
| 1052 |
wn_output_result(dico_result_t rp, size_t n, dico_stream_t str) |
| 1053 |
{ |
| 1054 |
struct result *res = (struct result *) rp; |
| 1055 |
void *item; |
| 1056 |
|
| 1057 |
if (!res->itr) { |
| 1058 |
res->itr = dico_list_iterator(res->list); |
| 1059 |
if (!res->itr) |
| 1060 |
return 1; |
| 1061 |
} |
| 1062 |
item = dico_iterator_item(res->itr, n); |
| 1063 |
switch (res->type) { |
| 1064 |
case result_match: |
| 1065 |
dico_stream_write(str, item, strlen((char*)item)); |
| 1066 |
break; |
| 1067 |
|
| 1068 |
case result_define: |
| 1069 |
if (res->wndb->flags & WNDB_MERGE_DEFS) |
| 1070 |
format_all_defns(res, str); |
| 1071 |
else |
| 1072 |
format_defn(item, res, str); |
| 1073 |
break; |
| 1074 |
|
| 1075 |
default: |
| 1076 |
return 1; |
| 1077 |
} |
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
|
| 1081 |
static size_t |
| 1082 |
wn_result_count (dico_result_t rp) |
| 1083 |
{ |
| 1084 |
struct result *res = (struct result *) rp; |
| 1085 |
if (res->type == result_define && (res->wndb->flags & WNDB_MERGE_DEFS)) |
| 1086 |
return 1; |
| 1087 |
return dico_list_count(res->list); |
| 1088 |
} |
| 1089 |
|
| 1090 |
static size_t |
| 1091 |
wn_compare_count (dico_result_t rp) |
| 1092 |
{ |
| 1093 |
struct result *res = (struct result *) rp; |
| 1094 |
return res->compare_count; |
| 1095 |
} |
| 1096 |
|
| 1097 |
static void |
| 1098 |
wn_free_result(dico_result_t rp) |
| 1099 |
{ |
| 1100 |
struct result *res = (struct result *) rp; |
| 1101 |
|
| 1102 |
dico_list_destroy(&res->list); |
| 1103 |
dico_iterator_destroy(&res->itr); |
| 1104 |
dico_list_destroy(&res->rootlist); |
| 1105 |
free(res->searchword); |
| 1106 |
free(res); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
struct dico_database_module DICO_EXPORT(wordnet, module) = { |
| 1111 |
DICO_MODULE_VERSION, |
| 1112 |
DICO_CAPA_NONE, |
| 1113 |
wn_init, |
| 1114 |
wn_init_db, |
| 1115 |
wn_free_db, |
| 1116 |
NULL, |
| 1117 |
NULL, |
| 1118 |
wn_info, |
| 1119 |
wn_descr, |
| 1120 |
wn_lang, |
| 1121 |
wn_match, |
| 1122 |
wn_define, |
| 1123 |
wn_output_result, |
| 1124 |
wn_result_count, |
| 1125 |
wn_compare_count, |
| 1126 |
wn_free_result |
| 1127 |
}; |