说我有一个表,其中包含另一个表中每个单词的概率。该表有2个类;实际和 non_actual 。我将命名 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)
然后我有一个包含句子的表。我将命名为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)
如何制作这样的方法(不属于" simon"," haha"的单词不属于主人,因此它不会影响确定类的概率的计算):
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
以及如何从上面的问题中制作VSM(向量空间模型):
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
作为一个快速解决方案,以下代码应解决您的问题:
% 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;
您应该阅读代码中使用的每个功能的帮助章节:strsplit
strfind
cellfun
,以获取有关其工作方式的更多信息。
这也有点循环,但我像性能一样跌倒不是问题。我将首先创建一个较大的表,然后更改循环中的值:
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;