1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
#include <bow/libbow.h>
#include <bow/archer.h>
#include <bow/archer_query.h>
#include <bow/archer_query_array.h>
#include <bow/archer_query_execute.h>
#include <bow/archer_query_index.h>
#include <bow/archer_query_table.h>
extern bow_sarray *archer_docs;
archer_query_info *archer_query_last_query = NULL;
int archer_query_doc_restriction = -1;
static int
list_length (archer_query_term * term)
{
int ret;
ret = 0;
while (term && term->proximity)
{
ret++;
term = term->proximity->term;
}
if (term)
ret++;
return ret;
}
/* moves the index file pointers from where they are to the next di that is
>= target_di for term `term', and places the final di in result_di. if this
is not possible (i.e. no more dis that meet that critereon exist) then the
return value is 1; success returns 0. */
static int
scan_to_di (bow_index * index, archer_query_term * term, int target_di)
{
int ret, pi;
archer_query_index_current_di (index, term, &ret, &pi);
while ((ret != -1) &&
((archer_query_doc_restriction > -1 &&
ret != archer_query_doc_restriction) ||
(ret < target_di)))
archer_query_index_next_di (index, term, &ret);
return ret;
}
/* Insert into pi_array, maintaining sortedness */
static void
insert_pi_into_pi_array(int pi, bow_array *pi_array)
{
void *ptr;
int i, opi, len = pi_array->length;
for (i = 0; i < len; ++i)
{
opi = *(int *)bow_array_entry_at_index(pi_array, i);
if (pi == opi)
return;
if (opi > pi)
break;
}
/* i is now the appropriate index for pi */
if (i == len)
bow_array_append(pi_array, &pi);
else
{
/* Make space for a new entry */
opi = -1;
bow_array_append(pi_array, &opi);
/* Shift contents up one space */
ptr = bow_array_entry_at_index(pi_array, i);
memmove(ptr + sizeof(int), ptr, sizeof(int) * (len - i));
/* Insert new pi */
*((int *)ptr) = pi;
}
}
static int
insert_new_pi_existing_wo(int pi, archer_query_word_occurence *wop,
bow_array *wo_array)
{
int i;
archer_query_word_occurence *wop2 = NULL;
for (i = 0; i < wo_array->length; ++i)
{
wop2 =(archer_query_word_occurence *)bow_array_entry_at_index(wo_array, i);
if (wop->wi == wop2->wi)
{
if (wop->is_li == wop2->is_li)
break;
else
return 0;
}
if (wop->wi < wop2->wi)
return 0;
}
if (i == wo_array->length)
return 0;
insert_pi_into_pi_array(pi, wop2->pi);
return 1;
}
static void
insert_new_wo(archer_query_word_occurence *wop, bow_array *wo_array)
{
void *ptr;
int i, len = wo_array->length;
int wosz = sizeof(archer_query_word_occurence);
archer_query_word_occurence *wop2;
for (i = 0; i < len; ++i)
{
wop2 = (archer_query_word_occurence*)bow_array_entry_at_index(wo_array, i);
if (wop2->wi > wop->wi || (wop2->wi == wop->wi && !wop2->is_li))
break;
}
/* i is now the appropriate index for wop */
if (i == len)
bow_array_append(wo_array, wop);
else
{
/* Make space for a new entry */
bow_array_append(wo_array, wop);
/* Shift contents up one space */
ptr = bow_array_entry_at_index(wo_array, i);
memmove(ptr + wosz, ptr, wosz * (len - i));
/* Insert new pi */
memcpy(ptr, wop, wosz);
}
}
static void
single_term_result(archer_query_term *term, int di, int pi, bow_array **table)
{
archer_query_word_occurence wo;
if (!table[di])
table[di] = bow_array_new(8, sizeof(archer_query_word_occurence),
archer_query_array_free_wo);
if (term->word)
{
wo.is_li = 0;
wo.wi = bow_word2int_no_add (term->word);
}
else
{
archer_label *lp =
bow_sarray_entry_at_keystr(archer_labels, term->labels->string);
wo.is_li = 1;
wo.wi = lp->li;
}
wo.weight = term->weight;
wo.term = term;
if (insert_new_pi_existing_wo(pi, &wo, table[di]))
return;
wo.pi = bow_array_new(32, sizeof(int), NULL);
bow_array_append(wo.pi, &pi);
insert_new_wo(&wo, table[di]);
}
/* recursively assembles the bow_array of results that satisfies all the
proximity constraints of the linked list `term' passed. pi_arrays should
contain the positions of the terms (e.g. pi_arrays[0] contains the
positions of term->proximity->term, pi_arrays[0] contains those of
term->proximity->term->proximity->term, etc.)
possibly some kind of DP approach would be better here; this is going to
make a lot of redundant calls i think */
static int
search_recursive (archer_query_term * term, int di, int pi,
bow_array ** pi_arrays, bow_array **table)
{
int i, opi, prox, ret, good = 0;
if (term == NULL)
return 1;
/* no recursion needed; just return a single-element bow_array for term */
else if (term->proximity == NULL)
{
if (table)
single_term_result(term, di, pi, table);
return 1;
}
else
{
ret = 0;
for (i = 0; i < pi_arrays[0]->length; i++)
{
opi = *((int *)bow_array_entry_at_index(pi_arrays[0], i));
prox = term->proximity->proximity;
switch (term->proximity->position)
{
case ARCHER_QUERY_PTERM_BEFORE :
good = opi > pi && opi - pi <= prox;
break;
case ARCHER_QUERY_PTERM_AFTER :
good = pi > opi && pi - opi <= prox;
break;
case ARCHER_QUERY_PTERM_WITHIN :
good = ABS(pi - opi) <= prox;
break;
}
if (good &&
search_recursive(term->proximity->term, di, opi, &pi_arrays[1],
table))
{
if (table)
{
ret = 1;
single_term_result(term, di, pi, table);
}
else
return 1;
}
}
}
return ret;
}
#define good_di(table, di, exclude) \
((table) == NULL || \
((table[di]) && !(exclude)) || \
(!(table[di]) && (exclude)))
/* next_term_di advances to next di in which all term components co-occur.
Does not check that proximity constraints are satisfied. The third
argument, if provided, is a shortlist; exclude tells whether to use it
as an exclusion or inclusion list */
static int
next_term_di(int current_di, bow_index *index, bow_array **table, int exclude,
archer_query_term *term)
{
archer_query_term *cterm = term;
++current_di;
while (1)
{
while (cterm)
{
int di = scan_to_di(index, cterm, current_di);
if (di == -1)
return -1;
if (di == current_di)
{
/* move on to the next term, recording this term's file pos */
cterm = cterm->proximity ? cterm->proximity->term : NULL;
}
else /* Try the next di */
{
current_di = di == current_di ? di + 1 : di;
cterm = term;
}
}
/*
This is a little inaccurate, since the terms may co-occur
without satisfying proximity constraints. Done this way to
avoid the cost of verifying prox constraints unnecessarily.
*/
term->head->df++;
if (good_di(table, current_di, exclude))
break;
++current_di;
cterm = term;
}
return current_di;
}
static int
satisfies_proximity_constraints(int di, bow_index *index, archer_query_term *term)
{
int i, j, pi, satisfies;
int num_terms = list_length(term);
bow_array *pi_arrays[num_terms];
archer_query_term *cterm;
for (cterm = term, i = 0;
cterm;
cterm = cterm->proximity ? cterm->proximity->term : NULL, ++i)
pi_arrays[i] = archer_query_index_current_pis(index, cterm);
satisfies = 0;
for (j = 0; j < pi_arrays[0]->length; ++j)
{
pi = *((int *)bow_array_entry_at_index(pi_arrays[0], j));
if (search_recursive(term, di, pi, &pi_arrays[1], NULL))
{
satisfies = 1;
break;
}
}
while (--i >= 0) bow_array_free(pi_arrays[i]);
return satisfies;
}
static int
add_if_satisfies_proximity_constraints(int di, bow_index *index,
archer_query_term *term,
bow_array **table)
{
int i, j, pi, ret = 0;
int num_terms = list_length(term);
bow_array *pi_arrays[num_terms];
archer_query_term *cterm;
for (cterm = term, i = 0;
cterm;
cterm = cterm->proximity ? cterm->proximity->term : NULL, ++i)
pi_arrays[i] = archer_query_index_current_pis(index, cterm);
for (j = 0; j < pi_arrays[0]->length; ++j)
{
pi = *((int *)bow_array_entry_at_index(pi_arrays[0], j));
if (search_recursive(term, di, pi, &pi_arrays[1], table))
ret = 1;
}
while (--i >= 0) bow_array_free(pi_arrays[i]);
return ret;
}
static inline void
delete_intervening_entries(bow_array **table, int lastdi, int di)
{
int i;
for (i = lastdi + 1; i < di; ++i)
if (table[i])
{
bow_array_free(table[i]);
table[i] = NULL;
}
}
static void
search_restrict(bow_index *index, bow_array **table, archer_query_term *term,
int exclude)
{
int di, lastdi;
int len = archer_docs->array->length;
archer_query_index_reset (index);
di = next_term_di(-1, index, (exclude ? table : NULL), 0, term);
lastdi = -1;
while (di != -1)
{
if (satisfies_proximity_constraints(di, index, term))
{
if (exclude)
{
assert(table[di]);
bow_array_free(table[di]);
table[di] = NULL;
}
else /* Delete all intervening docs that didn't match */
{
delete_intervening_entries(table, lastdi, di);
if (table[di])
add_if_satisfies_proximity_constraints(di, index, term, table);
lastdi = di;
}
}
di = next_term_di(di, index, (exclude ? table : NULL), 0, term);
}
delete_intervening_entries(table, lastdi, len);
}
static void
search (bow_index *index, bow_array **table, archer_query_term *term,
bow_array **shortlist, int exclude)
{
int len, di;
len = archer_docs->array->length;
archer_query_index_reset(index);
di = next_term_di(-1, index, shortlist, exclude, term);
while (di != -1)
{
add_if_satisfies_proximity_constraints(di, index, term, table);
di = next_term_di(di, index, shortlist, exclude, term);
}
}
/*
fill in the `score' elements of a bow_array of results.
Score used: tfidf(w) = tf(w) * log(|D| / df(w))
*/
static void
calculate_tfidf (bow_index * index, bow_array * array)
{
extern bow_sarray *archer_docs;
int doccount = archer_docs->array->length;
int i;
for (i = 0; i < array->length; i++)
{
archer_query_result *current;
int j;
current = (archer_query_result *) bow_array_entry_at_index (array, i);
current->score = 0.0;
for (j = 0; j < current->wo->length; j++)
{
archer_query_word_occurence *current_wo;
current_wo = (archer_query_word_occurence *)
bow_array_entry_at_index (current->wo, j);
if (current_wo->term->head->idf < 0.0)
current_wo->term->head->idf =
log(((double)doccount) / ((double) current_wo->term->head->df));
current->score +=
current_wo->pi->length *
current_wo->weight *
current_wo->term->head->idf;
}
}
}
/*
Set all member terms in a proximity list to point to the first
(for DF calculation).
*/
static void
archer_query_thread(archer_query_term *term)
{
archer_query_term *pterm;
while (term)
{
for (pterm = term;
pterm;
pterm = pterm->proximity ? pterm->proximity->term : NULL)
pterm->head = term;
term = term->next;
}
}
bow_array *
archer_query_execute (bow_index * index, archer_query_info * query)
{
int exclude = 0;
archer_query_term *term;
bow_array **table = NULL, **shortlist = NULL, *ranking_results = NULL;
if (query)
{
archer_query_doc_restriction = -1;
archer_query_last_query = query;
}
else
query = archer_query_last_query;
archer_query_thread(query->inclusion);
archer_query_thread(query->exclusion);
archer_query_thread(query->ranking);
for (term = query->inclusion; term; term = term->next)
{
if (table)
/* Delete any items in table that _don't_ match term */
search_restrict(index, table, term, 0);
else
{
/* Do an unconstrained search for matching docs */
table = archer_query_table_new();
search(index, table, term, NULL, 0);
}
}
/* If table is non-NULL, we have a short list */
if (query->exclusion)
{
if (query->inclusion)
{
/* We have a short list of documents; delete any with exclusion terms */
for (term = query->exclusion; term; term = term->next)
search_restrict(index, table, term, 1);
}
else
{
/* No short list: Create an exclusion table */
exclude = 1;
table = archer_query_table_new();
for (term = query->exclusion; term; term = term->next)
search(index, table, term, NULL, 0);
}
}
/* If table is non-NULL, it is meant to restrict search in some way.
If exclude = 1, it contains a list of docs to exclude */
if (query->ranking)
{
shortlist = table;
table = (exclude || !shortlist) ?
archer_query_table_new() :
archer_query_table_copy(shortlist);
for (term = query->ranking; term; term = term->next)
search(index, table, term, shortlist, exclude);
if (shortlist)
archer_query_table_free(shortlist);
}
/* This happens when only exclusion terms are given. The right thing
to do in such a case is just to return an empty list */
else if (exclude)
{
/* archer_query_table_invert(table); */
archer_query_table_empty(table);
}
ranking_results = archer_query_table_to_bow_array_with_freeing(table);
calculate_tfidf (index, ranking_results);
return ranking_results;
}
bow_array *
archer_query_repeat_for_document(bow_index *index, int di)
{
if (!archer_query_last_query)
return NULL;
archer_query_doc_restriction = di;
return archer_query_execute(index, NULL);
}
|