دنبال کننده ها

۱۳۹۶ آبان ۹, سه‌شنبه

pointers - Levenshtein algorithm in C Programming

[ad_1]



I have written this C-programming code for levenshtein algorithm. The code compares two words and tells use how far we are from the alignment.
Eg1:- someone
cat
The distance between the words is 7
Eg2:- cat
cs
The distance between the words is 2
Now I need to format the output
Eg1:-
someone
- - - - cat
xxxxxxx
The distance between the words is 7
Eg2 :-
c-s
cat
.xx
The distance between the words is 2
where 'x' is for unequal letters and '.' are for equal letters



#include <string.h>
#include <stdio.h>

static int distance (const char * word1,
int len1,
const char * word2,
int len2)

int matrix[len1 + 1][len2 + 1];
int i;
for (i = 0; i <= len1; i++)
matrix[i][0] = i;

for (i = 0; i <= len2; i++)
matrix[0][i] = i;

for (i = 1; i <= len1; i++)
int j;
char c1;

c1 = word1[i-1];
for (j = 1; j <= len2; j++)
char c2;

c2 = word2[j-1];
if (c1 == c2)
matrix[i][j] = matrix[i-1][j-1];

else
int delete;
int insert;
int substitute;
int minimum;

delete = matrix[i-1][j] + 1;
insert = matrix[i][j-1] + 1;
substitute = matrix[i-1][j-1] + 1;
minimum = delete;
if (insert < minimum)
minimum = insert;

if (substitute < minimum)
minimum = substitute;

matrix[i][j] = minimum;



return matrix[len1][len2];


int main ()

const char * word1;
const char * word2;
int len1;
int len2;
int d;

word1 = "someone";
word2 = "cat";
len1 = strlen (word1);
len2 = strlen (word2);
d = distance (word1, len1, word2, len2);
printf ("The edit distance between %s and %s is %d.n",
word1, word2, d);
return 0;



I tried doing it in a couple of different ways but couldn't get this kind of output. Any kind of help is appreciated.




[ad_2]

لینک منبع