From: Dave Thomas Date: 2001-03-01T02:55:02+09:00 Subject: [ruby-talk:11794] Re: building n-grams Arno Erpenbeck writes: > Example: > Input "The man sees the boy with the telescope." > Output "The, ma,n s,ees, th,e b,oy ,wit,h t,he ,tel,esc,ope," > Desired output "The,he ,e m, ma,man,..." One solutoin is simply to index slices out of the string. Here's an example wrapped as an iterator, but that's kind of incidental. def ngram(str, n) pos = 0 while pos <= str.length - n yield str[pos, n] pos += 1 end end ngram("The man sees the boy with the telescope.", 3) {|txt| puts txt} produces: The he e m ma man ... cop ope pe. Regards Dave