Comparing two different strings

7 次查看(过去 30 天)
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!

采纳的回答

Image Analyst
Image Analyst 2018-1-16
编辑:Image Analyst 2018-1-16

更多回答(1 个)

Stephen23
Stephen23 2018-1-16
You could use the edit distance as a measure of similarity:
>> 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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by