[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

simple way to turn "foo and bar" to "+foo +bar"

Max Williams

12/14/2007 5:49:00 PM

I want to add a slightly hacky feature into my boolean mysql search
which lets users write 'foo and bar', which is then translated into
'+foo +bar' (ie both 'foo' and 'bar' must be present) before being
passed to the search. Similarly, "foo and bar and snafu" should be
translated into "+foo +bar +snafu".

I'm sure there must be a simple way to do this but i'm new to ruby and
have got bogged down in loads of nested ifs and exceptions already.

Can anyone help with an elegant solution?

thanks
max
--
Posted via http://www.ruby-....

10 Answers

Phrogz

12/14/2007 6:02:00 PM

0

On Dec 14, 10:48 am, Max Williams <toastkid.willi...@gmail.com> wrote:
> I want to add a slightly hacky feature into my boolean mysql search
> which lets users write 'foo and bar', which is then translated into
> '+foo +bar' (ie both 'foo' and 'bar' must be present) before being
> passed to the search. Similarly, "foo and bar and snafu" should be
> translated into "+foo +bar +snafu".
>
> I'm sure there must be a simple way to do this but i'm new to ruby and
> have got bogged down in loads of nested ifs and exceptions already.
>
> Can anyone help with an elegant solution?

I feel like there's a more elegant one-pass regexp, but I haven't the
time to think much on it. So:

phrases = [
"foo",
"foo or bar",
"foo and bar",
"foo and bar and snafu"
]

phrases.each{ |phrase|
result = phrase.dup
:go while result.gsub!( /\+?(\S+)\s*and\s*(\S+)/, '+\\1 +\\2' )
puts "#{phrase.inspect} -> #{result.inspect}"
}

#=> "foo" -> "foo"
#=> "foo or bar" -> "foo or bar"
#=> "foo and bar" -> "+foo +bar"
#=> "foo and bar and snafu" -> "+foo +bar +snafu"

John Pywtorak

12/14/2007 6:13:00 PM

0

Hi max,

How about...

irb(main):001:0> "foo and bar".sub(/(foo) and (bar)/, "+#{$1} +#{$2}")
=> "+foo +bar"
irb(main):002:0> "foo and bar".sub(/(foo) and (bar)/, '+\1 +\2')
=> "+foo +bar"
irb(main):003:0> "foo and bar and snafu".sub(/(foo) and (bar) and
(snafu)/, '+\1 +\2 +\3')
=> "+foo +bar +snafu"

or

irb(main):004:0> "foo and bar and snafu".gsub(/(\w+)\s+and/,
'+\1').sub(/(\w+)$/, '+\1')
=> "+foo +bar +snafu"

Max Williams wrote:
> I want to add a slightly hacky feature into my boolean mysql search
> which lets users write 'foo and bar', which is then translated into
> '+foo +bar' (ie both 'foo' and 'bar' must be present) before being
> passed to the search. Similarly, "foo and bar and snafu" should be
> translated into "+foo +bar +snafu".
>
> I'm sure there must be a simple way to do this but i'm new to ruby and
> have got bogged down in loads of nested ifs and exceptions already.
>
> Can anyone help with an elegant solution?
>
> thanks
> max

MenTaLguY

12/14/2007 6:14:00 PM

0

On Sat, 15 Dec 2007 02:48:46 +0900, Max Williams <toastkid.williams@gmail.com> wrote:
> I want to add a slightly hacky feature into my boolean mysql search
> which lets users write 'foo and bar', which is then translated into
> '+foo +bar' (ie both 'foo' and 'bar' must be present) before being
> passed to the search. Similarly, "foo and bar and snafu" should be
> translated into "+foo +bar +snafu".
>
> I'm sure there must be a simple way to do this but i'm new to ruby and
> have got bogged down in loads of nested ifs and exceptions already.
>
> Can anyone help with an elegant solution?

query = "foo and bar and baz"
nil while query.sub!(/\+*(\w+)\s+and\s+(\w+)/, '+\1 +\2')
puts query

-mental


Rolando Abarca

12/14/2007 6:19:00 PM

0

On Dec 14, 2007, at 2:48 PM, Max Williams wrote:

> I want to add a slightly hacky feature into my boolean mysql search
> which lets users write 'foo and bar', which is then translated into
> '+foo +bar' (ie both 'foo' and 'bar' must be present) before being
> passed to the search. Similarly, "foo and bar and snafu" should be
> translated into "+foo +bar +snafu".
>
> I'm sure there must be a simple way to do this but i'm new to ruby and
> have got bogged down in loads of nested ifs and exceptions already.
>
> Can anyone help with an elegant solution?

"foo and bar and baz".split(/\s+and\s+/i).map{|s| "+#{s}"}.join(' ')

> thanks
> max

regards,
--
Rolando Abarca
Phone: +56-9 97851962
My Amazon Wish List: http://www.amazon.com/gp/registry/wishlist/3AG...


MenTaLguY

12/14/2007 6:23:00 PM

0

On Sat, 15 Dec 2007 03:19:22 +0900, Rolando Abarca <funkaster@gmail.com> wrote:
>> Can anyone help with an elegant solution?
>
> "foo and bar and baz".split(/\s+and\s+/i).map{|s| "+#{s}"}.join(' ')

I think this one is best.

-mental


James Moore

12/14/2007 6:37:00 PM

0

Do you need to worry about phrases?

Something like:

require 'pp'
f = "foo and bar AND north dakota"
result = f.split(/\s+and\s+/i).inject('') {|memo, obj| memo +
"+(#{obj}) "}.strip
pp result

=> "+(foo) +(bar) +(north dakota)"

--
James Moore | james@restphone.com
Ruby and Ruby on Rails consulting
blog.restphone.com

Phrogz

12/14/2007 6:48:00 PM

0

On Dec 14, 11:22 am, MenTaLguY <men...@rydia.net> wrote:
> On Sat, 15 Dec 2007 03:19:22 +0900, Rolando Abarca <funkas...@gmail.com> wrote:
> >> Can anyone help with an elegant solution?
>
> > "foo and bar and baz".split(/\s+and\s+/i).map{|s| "+#{s}"}.join(' ')
>
> I think this one is best.

It's elegant, but it assumes that the search phrase always has "and".
Although not originally specified either way, that seems like an
unlikely assumption. But then, I don't know the problem domain.

John Pywtorak

12/14/2007 6:57:00 PM

0

I could not resist, so a slight mod ;-)

