Last active
December 17, 2020 03:00
-
-
Save Plotist/da10ff1561239466a29f2fe72c2b0205 to your computer and use it in GitHub Desktop.
Edit distance calculation [ C++ ]
This file contains hidden or 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 <iostream> | |
#include "edit_distance.h" | |
using namespace std; | |
// public | |
EditDistance::EditDistance(char *string1, char *string2){ | |
s1 = string1; | |
s2 = string2; | |
rows = strlen(s1)+1; | |
columns = strlen(s2)+1; | |
allocate_range(); | |
vagner_fisher_calc(); | |
} | |
void EditDistance::vagner_fisher_view(){ | |
cout << "Calculated rows: " << s1 << " Calculated columns: " << s2 << endl; | |
for(int i = 0; i<rows; i++){ | |
for(int j = 0; j<columns; j++){ | |
cout << range[i][j] << ' '; | |
} | |
cout << endl; | |
} | |
} | |
int **EditDistance::get_range_matrix(){ | |
return range; | |
} | |
int EditDistance::get_range(){ | |
return range[rows-1][columns-1]; | |
} | |
// private | |
void EditDistance::allocate_range(){ | |
range = new int*[rows]; | |
for(int i = 0; i<rows; i++){ | |
range[i] = new int[columns]; | |
} | |
} | |
int EditDistance::compare_symbs(char a, char b){ | |
return (a==b)?0:1; | |
} | |
void EditDistance::vagner_fisher_calc(){ | |
for(int j = 0; j<columns; j++){ | |
range[0][j] = j; | |
} | |
for(int i = 0; i<rows; i++){ | |
range[i][0] = i; | |
} | |
for(int i = 1; i<rows; i++){ | |
for(int j = 1; j<columns; j++){ | |
range[i][j] = min(min(range[i][j-1]+1, range[i-1][j]+1), range[i-1][j-1] + compare_symbs(s1[i-1],s2[j-1])); | |
} | |
} | |
} | |
This file contains hidden or 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
class EditDistance { | |
private: | |
const char *s1; | |
const char *s2; | |
int **range; | |
int rows,columns; | |
void allocate_range(); | |
void vagner_fisher_calc(); | |
int compare_symbs(char, char); | |
public: | |
EditDistance(char *, char *); | |
~EditDistance(); | |
void vagner_fisher_view(); | |
int **get_range_matrix(); | |
int get_range(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment