1

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

1 Answers1

0

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
Marcus
  • 450
  • 4
  • 14
  • Hi Marcus, I have tried adding the code above but its still giving me same error. –  Oct 08 '17 at 01:39
  • Hi Marcus, I have tried adding the above code but its still giving me same error. I have also changed code with UTF-8 when opening the file.. any suggestions? –  Oct 08 '17 at 01:48
  • That you get the same error seems unlogical as the error occured in that one line you mentioned which is now part of the if else statement. Of course you have to handle the case yourself, as the comment says. I added one suggestion how you might solve your problem. – Marcus Oct 08 '17 at 07:33
  • Hello @sruthiyeruva, I think I have adequately answered your question. Please consider accepting it as answer, as this is what rewards authors for their work. Thank you – Marcus Oct 09 '17 at 10:02
  • I think you have a different problem. I downloaded your hello.txt, reproduced your error in exactly the line you mentioned and ran the modified code, finishing without any error thrown. Can you please elaborate on how your code fails now? – Marcus Oct 10 '17 at 03:55
  • I hope I understood you right but this is how I modified my code.Thanks –  Oct 10 '17 at 04:43