Difficulty Assigning String Values after Regexp
2 ビュー (過去 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
0 件のコメント
採用された回答
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
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!