Transcript
Page 1: Levenstein Distance between 2 words

8/15/2019 Levenstein Distance between 2 words

http://slidepdf.com/reader/full/levenstein-distance-between-2-words 1/1

long then = System.currentTimeMillis();long duration = System.currentTimeMillis() - then;System.out.println(duration + " milliseconds.");

//Levenstein Distancepublic int distance(int v[], int u[], int N, int M) {

d = new int[N + 1][M + 1];for(int i = 0; i <= N; i++)

d[i][0] = 0;for(int j = 0; j <= M; j++)

d[0][j] = 0;for(int i = 1; i <= N; i++) {

for(int j = 1; j <= M; j++) {int cost = (v[i - 1] == u[j - 1]) ? 0 : 1;d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1,

d[i - 1][j - 1] + cost);}

}return d[N][M];

}

//Levenstein Distance between more wordsfor(int i = 0; i <= M; i++)

u[i] = i;

for(int i = 1; i <= N; i++) {w[0] = i;for(int j = 2; j <= M; j++) {

int cost = 1;for(int k = 0; k < NR_VAR; k++)

if(v[j - 1] == m[k][i - 1]) {cost = 0;break;

}w[j] = min(w[j - 2] + 1, u[j - 1] + 1, u[j] + co

st);}

for(int j = 0; j <= M; j++)u[j] = w[j];}return w[M];

Top Related