Example #1
0
///
// Version 3: Dynamic programming version
///
int edit_distance3(char *s1, char *s2) {
    int i, j;
    int opt[3];

    for (i = 0; i < MAX; ++i) {
        row_init(i);
        column_init(i);
    }

    for (i = 1; i < strlen(s1); ++i) {
        for (j = 1; j < strlen(s2); ++j) {
            opt[MATCH] = m[i - 1][j - 1].cost + match(s1[i], s2[j]);
            opt[INSERT] = m[i][j - 1].cost + del(s2[j]);
            opt[DELETE] = m[i - 1][j].cost + ins(s1[i]);

            m[i][j].cost = opt[MATCH];
            m[i][j].parent = MATCH;
            for(int k = INSERT; k <= DELETE; ++k) {
                if (opt[k] < m[i][j].cost) {
                    m[i][j].cost = opt[k];
                    m[i][j].parent = k;
                }
            }
        }
    }

    goal_cell(s1, s2, &i, &j);
    return m[i][j].cost;
}
Example #2
0
int string_compare(char *s, char *t)
{
    uint i, j, k;
    int opt[3];

    for (i = 0; i < MAXLEN; i++) {
        row_init(i);
        column_init(i);
    }
    //print_matrix(s, t, TRUE);

    for (i = 1; i < strlen(s); i++) {
        for (j = 1; j < strlen(t); j++) {
            opt[MATCH]  = m[i-1][j-1].cost + match(s[i], t[j]);
            opt[INSERT] = m[i][j-1].cost + indel(t[j]);
            opt[DELETE] = m[i-1][j].cost + indel(s[i]);

            m[i][j].cost = opt[MATCH];
            m[i][j].parent = MATCH;
            for (k = INSERT; k <= DELETE; k++) {
                if (opt[k] < m[i][j].cost) {
                    m[i][j].cost = opt[k];
                    m[i][j].parent = k;
                }
            }
        }
    }

    goal_cell(s, t, &i, &j);
    
    return m[i][j].cost;
} 
int distance(char *s1, char *s2){
    // distance between s1[0..i], 0 is i+1
    int i = 0, j = 0;
    for(; i < MAXLEN; i++){
        row_init(i);
        column_init(i);
    }

    int cost[3];
    for(i = 1; i < strlen(s1); i++){
        for(j = 1; j < strlen(s2); j++){
            cost[0] = ((s1[i] == s2[j]) ? table[i-1][j-1].value : (table[i-1][j-1].value + 1));
            cost[1] = table[i-1][j].value + 1;
            cost[2] = table[i][j-1].value + 1;

            int k = 0, min = cost[0], operation = 0;
            for(; k < 3; k++){
                if(cost[k] < min){
                    min = cost[k];
                    operation = k;
                }
            }
            table[i][j].value = min;
            table[i][j].operation = operation;
        }
    }
    return table[strlen(s1)-1][strlen(s2)-1].value;
}
Example #4
0
int string_compare(char *s, char *t)
{
	int i,j,k;		/* counters */
	int opt[3];		/* cost of the three options */

	for (i=0; i<MAXLEN; i++) {
		row_init(i);
		column_init(i);
	}

	for (i=1; i<strlen(s); i++)
		for (j=1; j<strlen(t); j++) {
			opt[MATCH] = m[i-1][j-1].cost + match(s[i],t[j]);
			opt[INSERT] = m[i][j-1].cost + indel(t[j]);
			opt[DELETE] = m[i-1][j].cost + indel(s[i]);

			m[i][j].cost = opt[MATCH];
			m[i][j].parent = MATCH;
			for (k=INSERT; k<=DELETE; k++)
				if (opt[k] < m[i][j].cost) {
					m[i][j].cost = opt[k];
					m[i][j].parent = k;
				}
		}

	goal_cell(s,t,&i,&j);
	return( m[i][j].cost );
} 
Example #5
0
// return the edit distance for changing s into t by means of matching, swapping,
// changing, inserting, or deleting chars.
int string_compare(char* s, char* t)
{
  int i,j,k;
  int opt[5];

  for (i = 0; i < MAXLEN; i++)
  {
    row_init(i);
    column_init(i);
  }

  for (i = 1; i < strlen(s); i++)
  {
    for (j = 1; j < strlen(t); j++)
    {
      opt[MATCH]  = m[i-1][j-1].cost + match(s[i],t[j]);
      opt[SWAP]   = m[i-1][j-1].cost + swap(s, t, i);
      opt[CHANGE] = m[i-1][j-1].cost + 1;
      opt[INSERT] = m[i][j-1].cost + 1;
      opt[DELETE] = m[i-1][j].cost + 1;

      m[i][j].cost = opt[MATCH];
      m[i][j].parent = MATCH;
      for (k = SWAP; k <= DELETE; k++)
      {
        if (opt[k] < m[i][j].cost)
        {
          // if the last in-place action was a swap, and this action is a
          // change, then we are actually continuing the swap. Swap changes
          // two characters for a cost of one, so we change the first char for
          // 1, and change the second char for 0.
          if (k == CHANGE && m[i-1][j-1].parent == SWAP)
            m[i][j].cost = m[i-1][j-1].cost;
          // otherwise it's the least cost action, as identified
          else
            m[i][j].cost = opt[k];
          
          m[i][j].parent = k;
        }
      }
    }
  }

  goal_cell(s,t,&i,&j);
  return(m[i][j].cost);
}