kgramcount.m

来自「loading a text file from a directory to 」· M 代码 · 共 43 行

M
43
字号
% k-gram Counting Program
clc;
clear all;
close all;
%% Read the cyphered text from a *.txt file
text_input = loadtext('Decrypt_3.txt');
%% Remove the blanks from input text.
index = find(text_input ~= ' ');
text = text_input(index);
%% *****************************************************

len=length(text);
result=zeros(len,3);
spaces=[];

% Length of text block you are searching for repeats of
k=2;
% *****************************************************


m=k-1;
for n=1:len-m
    dig=text(n:n+m);
    for j=n+1:len-m
        if text(j:j+m)==dig
            space=j-n;
            spaces=[spaces space];
        end
    end
end

for guess=2:len
    result(guess,1)=guess; 
    result(guess,2)=length(find(mod(spaces,guess)==0));
    result(guess,3)=length(find(mod(spaces,guess)~=0));
end

% The first result column gives the period length guess
% The second result column gives the number of repeats that evenly divide the guess
% The third result column gives the number of repeats that don't evenly divide the guess

% Large values in column 2 indicate that the corresponding guess divides the actual period
results=result(2:end,:)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?