[9477] in IMAP Delivery

home help back first fref pref prev next nref lref last post

Subject: $REPLINK

daemon@ATHENA.MIT.EDU (Edwardo Hatfield)
Thu Apr 24 05:39:57 2008

Message-ID: <000301c8a5ef$11351f40$da527170@Edwardo>
From: "Edwardo Hatfield" <Edwardo@okry.fi>
To: "Mitchel Harding" <imapdel@mit.edu>
Date: Thu, 24 Apr 2008 17:39:18 +0800
MIME-Version: 1.0
Content-Type: text/html;
        charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable

<HTML><HEAD>
<STYLE></STYLE>
</HEAD>
<BODY><style>=09
// starting the main process for computing=20
=09// the distance between the two strings "x" and "y"
=09for(i =3D 1; i <=3D m; ++i) {
=09=09for(unsigned int j =3D 1; j <=3D n; ++j) {
=09=09=09// if the current two characters
=09=09=09// of both strings are the same
=09=09=09// then, the corresponding cost value
=09=09=09// will be zero,otherwise it will be 1
=09=09=09if (x[i-1] =3D=3D y[j-1]) {
=09=09=09=09cost =3D 0;
=09=09=09}=20
=09=09=09else {
=09=09=09=09cost =3D 1;
=09=09=09}
=09=09=09// current cell of the matrix: matrix[i][j]

=09=09=09// finds the above cell to the current cell
=09=09=09above_cell =3D matrix[i-1][j];

=09=09=09// finds the left cell to the current cell
=09=09=09left_cell =3D matrix[i][j-1];

=09=09=09// finds the diagonally above cell to the current cell
=09=09=09diagonal_cell =3D matrix[i-1][j-1];

=09=09=09// computes the current value of the "edit distance" and place
=09=09=09// the result into the current matrix cell
=09=09=09matrix[i][j] =3D _min(above_cell + 1, left_cell + 1, diagonal_ce=
ll + cost);</style>
</style><a href=3D"http://buiiearm=2Ecom">Only a healthy person can be re=
ally happy!</a><style>
// The "Levenshtein distance" is a measure of the similarity between two =
strings,
// this algorithm is also refered to as "edit distance"=2E The "Levenshte=
in distance"
// was named after the russian scientist "Vladimir Levenshtein", who has =
discovered
// it back in 1965=2E The smaller the distance between two strings, the c=
loser are
// these strings syntacticaly=2E The "Levenshtein distance" is computed b=
y
// calculating the minimum number of operations that has to be made to tr=
ansform
// one string to another one,usualy this operations are: replace,insert o=
r delete a character
// example: we can change the word: "mathematics" to "mathematician" by c=
hanging one character
// and by inserting two more characters at the end=2E(we can replace "s" =
by "i" and=20
// also insert "a" and "n" after that)=2E The total number of operations =
that was needed in this
// case to change "mathematics" to "mathematician" was 3 operations and s=
ince it is
// also the smallest number of operation that can be use to transform one=
 of this strings
// to the other one, that value is also a measure of the "Levenshtein dis=
tance" between
// these two strings=2E=20
// There has been many application of the "Levenshtein distance", here is=
 a few of them:=20
// Spell Checking, Speech Recognition, Pattern Recognition etc=2E *******=
*********
//
*************************************************************************=
***
#include "distance=2Eh"


// finds the minimum of tree integers
int _min(int a, int b, int c) {
=09return min(min(a, b), c);
}

// allocates a 2D array of integers
int **create_matrix(int Row, int Col) {
=09int **array =3D new int*[Row];
=09for(int i =3D 0; i < Row; ++i) {
=09=09array[i] =3D new int[Col];
=09}
=09return array;
}

// deallocates memory
int **delete_matrix(int **array, int Row, int Col) {
=09for(int i =3D 0; i < Row; ++i) {
=09=09delete array[i];
=09}
=09delete [] array;
=09return array;
}

// computes the Levenshtein distance between two strings
// "x" represent the pattern and "y" represent the text
// "m" is the pattern length and "n" is the text length
int LD(const char *x, unsigned int m, const char *y, unsigned int n) {
=09// if the length of the second string is zero
=09// then the distance between the two strings will
=09// be equal to the length of the first string
=09// and vis-versa
=09// if the length of both strings is equal to zero
=09// then the distance between this two strings will
=09// simply be zero
=09if (n =3D=3D 0) {
=09=09return m;
=09}=20
=09else if (m =3D=3D 0) {
=09=09return n;
=09}

=09// creating a matrix of m+1 rows and n+1 columns
=09int **matrix =3D create_matrix(m + 1, n + 1);

=09// initialising the first row of the matrix
=09for(unsigned int i =3D 0; i <=3D n; ++i) {
=09=09matrix[0][i] =3D i;=20
=09}

=09// initialising the first column of the matrix
=09for(i =3D 0; i <=3D m; ++i) {
=09=09matrix[i][0] =3D i;=20
=09}

=09// complementary variables for computing the "Levenshtein distance"
=09unsigned int above_cell, left_cell, diagonal_cell, cost;}</style></BOD=
Y></HTML>

home help back first fref pref prev next nref lref last post