필터 지우기
필터 지우기

Identifying Keywords in Strings strfind

조회 수: 4 (최근 30일)
kenny
kenny 2013년 6월 17일
hello! I'm trying to get this program to recognize a couple of keywords in a string and then based on what those words are, give me an output.
so if i said "killing your father" it would recognize "Killing" as "Bad", "father" as "GoodObject" and mixing those two would get a "that's bad" result.
If i were to write "helping your brother" it would recognize "helping" as "Good" and "brother" as "GoodObject" it would give a "that's good" result.
The problem is I don't know how to make it so the program can recognize two strings in the same sentence. I thought using "&" would work, but it doesn't and i get ??? Undefined function or method 'and' for input arguments of type 'cell'.
Error in ==> goodbad at 11
if any(strcmpi(question,GoodObject & Good))
how could i use strfind so that it identifies anything in the Array "Good Object" or "Bad" or "Good"?
thank you!
GoodObject = {'brother', 'sister', 'mother', 'father'};
Bad = {'killing', 'hurting', 'stealing', 'robbing'};
Good = {'helping', 'giving', 'sharing', 'forgiving'};
question = input ('what?','s')
if any(strcmpi(question,GoodObject & Good))
disp('that''s good')
else if any(strcmpi(question,GoodObject & Bad))
disp('that''s bad')
end
end
  댓글 수: 2
per isakson
per isakson 2013년 6월 17일
This looks like a toy example. Are you looking for a solution that would scale, i.e will the lists of words be much longer?
kenny
kenny 2013년 6월 17일
yes, much longer.

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

채택된 답변

Marc
Marc 2013년 6월 17일
I think you want to try regexp, regexpi and/or regexprep.
In the Matlab documentation, this is under: Matlab > Language Fundamentals > Data Types > Characters and Strings > Parse Strings....
There is a whole section under Examples and How To on "Regular Expressions".

추가 답변 (1개)

Muthu Annamalai
Muthu Annamalai 2013년 6월 17일
I have two comments, while in general agreement with @Marc;
1. You can build strings out of sentences in first pass 2. Use containers.Map() (see: http://www.mathworks.com/help/matlab/ref/containers.mapclass.html ) dictionary type to store your object-value associations, after sorting them.
valueMap = containers.Map();
valueMap('GoodThings') = sort({... })
valueMap('BadThings') = sort({...})
3. Use a binary-search to lookup each string in the sentence, against your Map. This will enormously speedup your program for long lists.
Goodluck!

카테고리

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