Difficulty Assigning String Values after Regexp

1 view (last 30 days)
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

Accepted Answer

Matt Tearle
Matt Tearle on 20 Jun 2013
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.

More Answers (1)

David Sanchez
David Sanchez on 20 Jun 2013
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 Comments
David Sanchez
David Sanchez on 20 Jun 2013
could you show the new code and the error again?
kenny
kenny on 20 Jun 2013
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

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!