Difficulty Assigning String Values after Regexp

조회 수: 3 (최근 30일)
kenny
kenny 2013년 6월 20일
Hi all! I had previously posted about trying to find keywords in a sentence, and I was sent to the regexp page (very helpful, thank you) which allowed me to do just that.
Now that it's possible for me to find the strings i'm looking for, I want matlab to assign each found string its corresponding value, and then multiply these values.
For example, the strings in the array "Good Object" all have a value of 1, and those in "Bad" all have a value of -1. so if i wrote "hurting your brother" regexp would find the keywords in their arrays, and then would return (-1)*(1) = -1.
so far with regexp it correctly finds the string's location in the array and prints it, but i'm having difficulties associating the words with their array values and multiplying them. below is the error i get, but i'm pretty sure it's just the tip of the iceberg. Any help would be greatly appreciated!
thank you
??? Too many outputs requested. Most likely cause is missing [] around
left hand side that has a comma separated list expansion.
Error in ==> goodbad at 17
x = (GoodObjectValue{Gov});
code:
GoodObject = {'brother', 'sister', 'mother', 'father'};
GoodObjectValue = {1};
Bad = {'hurting', 'stealing', 'robbing', 'breaking'};
BadValue = {-1};
Gov = find(strcmpi(question,GoodObject));
Bv = find(strcmpi(question,Bad));
question = input ('what?','s')
GoodExpression = GoodObject;
BadExpression = Bad;
matchstr = regexp(question,GoodExpression,'match')
matchstr = regexp(question, BadExpression,'match')
x = (GoodObjectValue{Gov});
y = (BadValue{Bv});
i=x*y;
if matchstr(question,GoodObject)&& matchstr(question,Bad)
disp (i)
if (i == -1)
disp('that''s bad')
else if (i == 1)
disp('that''s good')
end
end
end

채택된 답변

Matt Tearle
Matt Tearle 2013년 6월 20일
Maybe this is oversimplifying, but I think this does what you're trying to do:
GoodObject = {'brother', 'sister', 'mother', 'father'};
Bad = {'hurting', 'stealing', 'robbing', 'breaking'};
question = input('what? ','s');
Gov = any(~cellfun(@isempty,regexp(question,GoodObject)));
Bv = any(~cellfun(@isempty,regexp(question,Bad)));
if (Gov && Bv)
disp('that''s bad')
else
disp('that''s good')
end
If I interpret your question correctly, the key thing is that you're trying to see if anything in the question string is in either list of keywords. That's what
Gov = any(~cellfun(@isempty,regexp(question,GoodObject)));
Bv = any(~cellfun(@isempty,regexp(question,Bad)));
does. As I have it, these are logical values. You could easily modify them to be numeric, if you actually need that.

추가 답변 (1개)

David Sanchez
David Sanchez 2013년 6월 20일
you did not define neither the value of Gov nor bv.
Your code:
x = (GoodObjectValue{Gov});
y = (BadValue{Bv});
i=x*y;
Gov = find(strcmpi(question,GoodObject));
Bv = find(strcmpi(question,Bad));
It should be something like:
Gov = find(strcmpi(question,GoodObject));
Bv = find(strcmpi(question,Bad));
x = (GoodObjectValue{Gov});
y = (BadValue{Bv});
i=x*y;
  댓글 수: 3
David Sanchez
David Sanchez 2013년 6월 20일
could you show the new code and the error again?
kenny
kenny 2013년 6월 20일
error reads as :
??? Too many outputs requested. Most likely cause is missing [] around
left hand side that has a comma separated list expansion.
Error in ==> goodbad at 8
x = (GoodObjectValue{Gov});
code:
GoodObject = {'brother', 'sister', 'mother', 'father'};
GoodObjectValue = {1};
Bad = {'hurting', 'stealing', 'robbing', 'breaking'};
BadValue = {-1};
Gov = find(strcmpi(question,GoodObject));
Bv = find(strcmpi(question,Bad));
x = (GoodObjectValue{Gov});
y = (BadValue{Bv});
i=x*y;
question = input ('what?','s')
GoodExpression = GoodObject;
BadExpression = Bad;
matchstr = regexp(question,GoodExpression,'match')
matchstr = regexp(question, BadExpression,'match')
if matchstr(question,GoodObject)&& matchstr(question,Bad)
disp (i)
if (i == -1)
disp('that''s bad')
else if (i == 1)
disp('that''s good')
end
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