From: gotoken@... (GOTO Kentaro) Date: 2001-03-01T03:14:19+09:00 Subject: [ruby-talk:11799] Re: building n-grams In message "[ruby-talk:11788] building n-grams" on 01/03/01, Arno Erpenbeck writes: >while line = gets > line.gsub(/[a-zA-Z\s]{3,3}/) {|p| print "#{p},"} >end > >However, this makes "too big" steps because the regexp matches one >triple and then the next one behind it, but no overlaps. There must be a >simple solution I guess. > >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,..." Here is straightforward one: class Integer def gram_of(str) res = [] for i in 0..str.size-self res.push str[i,self] end res end end str = "The man sees the boy with the telescope." puts "## 3gram", 3.gram_of(str) puts "## 100gram", 100.gram_of(str) -- Gotoken