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

M

Max Williams

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
 
P

Phrogz

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"
 
J

John Pywtorak

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"
 
M

MenTaLguY

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
 
R

Rolando Abarca

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,
 
J

James Moore

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)"
 
P

Phrogz

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.
 
J

John Pywtorak

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
 
M

Mike McKinney

[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, '\|')


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top