/*  Copyright (C) 2009 Joshua Judson Rosen <rozzin@geekspace.com>.

    This program is free software: you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    version 3 as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING. If not, see
    <http://www.gnu.org/licenses/> or write to:

        The Free Software Foundation, inc.
        51 Franklin Street, Fifth Floor
        Boston, MA 02110-1301
        USA
*/

#include "levenshtein.h"
#include "weights.h"

#include <stdio.h>

static float
weight_normalised (const char *s0, int s0_len, int pos0,
                   const char *s1, int s1_len, int pos1,
                   levenshtein_op op)
{
  /* Weight all operations equivalently,
     relative to a worst-case total cost of 1.0:
  */

  float cost;

  if (op == LEVENSHTEIN_SUB) {
    cost = 2.0;
  } else {
    cost = 1.0;
  }

  return cost / (float) (s0_len + s1_len);
}

static float
weight_absolute (const char *s0, int s0_len, int pos0,
                 const char *s1, int s1_len, int pos1,
                 levenshtein_op op)
{
  /* Weight all operations equivalently,
     at 1.0 so that adding the weights
     simply counts the ops:
   */

  return 1.0;
}

float
weight_linear_normal (const char *s0, int s0_len, int pos0,
                      const char *s1, int s1_len, int pos1,
                      levenshtein_op op)
{
/* Assign a weighted value to a given operation
   dependant on the position of the op in each string--
   characters increase linearly in weight
   from the tail end of each string:
*/

  float cost0 = s0_len - pos0; /* ranging 1..s0_len,
                                  unless s0_len = 0;
                                  in which case cost0 = 0. */
  float cost1 = s1_len - pos1; /* ranging 1..s1_len,
                                  unless s1_len = 0; */
  float total_cost = cost0 + cost1;

  float max_cost = (1 + s0_len) * s0_len/2
                   + (1 + s1_len) * s1_len/2;

  printf ("max_cost = %g\n", max_cost);

  switch (op) {
  case LEVENSHTEIN_DEL:
    printf ("%c", s0[pos0]);
    break;
  default:
    printf ("%c", s1[pos1]);
  }

  switch (op) {
  case LEVENSHTEIN_NON:
    return 0;
  case LEVENSHTEIN_DEL:
    total_cost = cost0; break;
  case LEVENSHTEIN_INS:
    total_cost = cost1; break;
    /* Inserted/deleted characters occur in only 1 string (of two),
       so count them only 1/2 as strongly: */
  default:
    break;
  }
  printf ("[%d,%d]", pos0, pos1); /* XXX need to normalise: */
  printf ("((%d-%d=>%g) + (%d-%d=>%g) = %g)",
          s0_len, pos0, cost0,
          s1_len, pos1, cost1,
          total_cost);
  return total_cost / max_cost;
}

