a23b4c2001-05-25Per Hedbor #include <math.h>
7502512001-05-22Per Hedbor #include "global.h" #include "stralloc.h" #include "global.h"
3757912001-05-31Johan Schön RCSID("$Id: whitefish.c,v 1.28 2001/05/31 00:33:44 js Exp $");
7502512001-05-22Per Hedbor #include "pike_macros.h" #include "interpret.h" #include "program.h" #include "program_id.h" #include "object.h" #include "operators.h"
4ad07e2001-05-22Per Hedbor #include "array.h" #include "module_support.h"
7502512001-05-22Per Hedbor 
0853842001-05-22Per Hedbor #include "config.h"
7502512001-05-22Per Hedbor #include "whitefish.h" #include "resultset.h"
4926f72001-05-22Per Hedbor #include "blob.h"
54025d2001-05-26Per Hedbor #include "blobs.h"
9157232001-05-28Per Hedbor #include "linkfarm.h"
7502512001-05-22Per Hedbor  /* must be included last */ #include "module_magic.h"
4ad07e2001-05-22Per Hedbor struct tofree { Blob **blobs;
a23b4c2001-05-25Per Hedbor  Blob **tmp;
4ad07e2001-05-22Per Hedbor  int nblobs; struct object *res; };
6aa8ef2001-05-23Per Hedbor static void free_stuff( void *_t )
4ad07e2001-05-22Per Hedbor { struct tofree *t= (struct tofree *)_t; int i; if( t->res ) free_object( t->res ); for( i = 0; i<t->nblobs; i++ ) wf_blob_free( t->blobs[i] );
a23b4c2001-05-25Per Hedbor  free( t->tmp );
4ad07e2001-05-22Per Hedbor  free( t ); }
a23b4c2001-05-25Per Hedbor #define OFFSET(X) \
cce09d2001-05-25Per Hedbor  (X.type == HIT_BODY?X.u.body.pos:X.type==HIT_FIELD?(X.u.field.pos):(X.u.anchor.pos))
a23b4c2001-05-25Per Hedbor 
7a08b42001-05-25Per Hedbor static int _distance_f( int distance ) { if( distance < 2 ) return 0; if( distance < 6 ) return 1; if( distance < 11 ) return 2; if( distance < 22 ) return 3; if( distance < 42 ) return 4; if( distance < 82 ) return 5; if( distance < 161 ) return 6; return 7; } #define DOFF(X) _distance_f(X)
cce09d2001-05-25Per Hedbor #define MOFF(X) (X.type==HIT_BODY?0:X.type==HIT_FIELD?X.u.field.type+2:1)
a23b4c2001-05-25Per Hedbor  static void handle_hit( Blob **blobs, int nblobs, struct object *res,
5cdc2c2001-05-25Per Hedbor  int docid,
6ac4092001-05-25Johan Schön  double *field_c[66],
a633972001-05-25Per Hedbor  double *prox_c[8],
73e9422001-05-28Per Hedbor  double mc, double mp, int cutoff )
a23b4c2001-05-25Per Hedbor { int i, j, k, end = 0; Hit *hits = malloc( nblobs * sizeof(Hit) ); unsigned char *nhits = malloc( nblobs ); unsigned char *pos = malloc( nblobs );
6ac4092001-05-25Johan Schön  int matrix[66][8];
a23b4c2001-05-25Per Hedbor 
a633972001-05-25Per Hedbor  MEMSET(matrix, 0, sizeof(matrix) );
a23b4c2001-05-25Per Hedbor  MEMSET(hits, 0, nblobs * sizeof(Hit) ); MEMSET(pos, 0, nblobs ); for( i = 0; i<nblobs; i++ ) nhits[i] = wf_blob_nhits( blobs[i] ); for( i = 0; i<nblobs; i++ ) { MEMSET( pos, 0, nblobs ); for( j = 0; j<nhits[i]; j++ ) { hits[i] = wf_blob_hit( blobs[i], j );
a1a4882001-05-31Johan Schön  matrix[MOFF(hits[i])][3]++;
ccfc832001-05-25Per Hedbor 
a23b4c2001-05-25Per Hedbor  /* forward the other positions */ for( k = 0; k<nblobs; k++ )
a633972001-05-25Per Hedbor  if( k != i && pos[ k ] < nhits[ k ] )
a23b4c2001-05-25Per Hedbor  {
cce09d2001-05-25Per Hedbor  while( (hits[k].raw < hits[i].raw) && (pos[ k ] < nhits[ k ]))
a23b4c2001-05-25Per Hedbor  hits[k] = wf_blob_hit( blobs[k], pos[k]++ );
07a8a02001-05-25Per Hedbor  if( (pos[ k ] < nhits[ k ]) && hits[k].type == hits[i].type )
73e9422001-05-28Per Hedbor  matrix[MOFF(hits[i])][DOFF(OFFSET(hits[k])-OFFSET(hits[i]))]+=4;
a23b4c2001-05-25Per Hedbor  } } }
ccfc832001-05-25Per Hedbor  free( pos ); free( nhits ); free( hits );
a23b4c2001-05-25Per Hedbor  /* Now we have our nice matrix. Time to do some multiplication */
5cdc2c2001-05-25Per Hedbor  { double accum = 0.0, fc, pc; int accum_i;
6ac4092001-05-25Johan Schön  for( i = 0; i<66; i++ )
a633972001-05-25Per Hedbor  if( (fc = (*field_c)[i]) != 0.0 )
5cdc2c2001-05-25Per Hedbor  for( j = 0; j<8; j++ )
a633972001-05-25Per Hedbor  if( (pc = (*prox_c)[j]) != 0.0 )
73e9422001-05-28Per Hedbor  accum += (MINIMUM(matrix[i][j],cutoff)*fc*pc) / (mc*mp);
5cdc2c2001-05-25Per Hedbor  /* Limit */ if( accum > 32000.0 ) accum = 32000.0;
a633972001-05-25Per Hedbor  accum_i = (int)(accum *100 );
5cdc2c2001-05-25Per Hedbor  if( accum_i > 0 ) wf_resultset_add( res, docid, accum_i ); }
a23b4c2001-05-25Per Hedbor }
fdc4662001-05-31Johan Schön static struct object *low_do_query_or( Blob **blobs,
4ad07e2001-05-22Per Hedbor  int nblobs,
6ac4092001-05-25Johan Schön  double field_c[66],
73e9422001-05-28Per Hedbor  double prox_c[8], int cutoff)
4ad07e2001-05-22Per Hedbor { struct object *res = wf_resultset_new(); struct tofree *__f = malloc( sizeof( struct tofree ) );
a633972001-05-25Per Hedbor  double max_c=0.0, max_p=0.0;
4ad07e2001-05-22Per Hedbor  ONERROR e;
ccfc832001-05-25Per Hedbor  int i, j;
a23b4c2001-05-25Per Hedbor  Blob **tmp; tmp = malloc( nblobs * sizeof( Blob *) );
4ad07e2001-05-22Per Hedbor  __f->res = res; __f->blobs = blobs; __f->nblobs = nblobs;
a23b4c2001-05-25Per Hedbor  __f->tmp = tmp;
4ad07e2001-05-22Per Hedbor  SET_ONERROR( e, free_stuff, __f );
3693062001-05-22Per Hedbor 
a633972001-05-25Per Hedbor  for( i = 0; i<66; i++ ) if( field_c[i] > max_c ) max_c = field_c[i]; for( i = 0; i<8; i++ ) if( prox_c[i] > max_p ) max_p = prox_c[i];
a23b4c2001-05-25Per Hedbor 
a633972001-05-25Per Hedbor  if( max_p != 0.0 && max_c != 0.0 )
a23b4c2001-05-25Per Hedbor  {
a633972001-05-25Per Hedbor  /* Time to do the real work. :-) */ for( i = 0; i<nblobs; i++ ) /* Forward to first element */ wf_blob_next( blobs[i] ); /* Main loop: Find the smallest element in the blob array. */
ccfc832001-05-25Per Hedbor  while( 1 )
a633972001-05-25Per Hedbor  {
740b062001-05-28Johan Schön  unsigned int min = 0x7fffffff;
a23b4c2001-05-25Per Hedbor 
a633972001-05-25Per Hedbor  for( i = 0; i<nblobs; i++ ) if( !blobs[i]->eof && ((unsigned int)blobs[i]->docid) < min ) min = blobs[i]->docid;
3693062001-05-22Per Hedbor 
740b062001-05-28Johan Schön  if( min == 0x7fffffff )
a633972001-05-25Per Hedbor  break; for( j = 0, i = 0; i < nblobs; i++ ) if( blobs[i]->docid == min && !blobs[i]->eof ) tmp[j++] = blobs[i];
ccfc832001-05-25Per Hedbor 
73e9422001-05-28Per Hedbor  handle_hit( tmp, j, res, min, &field_c, &prox_c, max_c, max_p, cutoff );
a23b4c2001-05-25Per Hedbor 
a633972001-05-25Per Hedbor  for( i = 0; i<j; i++ ) wf_blob_next( tmp[i] ); }
a23b4c2001-05-25Per Hedbor  } /* Free workarea and return the result. */
3693062001-05-22Per Hedbor 
4ad07e2001-05-22Per Hedbor  UNSET_ONERROR( e ); __f->res = 0; free_stuff( __f ); return res; }
ccfc832001-05-25Per Hedbor static void handle_phrase_hit( Blob **blobs, int nblobs, struct object *res, int docid, double *field_c[66], double mc ) { int i, j, k;
a5039e2001-05-30Per Hedbor  unsigned char *nhits = malloc( nblobs*2 ); unsigned char *first = nhits+nblobs;
ccfc832001-05-25Per Hedbor  int matrix[66]; double accum = 0.0; MEMSET(matrix, 0, sizeof(matrix) );
a5039e2001-05-30Per Hedbor 
ccfc832001-05-25Per Hedbor  for( i = 0; i<nblobs; i++ )
a5039e2001-05-30Per Hedbor  {
ccfc832001-05-25Per Hedbor  nhits[i] = wf_blob_nhits( blobs[i] );
a5039e2001-05-30Per Hedbor  first[i] = 0; }
ccfc832001-05-25Per Hedbor 
a5039e2001-05-30Per Hedbor  for( i = 0; i<nhits[0]; i++)
ccfc832001-05-25Per Hedbor  { double add;
a5039e2001-05-30Per Hedbor  int hit = 1; Hit m = wf_blob_hit( blobs[0], i ); int h = m.raw; if( (add = (*field_c)[ MOFF(m) ]) == 0.0 ) continue; for( j = 1; j<nblobs; j++) for( k = first[j]; k<nhits[j]; k++ )
ccfc832001-05-25Per Hedbor  {
a5039e2001-05-30Per Hedbor  int h2 = wf_blob_hit_raw( blobs[j], k ); if( h2 > h )
ccfc832001-05-25Per Hedbor  {
a5039e2001-05-30Per Hedbor  first[j]=k; if( h2-j == h ) hit++; break;
ccfc832001-05-25Per Hedbor  } }
a5039e2001-05-30Per Hedbor  if( hit == nblobs ) accum += add/mc;
ccfc832001-05-25Per Hedbor  } free( nhits );
3757912001-05-31Johan Schön  if( accum > 0.0 )
ccfc832001-05-25Per Hedbor  wf_resultset_add( res, docid, (int)(accum*100) ); } static struct object *low_do_query_phrase( Blob **blobs, int nblobs, double field_c[66]) { struct object *res = wf_resultset_new(); struct tofree *__f = malloc( sizeof( struct tofree ) ); double max_c=0.0; ONERROR e; int i, j; __f->blobs = blobs; __f->nblobs = nblobs; __f->res = res; __f->tmp = 0; SET_ONERROR( e, free_stuff, __f ); for( i = 0; i<66; i++ ) if( field_c[i] > max_c ) max_c = field_c[i]; if( max_c != 0.0 ) { /* Time to do the real work. :-) */ for( i = 0; i<nblobs; i++ ) /* Forward to first element */ wf_blob_next( blobs[i] ); /* Main loop: Find the smallest element in the blob array. */ while( 1 ) {
740b062001-05-28Johan Schön  unsigned int min = 0x7fffffff;
ccfc832001-05-25Per Hedbor  for( i = 0; i<nblobs; i++ ) if( blobs[i]->eof ) goto end; else if( ((unsigned int)blobs[i]->docid) < min ) min = blobs[i]->docid;
740b062001-05-28Johan Schön  if( min == 0x7fffffff )
ccfc832001-05-25Per Hedbor  goto end; for( j = 0, i = 0; i < nblobs; i++ ) if( blobs[i]->docid != min ) goto next; handle_phrase_hit( blobs, nblobs, res, min, &field_c, max_c ); next: for( i = 0; i<nblobs; i++ ) if( blobs[i]->docid == min ) wf_blob_next( blobs[i] ); } } end: /* Free workarea and return the result. */ UNSET_ONERROR( e ); __f->res = 0; free_stuff( __f ); return res; }
73e9422001-05-28Per Hedbor static struct object *low_do_query_and( Blob **blobs, int nblobs, double field_c[66], double prox_c[8], int cutoff) { struct object *res = wf_resultset_new(); struct tofree *__f = malloc( sizeof( struct tofree ) ); double max_c=0.0, max_p=0.0; ONERROR e; int i, j; __f->blobs = blobs; __f->nblobs = nblobs; __f->res = res; __f->tmp = 0; SET_ONERROR( e, free_stuff, __f ); for( i = 0; i<66; i++ ) if( field_c[i] > max_c ) max_c = field_c[i]; for( i = 0; i<8; i++ ) if( prox_c[i] > max_p ) max_p = prox_c[i]; if( max_c != 0.0 ) { /* Time to do the real work. :-) */ for( i = 0; i<nblobs; i++ ) /* Forward to first element */ wf_blob_next( blobs[i] ); /* Main loop: Find the smallest element in the blob array. */ while( 1 ) {
740b062001-05-28Johan Schön  unsigned int min = 0x7fffffff;
73e9422001-05-28Per Hedbor  for( i = 0; i<nblobs; i++ ) if( blobs[i]->eof ) goto end; else if( ((unsigned int)blobs[i]->docid) < min ) min = blobs[i]->docid;
740b062001-05-28Johan Schön  if( min == 0x7fffffff )
73e9422001-05-28Per Hedbor  goto end; for( j = 0, i = 0; i < nblobs; i++ ) if( blobs[i]->docid != min ) goto next; handle_hit( blobs, nblobs, res, min, &field_c,&prox_c, max_c,max_p, cutoff ); next: for( i = 0; i<nblobs; i++ ) if( blobs[i]->docid == min ) wf_blob_next( blobs[i] ); } } end: /* Free workarea and return the result. */ UNSET_ONERROR( e ); __f->res = 0; free_stuff( __f ); return res; }
ccfc832001-05-25Per Hedbor  static void f_do_query_phrase( INT32 args ) /*! @decl ResultSet do_query_phrase( array(int) words, @ *! array(int) field_coefficients, @ *! function(int:string) blobfeeder) *! @[words] *! *! Arrays of word ids. Note that the order is significant *! for the ranking. *! *! @[field_coefficients] *! *! An array of ranking coefficients for the different fields. *! In the range of [0x0000-0xffff]. The array (always) has 66 *! elements: *! *! Index Coefficient for field *! ----- --------------------- *! 0 body *! 1 anchor *! 2..65 Special field 0..63 *! *! @[blobfeeder] *! *! This function returns a Pike string containing the word hits *! for a certain word_id. Call repeatedly until it returns 0. */ { double proximity_coefficients[8]; double field_coefficients[66]; int numblobs, i; Blob **blobs; struct svalue *cb; struct object *res; struct array *_words, *_field; /* 1: Get all arguments. */ get_all_args( "do_query_merge", args, "%a%a%*", &_words, &_field, &cb); if( _field->size != 66 ) Pike_error("Illegal size of field_coefficients array (expected 66)\n" ); numblobs = _words->size; if( !numblobs ) { struct object *o = wf_resultset_new( ); pop_n_elems( args ); push_object( o ); return; } blobs = malloc( sizeof(Blob *) * numblobs ); for( i = 0; i<numblobs; i++ ) blobs[i] = wf_blob_new( cb, _words->item[i].u.integer ); for( i = 0; i<66; i++ ) field_coefficients[i] = (double)_field->item[i].u.integer; res = low_do_query_phrase(blobs,numblobs, field_coefficients ); pop_n_elems( args ); push_object( res ); }
7502512001-05-22Per Hedbor 
73e9422001-05-28Per Hedbor static void f_do_query_and( INT32 args ) /*! @decl ResultSet do_query_merge( array(int) words, @ *! array(int) field_coefficients, @ *! array(int) proximity_coefficients, @ *! function(int:string) blobfeeder) *! @[words] *! *! Arrays of word ids. Note that the order is significant *! for the ranking. *! *! @[field_coefficients] *! *! An array of ranking coefficients for the different fields. *! In the range of [0x0000-0xffff]. The array (always) has 66 *! elements: *! *! Index Coefficient for field *! ----- --------------------- *! 0 body *! 1 anchor *! 2..65 Special field 0..63 *! *! @[proximity_coefficients] *! *! An array of ranking coefficients for the different *! proximity categories. Always has 8 elements, in the range *! of [0x0000-0xffff]. *! *! Index Meaning *! ----- ------- *! 0 spread: 0 (Perfect hit) *! 1 spread: 1-5 *! 2 spread: 6-10 *! 3 spread: 11-20 *! 4 spread: 21-40 *! 5 spread: 41-80 *! 6 spread: 81-160 *! 7 spread: 161- *! *! The 'spread' value should be defined somehow. *! *! @[blobfeeder] *! *! This function returns a Pike string containing the word hits *! for a certain word_id. Call repeatedly until it returns 0. */ { double proximity_coefficients[8]; double field_coefficients[66]; int numblobs, i, cutoff; Blob **blobs; struct svalue *cb; struct object *res; struct array *_words, *_field, *_prox; /* 1: Get all arguments. */ get_all_args( "do_query_and", args, "%a%a%a%d%*", &_words, &_field, &_prox, &cutoff, &cb); if( _field->size != 66 ) Pike_error("Illegal size of field_coefficients array (expected 66)\n" ); if( _prox->size != 8 ) Pike_error("Illegal size of proximity_coefficients array (expected 8)\n" ); numblobs = _words->size; if( !numblobs ) { struct object *o = wf_resultset_new( ); pop_n_elems( args ); push_object( o ); return; } blobs = malloc( sizeof(Blob *) * numblobs ); for( i = 0; i<numblobs; i++ ) blobs[i] = wf_blob_new( cb, _words->item[i].u.integer ); for( i = 0; i<8; i++ ) proximity_coefficients[i] = (double)_prox->item[i].u.integer; for( i = 0; i<66; i++ ) field_coefficients[i] = (double)_field->item[i].u.integer; res = low_do_query_and(blobs,numblobs, field_coefficients, proximity_coefficients, cutoff ); pop_n_elems( args ); push_object( res ); }
1b89a82001-05-29Johan Schön static void f_do_query_or( INT32 args ) /*! @decl ResultSet do_query_or( array(int) words, @ *! array(int) field_coefficients, @ *! array(int) proximity_coefficients, @ *! function(int:string) blobfeeder)
6ac4092001-05-25Johan Schön  *! @[words]
4926f72001-05-22Per Hedbor  *! *! Arrays of word ids. Note that the order is significant *! for the ranking. *! *! @[field_coefficients] *! *! An array of ranking coefficients for the different fields.
6ac4092001-05-25Johan Schön  *! In the range of [0x0000-0xffff]. The array (always) has 66
4926f72001-05-22Per Hedbor  *! elements: *! *! Index Coefficient for field *! ----- ---------------------
6ac4092001-05-25Johan Schön  *! 0 body *! 1 anchor *! 2..65 Special field 0..63
4926f72001-05-22Per Hedbor  *! *! @[proximity_coefficients] *! *! An array of ranking coefficients for the different *! proximity categories. Always has 8 elements, in the range *! of [0x0000-0xffff]. *! *! Index Meaning *! ----- ------- *! 0 spread: 0 (Perfect hit) *! 1 spread: 1-5 *! 2 spread: 6-10 *! 3 spread: 11-20 *! 4 spread: 21-40 *! 5 spread: 41-80 *! 6 spread: 81-160 *! 7 spread: 161- *! *! The 'spread' value should be defined somehow. *! *! @[blobfeeder] *! *! This function returns a Pike string containing the word hits *! for a certain word_id. Call repeatedly until it returns 0. */ {
5cdc2c2001-05-25Per Hedbor  double proximity_coefficients[8];
6ac4092001-05-25Johan Schön  double field_coefficients[66];
73e9422001-05-28Per Hedbor  int numblobs, i, cutoff;
4ad07e2001-05-22Per Hedbor  Blob **blobs; struct svalue *cb;
a23b4c2001-05-25Per Hedbor  struct object *res;
4ad07e2001-05-22Per Hedbor  struct array *_words, *_field, *_prox; /* 1: Get all arguments. */
73e9422001-05-28Per Hedbor  get_all_args( "do_query_merge", args, "%a%a%a%d%*", &_words, &_field, &_prox, &cutoff, &cb);
4ad07e2001-05-22Per Hedbor 
6ac4092001-05-25Johan Schön  if( _field->size != 66 ) Pike_error("Illegal size of field_coefficients array (expected 66)\n" );
4ad07e2001-05-22Per Hedbor  if( _prox->size != 8 ) Pike_error("Illegal size of proximity_coefficients array (expected 8)\n" ); numblobs = _words->size; if( !numblobs ) { struct object *o = wf_resultset_new( ); pop_n_elems( args ); push_object( o ); return; } blobs = malloc( sizeof(Blob *) * numblobs ); for( i = 0; i<numblobs; i++ ) blobs[i] = wf_blob_new( cb, _words->item[i].u.integer ); for( i = 0; i<8; i++ )
a633972001-05-25Per Hedbor  proximity_coefficients[i] = (double)_prox->item[i].u.integer;
4ad07e2001-05-22Per Hedbor 
6ac4092001-05-25Johan Schön  for( i = 0; i<66; i++ )
a633972001-05-25Per Hedbor  field_coefficients[i] = (double)_field->item[i].u.integer;
4926f72001-05-22Per Hedbor 
fdc4662001-05-31Johan Schön  res = low_do_query_or(blobs,numblobs,
a23b4c2001-05-25Per Hedbor  field_coefficients,
73e9422001-05-28Per Hedbor  proximity_coefficients, cutoff );
a23b4c2001-05-25Per Hedbor  pop_n_elems( args ); push_object( res );
4926f72001-05-22Per Hedbor }
73e9422001-05-28Per Hedbor 
7502512001-05-22Per Hedbor void pike_module_init(void) { init_resultset_program();
20c5972001-05-23Per Hedbor  init_blob_program();
54025d2001-05-26Per Hedbor  init_blobs_program();
9157232001-05-28Per Hedbor  init_linkfarm_program();
4926f72001-05-22Per Hedbor 
1b89a82001-05-29Johan Schön  add_function( "do_query_or", f_do_query_or,
73e9422001-05-28Per Hedbor  "function(array(int),array(int),array(int),int" ",function(int:string):object)", 0 ); add_function( "do_query_and", f_do_query_and, "function(array(int),array(int),array(int),int"
7ce8552001-05-22Per Hedbor  ",function(int:string):object)",
4926f72001-05-22Per Hedbor  0 );
ccfc832001-05-25Per Hedbor  add_function( "do_query_phrase", f_do_query_phrase, "function(array(int),array(int)" ",function(int:string):object)", 0 );
7502512001-05-22Per Hedbor } void pike_module_exit(void) {
0853842001-05-22Per Hedbor  exit_resultset_program();
20c5972001-05-23Per Hedbor  exit_blob_program();
54025d2001-05-26Per Hedbor  exit_blobs_program();
9157232001-05-28Per Hedbor  exit_linkfarm_program();
7502512001-05-22Per Hedbor }