how do i save unique words from a cell to a text file?
4 views (last 30 days)
Show older comments
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 Comment
Answers (1)
See Also
Categories
Find more on Standard File Formats in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!