Find if any string in a cell array is contained in a string

조회 수: 1 (최근 30일)
TastyPastry
TastyPastry 2015년 10월 6일
편집: Kirby Fears 2015년 10월 7일
I have a cell array of strings, for example, C = {'bob','jack','john'}.
I have a test string T = 'The quick brown jack jumped'.
In this case, I'd like my output to be 2, since 'jack' is contained in T. There is a guarantee that only one string in the cell array will match any part of the test string, i.e. the output will be a single index.
What's the best way to go about implementing this code quickly? I don't want to loop through the cell array and check each time since this function is called as the user types an input in a GUI.

채택된 답변

Star Strider
Star Strider 2015년 10월 6일
You have to split ‘T’ into its component words first, but that’s easy enough with a regexp call:
T = 'The quick brown jack jumped';
Tc = regexp(T, ' ', 'split');
C = {'bob','jack','john'};
CLoc = find(ismember(C,Tc))
CLoc =
2
‘CLoc’ is the location in ‘C’ of the matching word.
  댓글 수: 2
TastyPastry
TastyPastry 2015년 10월 6일
What happens if C is multiple words? For example, if C = {'bob','jack jumped','john'}. I'm trying to make a program which detects the user typing a phrase and then autofills the rest by detecting the phrase from some bank, in this case, C.
Star Strider
Star Strider 2015년 10월 6일
The same logic holds, but because ‘T’ has to be split into its component words, there would be no match with the phrase.
However, if ‘C’ were:
C = {'bob','jack', 'jumped','john'};
CLoc =
2 3
(I just verified that.)
So if you wanted to match phrases, you would have to figure out a way to parse the phrase from ‘T’ rather than the words. That would require you to create logic to recognise phrases. The ismember match would then work.

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

추가 답변 (1개)

Kirby Fears
Kirby Fears 2015년 10월 6일
편집: Kirby Fears 2015년 10월 7일
Have you tested the speed of a for-loop or cellfun()? It's probably your best bet here unless C contains a really large number of strings. You can make the computation faster by ordering C from most-likely to least-likely to be typed.
for-loop:
idx=[];
for c=1:numel(C),
temp=regexp(T,C{c},'once');
if ~isempty(temp),
idx=c;
break;
end,
end,
or cellfun:
idx2=find(cellfun(@(c)~isempty(regexp(T,c,'once')),C));
If you involve Java classes, you could possibly speed up string comparisons. However, you can't avoid the need to search for every string in C within T. If C is large enough to try Java code, clever use of an iterable class might be the solution you're looking for.
Hope this helps.
  댓글 수: 2
TastyPastry
TastyPastry 2015년 10월 6일
I'm not sure if the looping is causing the program to slow down enough so that the callback isn't functioning properly.
I have the program set up so that the string checker is the KeyPressFcn of an edit box in a GUI. However, it appears that unless I use a breakpoint before the for loop and manually step through, the callback fails to execute properly and does not replace matched text. I've got no idea what's going wrong here.
Kirby Fears
Kirby Fears 2015년 10월 7일
편집: Kirby Fears 2015년 10월 7일
Perhaps the "replacement" part is what's not working. Try setting your KeyPressFcn to replace text with a fixed string like str='mystring' any time a key is pressed to confirm that replacement is working.
Hope this helps.

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

카테고리

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