Question

I want to loop through files and check whether each file contains any spelling errors.

 If yes then return 1 else -1.

Now, I can check whether there is a spelling error for 1 file. But it can't loop for more than 1 and return 1 if get error else -1.

checkSpelling.m file

function suggestion = checkSpelling(word)

h = actxserver('word.application');
h.Document.Add;
correct = h.CheckSpelling(word);
if correct
  suggestion = []; %return empty if spelled correctly
else
  %If incorrect and there are suggestions, return them in a cell array
  if h.GetSpellingSuggestions(word).count > 0
      count = h.GetSpellingSuggestions(word).count;
      for i = 1:count
          suggestion{i} = h.GetSpellingSuggestions(word).Item(i).get('name');
      end
  else
      %If incorrect but there are no suggestions, return this:
      suggestion = 'no suggestions';
  end

end
%Quit Word to release the server
h.Quit

f20.m file

for i = 1:10

data2=fopen(strcat('DATA\',int2str(i),''),'r')
CharData = fread(data2, '*char')';  %read text file and store data in CharData
fclose(data2);

 word = regexp(CharData, ' ', 'split')

[sizeData b] = size(word)

suggestion = cellfun(@checkSpelling, word, 'UniformOutput', 0)
if sum(cellfun(@isempty,suggestion))==0
feature20(i)=-1;
else
feature20(i)=1;
end
end

I get to loop for the file, and also checking, but it return me the wrong results when suggestion is empty (1)

enter image description here enter image description here

Était-ce utile?

La solution

Based on your comments, it sounds like you want the output to be a vector of values, one for each file. The values would be 1 if suggestion contains errors and -1 if it is empty.

The problem is that since suggestion is a cell array, you cannot compare it using the boolean operator ==' . If you want a vector of outputs, you will also need to indexfeature20` each iteration. Here's a potential fix.

It sounds like isempty(suggestion) gives the wrong answer, so try this one:

if sum(~cellfun(@isempty,suggestion))==0
    feature20(i)=-1;
else
    feature20(i)=1;
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top