how do i save unique words from a cell to a text file?

조회 수: 3 (최근 30일)
Zubier  Abdullah
Zubier Abdullah 2017년 8월 12일
댓글: per isakson 2017년 8월 13일
Hey guys
I am trying to go through a number of text files and I want to find all of the unique words and save them in a different text file (dictionary.txt) . After doing that, I want to compare all of the original documents and replace the words with the position of the words from dictionary.txt. Can anyone help me find some code examples that I can follow?
Say - dictionary contains 'Goat', 'Tommy', 'Bag', 'is', 'wild', 'a' Txt file 1 has - Tommy is a wild goat would become 24651
This is the code I have written so far. I cannot save the file.
clc
clear all
close all
fid = fopen('test.txt');
fgetl(fid);fgetl(fid); %we are gettin rid of the first 2 lines of the thing using fgetl
words = textscan(fid,'%s'); %we are text scanning the words from the document
status = fclose(fid)
lower(words{1,1}) %we are making all of the words lowercase
%here we are getting rid of all of the words which are one letter
for i = 1:numel(words{1,1})
if size(words{1,1}{i,1}, 2) == 1
words{1,1}{i,1} = ' ';
end
end
%%Get rid of all Non words(Numbers)
for i=1:numel(words{1,1})
ind = find(isstrprop(words{1,1}{i,1}, 'digit') == 1);
words{1,1}{i,1}(ind)=[];
end
for i=1:numel(words{1,1})
ind = find(isstrprop(words{1,1}{i,1}, 'alphanum') == 0);
words{1,1}{i,1}(ind)=[];
end
mkdir('output')
dictionary = fopen('dict.txt','w')
[words,frequency,position] = unique(words{1,1})
words = lower(words)
fid2 = fopen('dict.txt','w')
fwrite(fid2,words)
  댓글 수: 1
per isakson
per isakson 2017년 8월 13일
Do all of the text files fit in a fraction of the memory (RAM)?

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 8월 12일
fprintf(fid2, '%s\n', words{:});
fclose(fid2)

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by