From: Jens Wille Date: 2008-12-08T23:22:46+09:00 Subject: Re: Proximity searches in Ruby hi! Robert Klemme [2008-12-07 13:23]: > I played a bit around and came up with a more involved version > which works with arbitrary numbers of words but still fits into a > few lines of code. here's my take on the task: Poor Man's Search ;-) it's a different approach with support for boolean operators as well as - back to topic! - proximity operators. pms builds an index for the input documents and stores the token positions along with the document numbers. still pretty rough, but working... > text = < Hello world, how are you today? I said "Hello" > to the other guy but he would not answer although > all the world could hear me. > EOT > > proximity_search text, 3, "hello", "World" do |a,b| > printf "Found at char positions %3d and %3d\n", a, b > end require 'pms/ext' search = text.search('hello').near('world', 3) p search.results #=> [0] p search.results_with_positions #=> {0=>[0, 8]} p search.matches #=> ["Hello world, how are you today? I said \"Hello\"\n"] cheers jens