Comparing two different strings
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vlad State
el 16 de En. de 2018
Respondida: Stephen23
el 16 de En. de 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 comentarios
Respuesta aceptada
Más respuestas (1)
Stephen23
el 16 de En. de 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 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!