Comparing two different strings
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Vlad State
le 16 Jan 2018
Réponse apportée : Stephen23
le 16 Jan 2018
Hello, this is my first post so please go easy on me.
I want to compare two strings which I obtained from an OCR, but I need to do it letter by letter. The catch is that one string may have some missing letters or some unwanted characters compared to the other.
My first idea was to use strsplit to separate the two strings in words, but after that I've no clue what to do next.
Thanks a lot!
0 commentaires
Réponse acceptée
Plus de réponses (1)
Stephen23
le 16 Jan 2018
>> C = {'live','eve','believe','belive'};
>> cellfun(@(c)wfEdits(c,'beleive'),C)
ans =
3 4 2 1
Thus showing that 'belive' is the closest to 'beleive'. The function is:
function d = wfEdits(S1,S2)
% Wagner–Fischer algorithm to calculate the edit distance / Levenshtein distance.
%
N1 = 1+numel(S1);
N2 = 1+numel(S2);
%
D = zeros(N1,N2);
D(:,1) = 0:N1-1;
D(1,:) = 0:N2-1;
%
for r = 2:N1
for c = 2:N2
D(r,c) = min([D(r-1,c)+1, D(r,c-1)+1, D(r-1,c-1)+~strcmpi(S1(r-1),S2(c-1))]);
end
end
d = D(end);
%
end
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!