image - How to find bunch of closest strings? -
i have string , pattern. how can use strfind in matlab , find bunch of closest strings? in other words, strfind finds exact match while interested find bunch of closest strings (e.g. 10 closest strings)
you can make use of this function strdist
file exchange, computes levenshtein distance between 2 strings.
here's convenient wrapper function. give single string str
, array of strings strarray
(and optionally number of strings return n
) , gives cell array containing closest n
strings:
function result = strfuzzy(str,strarray,n) #strfuzzy # # inputs # str string # strarray cell array of strings # n integer, 1 <= n <= length(strarray) # # outputs # result cell array of length n containing closest matches str # if nargin < 2, error('requires @ least 2 arguments'), end if nargin < 3, n = length(strarray); end = cellfun( @(x) strdist(str,x), strarray ); [tmp,idx] = sort(a); result = strarray(idx); result = result(1:n); end
here's how use it:
>> strarray = {'cat', 'hey', 'hay', 'hat', 'hey'}; >> strfuzzy('hey', strarray) ans = 'hey' 'hay' 'hey' 'hat' 'cat' >> strfuzzy('bat', strarray, 3) ans = 'cat' 'hat' 'hay'
Comments
Post a Comment