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

3 Ansichten (letzte 30 Tage)
Zubier  Abdullah
Zubier Abdullah am 12 Aug. 2017
Kommentiert: per isakson am 13 Aug. 2017
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)

Antworten (1)

Walter Roberson
Walter Roberson am 12 Aug. 2017
fprintf(fid2, '%s\n', words{:});
fclose(fid2)

Kategorien

Mehr zu Standard File Formats finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by