| 1 |
|
| #include "EXTERN.h"
|
| 2 |
|
| #include "perl.h"
|
| 3 |
|
| #include "XSUB.h"
|
| 4 |
|
|
|
| 5 |
|
| /****************************************************/
|
| 6 |
|
| /* Levenshtein Distance Algorithm */
|
| 7 |
|
| /* C Implementation by Lorenzo Seidenari */
|
| 8 |
|
| /* http://www.merriampark.com/ldc.htm */
|
| 9 |
|
| /* modified by dree */
|
| 10 |
|
| /****************************************************/
|
| 11 |
|
|
|
| 12 |
|
| #include <stdlib.h>
|
| 13 |
|
| #include <string.h>
|
| 14 |
|
|
|
| 15 |
|
| int levenshtein_distance(char *s,char*t);
|
| 16 |
|
| int minimum(int a,int b,int c);
|
| 17 |
|
|
|
| 18 |
5 |
| int levenshtein_distance(char *s,char*t)
|
| 19 |
|
|
|
| 20 |
|
| /*Compute levenshtein distance between s and t*/
|
| 21 |
|
| {
|
| 22 |
|
| //Step 1
|
| 23 |
|
| int k,i,j,n,m,cost,*d,distance;
|
| 24 |
5 |
▶ | if (strcmp(s,t) == 0) {return 0;}
|
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(s,t) == 0) {return 0;}
|
1 |
4 |
5 |
|
| 25 |
4 |
| n=strlen(s);
|
| 26 |
4 |
| m=strlen(t);
|
| 27 |
4 |
▶ | if(n==0) {return m;}
|
Branch: 1 branch
| Branch | True | False | Total |
| if(n==0) {return m;}
|
0 |
4 |
4 |
|
| 28 |
4 |
▶ | if(m==0) {return n;}
|
Branch: 1 branch
| Branch | True | False | Total |
| if(m==0) {return n;}
|
1 |
3 |
4 |
|
| 29 |
|
|
|
| 30 |
3 |
| d=malloc((sizeof(int))*(m+1)*(n+1));
|
| 31 |
3 |
| m++;
|
| 32 |
3 |
| n++;
|
| 33 |
|
| //Step 2
|
| 34 |
16 |
▶ | for(k=0;k<n;k++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0;k<n;k++)
|
13 |
3 |
16 |
|
| 35 |
13 |
| d[k]=k;
|
| 36 |
16 |
▶ | for(k=0;k<m;k++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0;k<m;k++)
|
13 |
3 |
16 |
|
| 37 |
13 |
| d[k*n]=k;
|
| 38 |
|
| //Step 3 and 4
|
| 39 |
13 |
▶ | for(i=1;i<n;i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1;i<n;i++)
|
10 |
3 |
13 |
|
| 40 |
43 |
▶ | for(j=1;j<m;j++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(j=1;j<m;j++)
|
33 |
10 |
43 |
|
| 41 |
|
| {
|
| 42 |
|
| //Step 5
|
| 43 |
33 |
▶ | if(s[i-1]==t[j-1])
|
Branch: 1 branch
| Branch | True | False | Total |
| if(s[i-1]==t[j-1])
|
6 |
27 |
33 |
|
| 44 |
6 |
| cost=0;
|
| 45 |
|
| else
|
| 46 |
27 |
| cost=1;
|
| 47 |
|
| //Step 6
|
| 48 |
33 |
| d[j*n+i]=minimum(d[(j-1)*n+i]+1,d[j*n+i-1]+1,d[(j-1)*n+i-1]+cost);
|
| 49 |
|
| }
|
| 50 |
3 |
| distance=d[n*m-1];
|
| 51 |
3 |
| free(d);
|
| 52 |
3 |
| return distance;
|
| 53 |
|
| }
|
| 54 |
|
|
|
| 55 |
33 |
| int minimum(int a,int b,int c)
|
| 56 |
|
| /*Gets the minimum of three values*/
|
| 57 |
|
| {
|
| 58 |
33 |
| int min=a;
|
| 59 |
33 |
▶ | if(b<min)
|
Branch: 1 branch
| Branch | True | False | Total |
| if(b<min)
|
12 |
21 |
33 |
|
| 60 |
12 |
| min=b;
|
| 61 |
33 |
▶ | if(c<min)
|
Branch: 1 branch
| Branch | True | False | Total |
| if(c<min)
|
9 |
24 |
33 |
|
| 62 |
9 |
| min=c;
|
| 63 |
33 |
| return min;
|
| 64 |
|
| }
|
| 65 |
|
|
|
| 66 |
|
| MODULE = Text::LevenshteinXS PACKAGE = Text::LevenshteinXS
|
| 67 |
|
|
|
| 68 |
|
| int
|
| 69 |
|
| distance(s,t)
|
| 70 |
|
| char * s
|
| 71 |
|
| char * t
|
| 72 |
|
| CODE:
|
| 73 |
5 |
| RETVAL = levenshtein_distance(s,t);
|
| 74 |
|
| OUTPUT:
|
| 75 |
|
| RETVAL
|
| 76 |
|
|
|