问题
Say that I have a table that contains probabilities from each words from another table. This Table has 2 classes; actual and non_actual. I will name it master_table
actual = [0.5;0.4;0.6;0.75;0.23;0.96;0.532]; %sum of the probabilities is 1.
actual + non_actual = 1
non_actual = [0.5;0.6:0.4;0.25;0.77;0.04;0.468];
words = {'finn';'jake';'iceking';'marceline';'shelby';'bmo';'naptr'};
master_table = table(actual,non_actual,...
'RowNames',words)
And then I have a table that contains sentences. I will name it T2
sentence = {'finn marceline naptr';'jake finn simon marceline haha';'jake finn finn jake iceking';'bmo shelby shelby finn naptr';'naptr naptr jake finn bmo shelby'}
T2 = table('RowNames',sentence)
How to make like this (Words that dont belong in the master_table like "simon", "haha" have value 1, so it wont affects the calculation of the probabilities to determine the class) :
actual %determines the value based on probabilities from each words% non_actual class
finn marceline naptr 0.5 * 0.75 * 0.532 0.5 * 0.25 * 0.468 compares the value from each class. if actual > non_actual then the class should be "actual"
jake finn simon marceline haha 0.4 * 0.5 * 1 * 0.25 * 1 0.6 * 0.5 * 1 * 0.75 * 1
jake finn finn jake iceking
bmo shelby shelby finn naptr
naptr naptr jake finn bmo shelby
And how to make the VSM (vector space model) from the problem above:
WORDS
| bmo | finn | jake | iceking | haha | marceline | naptr | shelby | simon | %words sorted alphabetically
finn marceline naptr 0 1 0 0 0 1 1 0 0
jake finn simon marceline haha 0 1 1 0 1 1 0 0 1
jake finn finn jake iceking 0 2 2 1 0 0 0 0 0
bmo shelby shelby finn naptr 1 1 0 0 0 0 1 1 0
naptr naptr jake finn bmo shelby 1 1 1 0 0 0 1 1 0
回答1:
as a quick solution following code should solve your problem:
% Split the sentence into single strings
s = strsplit(sentence{1});
% loop over all single strings
for i=1:length(s)
% search for each string pattern in the words-cell
c = strfind(words,s{i});
% get a logical vector for getting the index of the found pattern in
% the words-cell
ix=cellfun('isempty', c);
ind = find(ix == 0);
if actual(ind) > non_actual(ind)
% do something with actual...
end;
end;
you should read the help chapters for each function used in the code: strsplit
strfind
cellfun
for getting more information about how they work.
回答2:
This is a bit loopy as well but i fell like performance is not an issue. I would create a bigger table first and then change the values in a loop:
T2 = table(ones(height(T2),1),ones(height(T2),1),repmat({''},height(T2),1),'RowNames',sentence,'VariableNames',{'actual' 'non_actual' 'outcome'});
for i=1:height(T2)
% split the row name
A=strsplit([T2.Properties.RowNames{i,:}]);
actual=1; %which is neutral for multiplication
non_actual=1;
for j=1:length(A)
actual = actual * master_table{A(j),1};
non_actual = non_actual * master_table{A(j),2};
end
%if you need those
T2.actual(i)=actual;
T2.non_actual(i)=non_actual;
if actual > non_actual
T2.outcome(i)={'actual'};
else
T2.outcome(i)={'non_actual'};
end;
end;
来源:https://stackoverflow.com/questions/37924343/how-to-scan-values-from-each-words-based-on-tables-and-then-calculate-it-and-mak