MATLAB: Count punctuation marks in table columns

浪尽此生 提交于 2021-01-07 02:43:57

问题


I'm trying to find the amount of sentences in this table:

Download Table here: http://www.mediafire.com/file/m81vtdo6bdd7bw8/Table_RandomInfoMiddle.mat/file

As you can see by the full-stops, there is one sentence in column one, and 2 sentences in column 3. At the end of the day I desire to have a table with nothing but punctuation marks(with the exception of place holders like "", to keep the table rows the same length) that indicate the end of a sentence(Like "." or "?" or "!"), in order to calculate the total number of punctuation marks of each column. This is my code(Yet unsuccessful):

EqualCoumns = [2:2:max(width(Table_RandomInfoMiddle))];
for t=EqualCoumns
MiddleOnlySentenceIndicators = Table_RandomInfoMiddle((Table_RandomInfoMiddle{:, t}=='punctuation'),:);
%Reomve all but "!.?" = Which is the only sentence enders
    MiddleOnlySentenceIndicators(MiddleOnlySentenceIndicators{:, t} == ',', :) = [];
    MiddleOnlySentenceIndicators(MiddleOnlySentenceIndicators{:, t} == ';', :) = [];
    MiddleOnlySentenceIndicators(MiddleOnlySentenceIndicators{:, t} == ':', :) = [];
    MiddleOnlySentenceIndicators(MiddleOnlySentenceIndicators{:, t} == '-', :) = [];
MiddleSentence_Nr(t) =  height(MiddleOnlySentenceIndicators);
end

Right now this is almost giving good results, there is a little mistake somewhere. (In the answer I would like to request only one thing, that I might have access to the results in the same table like form, it should look something like this(edited):

Any help will be appreciated. Thank you!


回答1:


If we use the table from my previous answer, t, we can use the following solution:

punctuation_table = table();
for col=1:size(t,2)
    column_name = sprintf('Punctuation count for column %d',col);
    punctuation_table.(column_name) = nnz(ismember(t(:,col).Variables,{'?',',','.','!'}));
end

which will create a table like this:

punctuation_table =

1×4 table

Punctuation count for column 1    Punctuation count for column 2    Punctuation count for column 3    Punctuation count for column 4
______________________________    ______________________________    ______________________________    ______________________________

              2                                 0                                 2                                 0      


来源:https://stackoverflow.com/questions/64763611/matlab-count-punctuation-marks-in-table-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!