如何摆脱标点符号?并检查拼写错误

时间:2014-05-06 18:06:50

标签: matlab matlab-figure

  • 消除标点符号
  • 在遇到新行和空格时拆分,然后存储在数组中
  • 使用checkSpelling.m文件
  • 的功能检查文本文件是否有错误
  • 总结该文章中的错误总数
  • 没有任何建议被认为是没有错误,然后返回-1
  • 错误总和> 20,返回1
  • 错误总和< = 20,返回-1

我想检查某段的拼写错误,我面临摆脱标点符号的问题。它可能有问题,另一个原因,它返回错误如下:

enter image description here enter image description here

我的data2文件是:

enter image description here

checkSpelling.m

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 suggestion';
  end

end
%Quit Word to release the server
h.Quit    

f19.m

for i = 1:1

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

word_punctuation=regexprep(CharData,'[`~!@#$%^&*()-_=+[{]}\|;:\''<,>.?/','')

word_newLine = regexp(word_punctuation, '\n', 'split')

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

[sizeData b] = size(word)

suggestion = cellfun(@checkSpelling, word, 'UniformOutput', 0)

A19(i)=sum(~cellfun(@isempty,suggestion))

feature19(A19(i)>=20)=1
feature19(A19(i)<20)=-1
end

1 个答案:

答案 0 :(得分:0)

regexprep来电替换为

word_punctuation=regexprep(CharData,'\W','\n');

此处\W找到所有替换为换行符的非字母数字字符(包含空格)。

然后

word = regexp(word_punctuation, '\n', 'split');

如您所见,您不需要按空格分割(见上文)。但你可以删除空单元格:

word(cellfun(@isempty,word)) = [];

一切都适合我。不过我不得不说你checkSpelling功能很慢。在每次调用时,它都必须创建一个ActiveX服务器对象,添加新文档,并在检查完成后删除该对象。考虑重写函数以接受字符串的单元格数组。

<强>更新

我看到的唯一问题是删除引用'字符(我是,不要等)。您可以使用下划线(是的,它被认为是字母数字)或任何未使用的字符序列临时替换它们。或者,您可以使用方括号中的所有非字母数字字符列表而不是\W来删除。

更新2

第一次更新的另一个解决方案:

word_punctuation=regexprep(CharData,'[^A-Za-z0-9''_]','\n');
相关问题