Code covered by the BSD License  

Highlights from
Dict Search from Text File

Dict Search from Text File

by

 

This function reads a line from text file & generate all possible combination of words

dictsearch()
function [strstruct]= dictsearch()
% This function reads a line from text file & generate all possible
% combination of words & check with inbuilt dictionary available with Word
% Application
% $ Created By Konark Kelaiya  (Dec, 2011)
%% Read  file & prepare data
txtpath = fullfile(pwd, sprintf('\\word.txt'));


fid= fopen(txtpath,'r');
% s =fscanf(fid,'%s');

InputText=textscan(fid,'%s',4,'delimiter','\n');
linescan = size(InputText{1,1},1);
linenumber =[];

for i = 1:linescan
    var = double(InputText{1,1}{i,1}); % convert string to ascii
    index = var >=65 & var <=122; % remove character other than alphabet
    InputText{1,1}{i,1} = char(var(index));
    linenumber = [linenumber;sprintf('TxtLine%d',i)]; %#ok<AGROW>
end

% xvar = cellfun(@double,InputText{1,1},'UniformOutput',false);
% index = cellfun(@(x) find(x>=65 & x<=122),xvar,'UniformOutput',false);
% for i=1:size(index,1)
%     indexmat = cell2mat(index(i));
%     xvarmat = cell2mat(xvar(i));
%     InputText{1,1}{i,1} = char(xvar(indexmat));
% end
filescan = cell2struct(InputText{1,1},linenumber);
filefield = fields(filescan);
strstruct = struct();

%% invoke doc COM
Doc = actxserver('Word.Application');

%% Check All Possible combination for possible words

for i = 1:size(filefield,1)
    strint = '' ; %Empty string;
    strstruct = setfield(strstruct,filefield{i},''); %#ok<SFLD> % Create Output Structure
    str = filescan.(filefield{i}); % Get the String from structure
    for j = 3:length(str)
        combinations = num2cell(nchoosek(1:length(str),j)); %Get Combinations
        for s = 1:size(combinations,1)
            cellcombinations{s} = cell2mat(combinations(s,:)); %#ok<AGROW,*SAGROW>
        end
        %         if size(cellcombinations,2)>size(combinations,1)
        %             index =(size(cellcombinations,2)-size(combinations,1));
        %             cellcombinations(index+1:end)=[]; % remove extra combination
        %         end
        %         cellcombinations = cellfun(@transpose,cellcombinations,'UniformOutput',false);
        cellcombinations = transpose(cellcombinations);
        xstr = cellfun(@(x) str(x(:,:)),cellcombinations,'UniformOutput',false);
        
        for p = 1:size(xstr,1)
            xstr{p,2}=perms(1:j); % Get permutations
            x = xstr{p,1}; k = xstr{p,2};
            xstr{p,3} = x(k);
            for w = 1:size(xstr{p,3},1)
                st = xstr{p,3}(w,:);
                status = invoke(Doc,'Checkspelling',st,[],1,1);  % Check Spelling from Word Dictionary
                if status ==1
                    strint = char(strint,st);
                end
            end
        end
        clear cellcombinations x k; % flush variables
        
    end
    clear xstr; %flush strings created temporary
    uniquestr = unique(cellstr(strint));
    ind = cell2mat(cellfun(@isempty,uniquestr,'UniformOutput',false));
    uniquestr(ind) =[]; % remove any empty string
    strstruct.(filefield{i}) = uniquestr;
end

%% Quit doc COM

invoke(Doc,'Quit');
delete(Doc);


Contact us