Hi,
I have two texts . is it possible to extract the text where the two arrays match.For example: A='First Boston Corp Lehman Brothers ' B='Lehman Brothers Merill Lynch'; How can I get the match "Lehman Brothers"

댓글 수: 3

Walter Roberson
Walter Roberson 2011년 6월 28일
Should the matching be pure consecutive substring? For example, if B='Lehman Brother''s Merill Lynch' then should the algorithm notice the longer match available if you omit the '''' character?
C='Sam Brown eats a boiled egg on Fridays'
D='Sam Brown eats boiled eggs on Fridays'
should the algorithm match as
'Sam Brown eats boiled egg on Fridays'
as being a match if you remove the 'a ' in one and the 's' in the other?
Longest substring only? If several are of the longest size, first of those?
joseph Frank
joseph Frank 2011년 6월 28일
yes they should be consecutive because these are names and should match perfectly such as Lehman it can never be lehmans
joseph Frank
joseph Frank 2011년 6월 28일
longest string only

댓글을 달려면 로그인하십시오.

 채택된 답변

Teja Muppirala
Teja Muppirala 2011년 6월 29일

1 개 추천

The simple brute force method:
A='Lehman Brothers Merill Lynch';
B='First Boston Corp Lehman Brothers';
for n = 1:numel(B);
for k = 1:n
if ~isempty(strfind(A,B(k + (0:numel(B)-n))))
Bmatch = B(k + (0:numel(B)-n))
return
end
end
end

추가 답변 (2개)

Matt Fig
Matt Fig 2011년 6월 29일

0 개 추천

A = 'First Boston Corp Lehman Brothers ';
B = 'Lehman Brothers Merill Lynch';
Am = regexp(A,'\s','split');
Am = Am(ismember(Am,regexp(B,'\s','split')))

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 6월 29일
That finds words in common, not substrings in common. For example if B='Brothers Merill Lehman Lynch' then that algorithm would output {'Lehman' 'Brothers'} even though 'Brothers ' is the longest common substring.
Longest substring could potentially be 'Lehman Brother' if one of the strings had 'Lehman Brothers' and the other had 'Lehman Brotherhood'. It is not completely clear from Joseph's description whether only "words" are to be matched or whether parts of words are okay as well.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 6월 29일

0 개 추천

This is the "longest common substring problem"; see http://en.wikipedia.org/wiki/Longest_common_substring_problem (which looks a bit biased in that it only presents one algorithm)

카테고리

태그

Community Treasure Hunt

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

Start Hunting!

Translated by