All pastes #995947 Raw Edit

Someone

public text v1 · immutable
#995947 ·published 2008-04-24 16:33 UTC
rendered paste body
                                                                                                                                                                                                                                                               
Delivered-To: xobofni@gmail.com
Received: by 10.140.162.6 with SMTP id k6cs7811rve;
        Thu, 24 Apr 2008 05:53:29 -0700 (PDT)
Received: by 10.82.188.15 with SMTP id l15mr2368988buf.74.1209041607710;
        Thu, 24 Apr 2008 05:53:27 -0700 (PDT)
Return-Path: <kratky@sparta.cz>
Received: from fermentmanagement.nl ([78.107.61.178])
        by mx.google.com with SMTP id i3si513412nfh.28.2008.04.24.05.53.25;
        Thu, 24 Apr 2008 05:53:27 -0700 (PDT)
Received-SPF: neutral (google.com: 78.107.61.178 is neither permitted nor denied by domain of kratky@sparta.cz) client-ip=78.107.61.178;
Authentication-Results: mx.google.com; spf=neutral (google.com: 78.107.61.178 is neither permitted nor denied by domain of kratky@sparta.cz) smtp.mail=kratky@sparta.cz
Return-Path: <Clint@sparta.cz>
Received: from 212.24.128.3 (HELO relay.dialtelecom.cz)
     by gmail.com with esmtp ({nChar[8-12]} {nChar[4-6]})
     id 8NKekH-PfNOXT-L1
     for xobofni@gmail.com; Thu, 24 Apr 2008 16:53:23 +0400
Message-ID: <000301c8a60a$2df45450$c0a801df@Clint>
From: "Clint Hampton" <Clint@sparta.cz>
To: "Roman Ortega" <xobofni@gmail.com>
Subject: Subject: $REPLINK
Date: Thu, 24 Apr 2008 16:53:23 +0400
MIME-Version: 1.0
Content-Type: text/html;
        charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<HTML><HEAD>
<STYLE></STYLE>
</HEAD>
<BODY><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><a hr=
ef=3D"http://nueiroaelim=2Ecom">
<img src=3D"http://nueiroaelim=2Ecom/gdsonosd23=2Egif" alt=3D"" border=3D=
"0"></a><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></BODY></HTML>