音节词索引超出了矩阵维度。软件

Syllables Words Index exceeds matrix dimensions. Matlab

我的代码可以正常工作,但有一个小问题 - 我遇到了索引超出矩阵维度的错误。当文本文件很大时会出现此错误。有人可以帮我解决这个问题吗?

Index exceeds matrix dimensions.
Error in getPolySyllWords>getSyllable (line 38)
InnerCode = InnerCode{1};
Error in getPolySyllWords>@(x)getSyllable(x)
Error in getPolySyllWords (line 8)
TextSyl = cellfun(@(x) getSyllable(x), TextCell);

我遇到了上述错误

function Syl = getSyllable(Word)
   if nargin == 0
       Word = input('What word do you want? ', 's');
   end
  if isempty(Word)
    Syl = 0;
    return
  end
Word = strrep(Word, ' ', '');

try
   Txt = webread(sprintf('http://www.dictionary.com/browse/%s?s=t', Word));
catch
    warning('Could not determine syllable for "%s". Returning 0.', Word);
    Syl = 0;
return
end

if isempty(InnerCode)
InnerCode = InnerCode{1};
CodeSrch2 = '>(?<Phonetics>[^\<]+)';
Phonetics = regexp(InnerCode, CodeSrch2, 'tokens');
Phonetics = [Phonetics{1}{:}];
else
% handle this case
Syl=1;
   return;
end

问题是,你的电话

SiteTxt = webread(sprintf('http://www.dictionary.com/browse/%s?s=t', Word));

对于单词 "Dr" returns 一些站点文本,其中标签

<span class="pron spellpron">[someResult] </span>

您正在扫描 SiteText 是否仍为空。这在下一行

InnerCode = regexp(SiteTxt, CodeSrch1, 'tokens');

生成一个空数组,导致 "Index exceeds matrix dimension" 错误。

大家可以自己测试一下,在首页直接输入"doctor"和"dr"这两个不同的词时:

请通过类似以下代码片段的方式扩展您的代码以处理这些情况:

if ~isempty(InnerCode)
    InnerCode = InnerCode{1};
    CodeSrch2 = '>(?<Phonetics>[^\<]+)';
    Phonetics = regexp(InnerCode, CodeSrch2, 'tokens');
    Phonetics = [Phonetics{1}{:}];
else
    % handle this case
    Syl=0;
    return;
end