Difficulty Assigning String Values after Regexp

1 次查看(过去 30 天)
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 个评论
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