这是我正在运行的代码:
folder = 'C:\...';
textfiles = dir(fullfile(folder, '*.txt'));
nfiles = length(textfiles);
data = cell(nfiles);
fid_out = fopen('C:\...', 'w');
for i = 1 : nfiles %loop through all files in the folder
fid = fopen(fullfile(folder, textfiles(i).name));
line = fgetl(fid);
fclose(fid);
s=textscan(text,'%s');
str=s{:};
[ii,jj,kk]=unique(str);
freq=hist(kk,(1:numel(jj))')';
for j = 1:length(ii)
word = ii{j};
count = freq(j);
fprintf(fid_out,'%d %s %d\r\n',i , word, count);
end
%i is the document index
end
我的输出文本文件如下所示(示例):
1 A 3
1 and 4
1 the 6
2 A 4
2 anode 5
3 time 7
...
这只是我输出文件的一个例子,有很多单词。
现在我想用一个数字替换单词,使它看起来像这样:
1 1 3
1 2 4
1 35 6
2 1 4
2 3 5
3 36 7
...
所以基本上,我想为一个特定的单词指定一个数字(意思是'A'将始终= 1,'和'= 2,依此类推),并用文本替换该文本。