-
Notifications
You must be signed in to change notification settings - Fork 1
/
spellcheck.m
73 lines (66 loc) · 2.21 KB
/
spellcheck.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function [status,varargout] = spellcheck(text)
%SPELLCHECK checks the spelling of word(s) and returns suggestions if misspelled.
%
% [status,suggestions] = spellcheck(text);
% status = spellcheck(text);
%
% text: a word or string of words separated by space(s).
% status: returns '1' if words exist in dictionary or '0' otherwise.
% suggestions: an array of suggested corrections of the misspelled word
% or otherwise returns a message 'Correct Spelling'.
%
% Examples:
% [status,suggestions] = spellcheck('Hellow');
% [status,suggestions] = spellcheck('My Name is Fahad!');
% status = spellcheck('Emirates');
%
% Copyright 2004 Fahad Al Mahmood
% Version: 1.0 $ $Date: 30-Jun-2004
% Version: 1.5 $ $Date: 01-Jul-2004 (Multiple words check & status,
% thanks Michael Kleder!)
% Version: 1.6 $ $Date: 11-Jul-2004 (Fixed the 'no suggestions' result,
% thanks Sander Stepanov!)
% Separating string of words into arrays of words.
k=1;
temp='';
for n=1:length(text);
if ~isspace(text(n))
temp = [temp text(n)];
else
if ~isspace(text(n-1))
words{k} = temp;
end
temp='';
k=k+1;
end
end
words{k} = temp;
% Opening MS Word and Starting the Spelling Check
Doc = actxserver('Word.Application');
m=1;
for n=1:length(words)
if ~isempty(words{n})
status(m) = invoke(Doc,'CheckSpelling',words{n},[],1,1);
if nargout==2
invoke(Doc.Documents,'Add');
X = invoke(Doc,'GetSpellingSuggestions',words{n});
count = get(X,'Count');
suggestions{m,1} = words{n};
if count~=0
for k=2:count+1
suggestion = invoke(X,'Item',k-1);
suggestions{m,k} = get(suggestion,'Name');
end
elseif count==0 & status(m)==1
suggestions{m,2} = 'Correct Spelling';
elseif count==0 & status(m)==0
suggestions{m,2} = 'No Suggestions!';
end
varargout = {suggestions};
end
m=m+1;
end
end
status = all(status);
invoke(Doc,'Quit');
delete(Doc);