On 20/10/11 17:36, Josh Cheek wrote:
> On Wed, Oct 19, 2011 at 11:29 PM, Sam Duncan<sduncan / wetafx.co.nz>  wrote:
>
>> ruby-1.9.2-p290 :002>  def find_frequency(prose, word)
>> ruby-1.9.2-p290 :003?>    prose.scan(/\b(#{word})\b/im).**length
>> ruby-1.9.2-p290 :004?>    end
>>
>>
> This was my approach, too. But by the time you make it robust, it's no
> longer elegant. i.e. it should really be escaped, leading to:
>
> def find_frequency(prose, word)
>    regex = Regexp.new "\\b#{Regexp.escape word}\\b", Regexp::IGNORECASE
>    prose.scan(regex).length
> end
>
Too true! Still not too bad =]

def find_frequency(prose, word)
   prose.scan(/\b(#{Regexp.escape word})\b/im).length
end


Sam