matlabindexingexceed

Syllables Words Index exceeds matrix dimensions. Matlab


I got this code working but there is a small issue - I'm getting an error with Index exceeds matrix dimensions. This error is coming up when the text file is big. can anybody help me with this please?

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);

I am getting above error

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

Solution

  • The problem is, that your call

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

    for the Word "Dr" returns some SiteText where the Tag

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

    you are scanning the SiteText for remains empty. This in the following line

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

    produces an empty array which causes the "Index exceeds matrix dimension" error.

    You can test it yourself, when entering the two different words "doctor" and "dr" directly on the homepage:

    enter image description here

    Please extend your code by something like the following code snippet to handle these cases:

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