How to use spell checker with matlab?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I have written few words in text file result.txt using matlab....but there are some spelling mistakes on it... i am doing character recognition...How should i correct the spelling and show the result in the next line in the same file result.txt....can anyone help
채택된 답변
You could try this:
UPDATE : here is a slightly modified version (also attached)
function wordsChecked = checkWordsSpelling( words )
%
% Based on Mathworks thread:
% http://www.mathworks.com/matlabcentral/answers/91885-is-there-any-way-to-check-spelling-from-within-matlab
%
% - Split space-separated words into cell array of words, or wrap
% single word into cell array.
if ischar( words )
if any( words == ' ' )
words = strsplit( words, ' ' ) ;
else
words = {words} ;
end
end
% - Launch MS Word and create document.
h = actxserver( 'word.application' ) ;
h.Document.Add ;
% - Build cell array of originals and suggestions.
words = words(:) ; % -> columns cell array.
nWords = numel( words ) ;
for wId = 1 : nWords
% - Check if spelling correct. Loop back if so.
isCorrect = h.CheckSpelling( words{wId,1} ) ;
words{wId,2} = isCorrect ;
if isCorrect
words{wId,3} = false ;
continue ;
end
% - Build cell array of suggestions.
nSug = h.GetSpellingSuggestions( words{wId,1} ).count;
words{wId,3} = nSug > 0 ;
if nSug > 0
for sId = 1 : nSug
words{wId,4}{sId} = ...
h.GetSpellingSuggestions( words{wId,1} ).Item(sId).get( 'name' ) ;
end
end
end
% - Quit MS Word.
h.Quit
% - Build table (or struct array if you prefer).
%wordsChecked = cell2struct( words, {'original', 'isCorrect', 'hasSuggestion', 'suggestion'}, 2 ) ;
wordsChecked = cell2table( words, 'VariableNames', {'original', 'isCorrect', 'hasSuggestion', 'suggestion'} ) ;
end
With that, you can do the following:
>> checked = checkWordsSpelling( 'Helloo' )
checked =
original isCorrect hasSuggestion suggestion
________ _________ _____________ _______________________________
'Helloo' false true 'Hello' 'Halloo' 'Hellos'
>> checked = checkWordsSpelling( 'Helloo Wolrd Hello' )
checked =
original isCorrect hasSuggestion suggestion
________ _________ _____________ __________
'Helloo' false true {1x3 cell}
'Wolrd' false true {1x2 cell}
'Hello' true false []
>> checked.suggestion{2}
ans =
'World' 'Word'
>> checked = checkWordsSpelling( {'Helloo', 'Wolrd'} )
checked =
original isCorrect hasSuggestion suggestion
________ _________ _____________ __________
'Helloo' false true {1x3 cell}
'Wolrd' false true {1x2 cell}
Hope it helps!
댓글 수: 4
PS : so the code should look like the following. Using a struct/cell array as output of the spell checking function would allow you to write the full thing in 6 lines I guess, but this gives you a starting point.
% - Read file and check spelling.
checked = checkWordsSpelling( fileread( 'data.txt' )) ;
% - Etxract originals or suggestions.
nWords = size( checked, 1 ) ;
output = cell( nWords, 1 ) ;
for wId = 1 : nWords
if checked.hasSuggestion(wId)
output{wId} = checked.suggestion{wId}{1} ;
else
output{wId} = checked.original{wId} ;
end
end
% - Build space-separated string of words.
output = strtrim( sprintf( '%s ', output{:} )) ;
% - Export to file.
fId = fopen( 'dataCorrected.txt', 'w' ) ;
fwrite( fId, output ) ;
fclose( fId ) ;
peyush
2015년 7월 27일
thanks
Isabelle Goy
2023년 5월 12일
Hi,
thanks for the code provided. I'm using a similar spell-check on my side but I need that the spell-check done through Word uses french as a refernce language. I have tried to set it this way:
h = actxserver( 'word.application' ) ;
h.Document.Add ;
selection = h.Selection;
selection.LanguageID = 1036; % which is the code for french language
but also the code is running, this line is not taken into consideration when the spell checking is happening, or at the leat the spell checking is still happening on an english base ?
Any suggestion/help aprreciated.
Some words produce an error
checked = checkWordsSpelling( 'Procuremend' )
Error using cell2table (line 77)
The VariableNames property must contain one name for each variable in the table.
Error in checkWordsSpelling (line 52)
wordsChecked = cell2table( words, 'VariableNames', {'original', 'isCorrect', ...
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
