Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用matlab计算文本中字母的频率_Matlab_Text_Charts_Word Frequency - Fatal编程技术网

用matlab计算文本中字母的频率

用matlab计算文本中字母的频率,matlab,text,charts,word-frequency,Matlab,Text,Charts,Word Frequency,假设我们有下面的文本 s='i love georgia and its nature'; 我想要的是计算每个字母的出现频率(当然不包括空格)并绘制一些图表(例如条形图),首先我创建了使用地图容器计算字母的代码 function character_count(s) % s is given string and given program will count occurence of letters in % sentence MAP=containers.Ma

假设我们有下面的文本

s='i love georgia and its nature';
我想要的是计算每个字母的出现频率(当然不包括空格)并绘制一些图表(例如条形图),首先我创建了使用地图容器计算字母的代码

 function character_count(s)
    % s is given string and given program will count  occurence of letters in
    % sentence
    MAP=containers.Map();% initialize   MAP for frequency counting
    n=length(s); % get length of given string
    letters=unique_without_space_sorting(s);
    for ii=1:n
        if ~isletter(s(ii))==1
            continue;
        elseif  isKey(MAP,s(ii) )
            MAP(s(ii)) = MAP(s(ii))  + 1;
        else
          MAP(s(ii)) = 1;

        end
    end
      y=values(MAP);
      y= cell2mat(y);
     bar(y);
    set(gca,'xticklabel',letters)

    end
此处函数

letters=unique_without_space_sorting(s);
返回字符串s的字母的单元格数组,不带排序和空格,下面是其对应的代码

 function cell_stirng=unique_without_space_sorting(s)
    s=regexprep(s,'[^\w'']','');
     [~, idxs, ~] = unique(s, 'last');
     s= s(sort(idxs));
     n=length(s);
     cell_stirng=cell(n,1);
     for jj=1:n
         cell_string{jj}=s(jj);
     end
     end
当我运行这段代码时,我得到了如下图像


如您所见,x轴上没有标签,如何解决此问题?提前感谢

您可以使用
unique
的第一个输出为您提供唯一值,并将其用作x标签

[values, idxs, ~] = unique(s, 'last');

% Make sure that they aren't sorted
[~, sortind] = sort(idxs);
values = num2cell(values(sortind));

% And later after creating your bar plot
set(gca, 'xtick', 1:numel(values), 'XTickLabels', values);
或者,您可以只使用
unique
stable
输入来确保它们的出现顺序

S = lower(strrep(s, ' ', ''));
[values, ~, b] = unique(S, 'stable');

hist(b, unique(b))
set(gca, 'xtick', 1:numel(values), 'xticklabels', num2cell(values))
或者如果你想要所有字母的柱状图

S = lower(strrep(s, ' ', ''));
counts = histcounts(double(S), double('a':'z'));
bar(counts)
set(gca, 'xtick', 1:26, 'xticklabels', num2cell('a':'z'))

您可以使用
unique
的第一个输出为您提供唯一值,并将其用作x标签

[values, idxs, ~] = unique(s, 'last');

% Make sure that they aren't sorted
[~, sortind] = sort(idxs);
values = num2cell(values(sortind));

% And later after creating your bar plot
set(gca, 'xtick', 1:numel(values), 'XTickLabels', values);
或者,您可以只使用
unique
stable
输入来确保它们的出现顺序

S = lower(strrep(s, ' ', ''));
[values, ~, b] = unique(S, 'stable');

hist(b, unique(b))
set(gca, 'xtick', 1:numel(values), 'xticklabels', num2cell(values))
或者如果你想要所有字母的柱状图

S = lower(strrep(s, ' ', ''));
counts = histcounts(double(S), double('a':'z'));
bar(counts)
set(gca, 'xtick', 1:26, 'xticklabels', num2cell('a':'z'))

作为一种更简单的方法,这个怎么样

str = 'i love georgia and its nature';

num_times = zeros(26,1);
letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', ...
           'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

for k = 1:length(str)

    % Convert letter to its lower case, get ASCII value, 
    % a = 97, b = 98, ..., z = 122
    n = uint16(lower(str(k)));

    % If character between 'a' and 'z'
    if n < 122 && n > 97

        % Convert to be between 1 and 26
        n = n - 96;

        % Index count array with n        
        num_times(n) = num_times(n) + 1;

    end

end

clf
stem(num_times);
set(gca, 'XTick', 1:26);
set(gca,'XTickLabel', letters)
输出:


这是一种更简单的方法,怎么样

str = 'i love georgia and its nature';

num_times = zeros(26,1);
letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', ...
           'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

for k = 1:length(str)

    % Convert letter to its lower case, get ASCII value, 
    % a = 97, b = 98, ..., z = 122
    n = uint16(lower(str(k)));

    % If character between 'a' and 'z'
    if n < 122 && n > 97

        % Convert to be between 1 and 26
        n = n - 96;

        % Index count array with n        
        num_times(n) = num_times(n) + 1;

    end

end

clf
stem(num_times);
set(gca, 'XTick', 1:26);
set(gca,'XTickLabel', letters)
输出:



但是unique返回排序数组,这将在彼此之间混合东西我需要询问第二个代码S=lower(strrep(S,,,'');此代码将用无空格替换空格并转换为小写right@datodatuashvili是的,因为你们想忽略空格,你们想同时计算大写和小写字母。还有一个关于b值的问题,b值是多少they@datodatuashvili但unique返回排序数组,这将使事情相互混合我需要询问关于第二个代码S=lower(strep(S,,,'');此代码将用无空格替换空格并转换为小写right@datodatuashvili是的,因为你想忽略空格,想同时计算大小写字母,还有一个关于b值的问题,是什么they@datodatuashvili问题标题与实际问题/问题无关。在这种情况下,通常需要使用所有不相关字符的正则表达式清除输入,然后将其转换为同一大小写(WLG:小写)或不转换,具体取决于您定义的“唯一”字母。然后创建一个直方图,将字母表的ASCII值的字母作为BIN(
histcounts
使用
“整数”
的binning方法)。这也会显示根本不出现的字母。@P0W相关名称是什么?问题标题与实际问题/问题无关。通常情况下,在这种情况下要做的是使用所有不相关字符的正则表达式清除输入,然后将其转换为同一大小写(WLG:小写)或者不-取决于你定义的“唯一”字母。然后创建一个直方图,将字母表的ASCII值的字母作为BIN(
histcounts
使用
“整数”
的binning方法)。这也会向您显示根本没有出现的字母。@P0W相关名称是什么?如果您将访问格鲁吉亚,我很高兴,这是一个非常美丽的国家我正在使用您的示例字符串。。。这个代码对你有用吗?如果你访问格鲁吉亚,我很高兴,它是非常美丽的国家我使用你的示例字符串。。。这个代码对你有用吗?