"+" << "foo and bar and baz".split(/\s+and\s+/i).join(' +')

irb(main):001:0> "+" << "foo and bar and baz".split(/\s+and\s+/i).join(' +')
=> "+foo +bar +baz"

Johnny P


MenTaLguY wrote:
> On Sat, 15 Dec 2007 03:19:22 +0900, Rolando Abarca <funkaster@gmail.com> wrote:
>>> Can anyone help with an elegant solution?
>> "foo and bar and baz".split(/\s+and\s+/i).map{|s| "+#{s}"}.join(' ')
>
> I think this one is best.
>
> -mental
>
>
>

mike.s.mckinney

12/14/2007 7:12:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

just pass in the delimeter...

test.rb:
and_str = "foo and bar AND baz"
pipe_str = "foo|bar| baz"

def translate(str, del)
str.split(/\s*#{del}\s*/i).map{|s| "+#{s}"}.join(' ')
end

puts translate(and_str, 'and')
puts translate(pipe_str, '\|')


On Dec 14, 2007 1:50 PM, Phrogz <phrogz@mac.com> wrote:

> On Dec 14, 11:22 am, MenTaLguY <men...@rydia.net> wrote:
> > On Sat, 15 Dec 2007 03:19:22 +0900, Rolando Abarca <funkas...@gmail.com>
> wrote:
> > >> Can anyone help with an elegant solution?
> >
> > > "foo and bar and baz".split(/\s+and\s+/i).map{|s| "+#{s}"}.join(' ')
> >
> > I think this one is best.
>
> It's elegant, but it assumes that the search phrase always has "and".
> Although not originally specified either way, that seems like an
> unlikely assumption. But then, I don't know the problem domain.
>
>

Max Williams

12/15/2007 9:03:00 AM

0


Awesome, thanks a lot everyone!

max

--
Posted via http://www.ruby-....