我试着计算并绘制出比克频率的分布,首先,我生成了所有可能的比格,给出了1296个比例尺。
然后从给定的文件中提取出大图,并将它们保存在words1中。
我的问题是如何为文件a.txt计算这1296个比例尺的频率?如果文件中根本没有出现一些大图,那么它们的频率应该是零。
a.txt是任何文本文件
clear
clc
%************create bigrams 1296 ***************************************
chars ='1234567890abcdefghijklmonpqrstuvwxyz';
chars1 ='1234567890abcdefghijklmonpqrstuvwxyz';
bigram='';
for i=1:36
for j=1:36
bigram = sprintf('%s%s%s',bigram,chars(i),chars1(j));
end
end
temp1 = regexp(bigram, sprintf('\\w{1,%d}', 1), 'match');
temp2 = cellfun(@(x,y) [x '' y],temp1(1:end-1)', temp1(2:end)','un',0);
bigrams = temp2;
bigrams = unique(bigrams);
bigrams = rot90(bigrams);
bigram = char(bigrams(1:end));
all_bigrams_len = length(bigrams);
clear temp temp1 temp2 i j chars1 chars;
%****** 1. Cleaning Data ******************************
collection = fileread('e:\a.txt');
collection = regexprep(collection,'<.*?>','');
collection = lower(collection);
collection = regexprep(collection,'\W','');
collection = strtrim(regexprep(collection,'\s*',''));
%*******************************************************
temp = regexp(collection, sprintf('\\w{1,%d}', 1), 'match');
temp2 = cellfun(@(x,y) [x '' y],temp(1:end-1)', temp(2:end)','un',0);
words1 = rot90(temp2);
%*******************************************************
words1_len = length(words1);
vocab1 = unique(words1);
vocab_len1 = length(vocab1);
[vocab1,void1,index1] = unique(words1);
frequencies1 = hist(index1,vocab_len1);发布于 2014-09-12 12:25:58
I.字符串的字符计数问题
基于bsxfun的字符计数解决方案-
counts = sum(bsxfun(@eq,[string1-0]',65:90))产出-
counts =
2 0 0 0 0 2 0 1 0 0 ....如果你想用表格输出每一个字母的计数-
out = [cellstr(['A':'Z']') num2cell(counts)']产出-
out =
'A' [2]
'B' [0]
'C' [0]
'D' [0]
'E' [0]
'F' [2]
'G' [0]
'H' [1]
'I' [0]
....请注意,这是一个区分大写字母的计数.
对于小写字母计数,请对前面的代码使用此编辑-
counts = sum(bsxfun(@eq,[string1-0]',97:122))对于不区分大小写的计数,用这个-
counts = sum(bsxfun(@eq,[upper(string1)-0]',65:90))二.比格计数案
假设您将所有可能的bigram保存在一个一维单元格数组bigrams1中,并且从文件中传入的bigram被保存到另一个单元格数组words1中。让我们也假设其中的某些价值,以供示范-
bigrams1 = {
'ar';
'de';
'c3';
'd1';
'ry';
't1';
'p1'}
words1 = {
'de';
'c3';
'd1';
'r9';
'yy';
'de';
'ry';
'de';
'dd';
'd1'}现在,您可以通过以下代码从words1中获得bigrams1中存在的大图的计数-
[~,~,ind] = unique(vertcat(bigrams1,words1));
bigrams_lb = ind(1:numel(bigrams1)); %// label bigrams1
words1_lb = ind(numel(bigrams1)+1:end); %// label words1
counts = sum(bsxfun(@eq,bigrams_lb,words1_lb'),2)
out = [bigrams1 num2cell(counts)]代码运行的输出是-
out =
'ar' [0]
'de' [3]
'c3' [1]
'd1' [2]
'ry' [1]
't1' [0]
'p1' [0]结果表明-第一个元素ar从所有可能的bigram列表中没有在words1中找到;第二个元素de在words1中有三次出现,等等。
发布于 2014-09-12 11:22:43
嘿,类似于丹尼斯的解决方案,您可以只使用histc()
string1 = 'ASHRAFF'
histc(string1,'ABCDEFGHIJKLMNOPQRSTUVWXYZ')这将检查由字符串'ABCDEFGHIJKLMNOPQRSTUVWXYZ' (希望是字母表)定义的回收箱中的条目数(只是写得很快,所以没有保存)。结果是:
Columns 1 through 21
2 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0
Columns 22 through 26
0 0 0 0 0只是对我的解决方案做了一点修改:
string1 = 'ASHRAFF'
alphabet1='A':'Z'; %%// as stated by Oleg Komarov
data=histc(string1,alphabet1);
results=cell(2,26);
for k=1:26
results{1,k}= alphabet1(k);
results{2,k}= data(k);
end如果您现在查看results,您可以很容易地检查它是否有效:D
发布于 2014-09-12 14:39:43
这个答案创建了所有的大写,文件中的加载做了一些清理,ans然后使用unique和histc的组合来计数行。
生成所有的大图
注意,这里的顺序很重要,因为唯一将数组排序,这样就可以预先创建数组,以便输出与期望相匹配;
[y,x] = ndgrid(['0':'9','a':'z']);
allBigrams = [x(:),y(:)];阅读文件
这将移除大写,只需提取任意0-9或a-z字符,然后创建以下列向量
fileText = lower(fileread('d:\loremipsum.txt'));
cleanText = regexp(fileText,'([a-z0-9])','tokens');
cleanText = cell2mat(vertcat(cleanText{:}));从文件中移动一个并连接起来创建大图
fileBigrams = [cleanText(1:end-1),cleanText(2:end)];获得计数
将所有bigram的集合添加到我们的集合中(因此,所有可能的值都会被创建)。然后使用...,1296的第三个输出为每个唯一的行分配一个值∈{1,2,unique}。然后用histc创建计数,其中的回收箱等于unique输出的一组值,从每个回收站减去1,以删除我们添加的完整的集合比格。
[~,~,c] = unique([fileBigrams;allBigrams],'rows');
counts = histc(c,1:1296)-1;显示
查看文本计数
[allBigrams, counts+'0']或者是更有用的东西..。
[sortedCounts,sortInd] = sort(counts,'descend');
[allBigrams(sortInd,:), sortedCounts+'0']
ans =
or9
at8
re8
in7
ol7
te7
do6 ...https://stackoverflow.com/questions/25806657
复制相似问题