+ double target = 1.0 / rp->record->position;
+ double normscore = rp->score * a + b;
+ double diff = target - normscore;
+ sum += diff * diff;
+ }
+ return sum;
+}
+
+// For each client, normalize scores
+static void normalize_scores(struct relevance *rel)
+{
+ const int maxiterations = 100;
+ const double enough = 100.0; // sets the number of decimals we are happy with
+ struct norm_client *norm;
+ for ( norm = rel->norm; norm; norm = norm->next )
+ {
+ yaz_log(YLOG_LOG,"Normalizing client %d: scorefield=%d count=%d",
+ norm->num, norm->scorefield, norm->count);
+ norm->a = 1.0; // default normalizing factors, no change
+ norm->b = 0.0;
+ if ( norm->scorefield != scorefield_none &&
+ norm->scorefield != scorefield_position )
+ { // have something to normalize
+ double range = norm->max - norm->min;
+ int it = 0;
+ double a,b; // params to optimize
+ double as,bs; // step sizes
+ double chi;
+ char dir = 'a';
+ // initial guesses for the parameters
+ if ( range < 1e-6 ) // practically zero
+ range = norm->max;
+ a = 1.0 / range;
+ b = abs(norm->min);
+ as = a / 10;
+ bs = b / 10;
+ chi = squaresum( norm->records, a,b);
+ while (it++ < maxiterations) // safeguard against things not converging
+ {
+ double aplus = squaresum(norm->records, a+as, b);
+ double aminus= squaresum(norm->records, a-as, b);
+ double bplus = squaresum(norm->records, a, b+bs);
+ double bminus= squaresum(norm->records, a, b-bs);
+ if ( aplus < chi && aplus < aminus && aplus < bplus && aplus < bminus)
+ {
+ a = a + as;
+ chi = aplus;
+ yaz_log(YLOG_LOG,"Fitting aplus it=%d: a=%f / %f b=%f / %f chi = %f",
+ it, a, as, b, bs, chi );
+ }
+ else if ( aminus < chi && aminus < aplus && aminus < bplus && aminus < bminus)
+ {
+ a = a - as;
+ chi = aminus;
+ yaz_log(YLOG_LOG,"Fitting aminus it=%d: a=%f / %f b=%f / %f chi = %f",
+ it, a, as, b, bs, chi );
+ }
+ else if ( bplus < chi && bplus < aplus && bplus < aminus && bplus < bminus)
+ {
+ b = b + bs;
+ chi = bplus;
+ yaz_log(YLOG_LOG,"Fitting bplus it=%d: a=%f / %f b=%f / %f chi = %f",
+ it, a, as, b, bs, chi );
+ }
+ else if ( bminus < chi && bminus < aplus && bminus < bplus && bminus < aminus)
+ {
+ b = b - bs;
+ chi = bminus;
+ yaz_log(YLOG_LOG,"Fitting bminus it=%d: a=%f / %f b=%f / %f chi = %f",
+ it, a, as, b, bs, chi );
+ }
+ else
+ {
+ if ( as > bs )
+ {
+ as = as / 2;
+ yaz_log(YLOG_LOG,"Fitting step a it=%d: a=%f / %f b=%f / %f chi = %f",
+ it, a, as, b, bs, chi );
+ }
+ else
+ {
+ bs = bs / 2;
+ yaz_log(YLOG_LOG,"Fitting step b it=%d: a=%f / %f b=%f / %f chi = %f",
+ it, a, as, b, bs, chi );
+ }
+ }
+ norm->a = a;
+ norm->b = b;
+ if ( fabs(as) * enough < fabs(a) &&
+ fabs(bs) * enough < fabs(b) ) {
+ yaz_log(YLOG_LOG,"Fitting done: stopping loop at %d" , it );
+ break; // not changing much any more
+
+ }
+ }
+ yaz_log(YLOG_LOG,"Fitting done: it=%d: a=%f / %f b=%f / %f chi = %f",
+ it-1, a, as, b, bs, chi );
+ yaz_log(YLOG_LOG," a: %f < %f %d",
+ fabs(as)*enough, fabs(a), (fabs(as) * enough < fabs(a)) );
+ yaz_log(YLOG_LOG," b: %f < %f %d",
+ fabs(bs)*enough, fabs(b), (fabs(bs) * enough < fabs(b)) );
+ }
+
+ if ( norm->scorefield != scorefield_none )
+ { // distribute the normalized scores to the records
+ struct norm_record *nr = norm->records;
+ for ( ; nr ; nr = nr->next ) {
+ double r = nr->score;
+ r = norm->a * r + norm -> b;
+ nr->clust->relevance_score = 10000 * r;
+ yaz_log(YLOG_LOG,"Normalized %f * %f + %f = %f",
+ nr->score, norm->a, norm->b, r );
+ // TODO - This keeps overwriting the cluster score in random order!
+ // Need to merge results better
+ }
+
+ }
+
+ } // client loop
+}
+
+
+static struct word_entry *word_entry_match(struct relevance *r,
+ const char *norm_str,
+ const char *rank, int *weight)
+{
+ int i = 1;
+ struct word_entry *entries = r->entries;
+ for (; entries; entries = entries->next, i++)
+ {
+ if (*norm_str && !strcmp(norm_str, entries->norm_str))
+ {
+ const char *cp = 0;
+ int no_read = 0;
+ sscanf(rank, "%d%n", weight, &no_read);
+ rank += no_read;
+ while (*rank == ' ')
+ rank++;
+ if (no_read > 0 && (cp = strchr(rank, ' ')))
+ {
+ if ((cp - rank) == strlen(entries->ccl_field) &&
+ memcmp(entries->ccl_field, rank, cp - rank) == 0)
+ *weight = atoi(cp + 1);
+ }
+ return entries;
+ }