Created
May 25, 2019 09:41
-
-
Save hemendrarajawat/9bcdb7c2cf463513a5c25489e781637a to your computer and use it in GitHub Desktop.
Calculate the Degree of Similarity b/w two words.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
int min(int,int,int); | |
void main() { | |
int r,c,i,j; | |
char firstword[30], secondword[30]; | |
clrscr(); | |
printf("Enter first word: "); | |
scanf("%s", &firstword); | |
printf("Enter first word: "); | |
scanf("%s", &secondword); | |
r = strlen(firstword)+1; | |
c = strlen(secondword)+1; | |
int **arr = (int**)malloc(r*sizeof(int *)); | |
for (i=0; i<r; i++) { | |
arr[i] = (int *)malloc(c * sizeof(int)); | |
} | |
for (i=0;i<r;i++) { | |
for (j=0;j<c;j++) { | |
arr[i][j] = 0; | |
} | |
} | |
for (i=0;i<r;i++) { | |
arr[i][0] = i; | |
} | |
for (j=0;j<c;j++) { | |
arr[0][j] = j; | |
} | |
for (i=1; i<r; i++) { | |
for (j=1; j<c; j++) { | |
if (firstword[i-1] == secondword[j-1]) { | |
arr[i][j] = arr[i-1][j-1]; | |
} else { | |
arr[i][j] = min(arr[i-1][j], arr[i-1][j-1], arr[i][j-1])+1; | |
} | |
} | |
} | |
printf("-----------------------------\n"); | |
for (i=0; i<r; i++) { | |
for (j=0; j<c; j++) { | |
printf("%d ",arr[i][j]); | |
} | |
printf("\n"); | |
} | |
int sim_score = arr[r-1][c-1]; | |
float maxlen = 0; | |
if (strlen(secondword) < strlen(firstword)) { | |
maxlen = strlen(firstword); | |
} else { | |
maxlen = strlen(secondword); | |
} | |
float similarity = (1-(sim_score/maxlen))*100; | |
printf("Similarity: %.2f%", similarity); | |
getch(); | |
} | |
int min(int a, int b, int c) { | |
int min = a; | |
if (b < min) { | |
min = b; | |
} | |
if (c < min) { | |
min = c; | |
} | |
return min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment