[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Method to get string combinations

Christoph Blank

2/11/2009 10:22:00 AM

Hi,

I have a text like this:

"This {is|was} a {good|nice} day" and want to generate possible
combinations like:

This is a good day
This was a good day
This is a nice day
This was a nice day

I tried using zsh functionality to output this, but it doesn't seem that
easy.
Does anyone know an easy way how to do this in ruby?

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

8 Answers

Jesús Gabriel y Galán

2/11/2009 10:39:00 AM

0

On Wed, Feb 11, 2009 at 11:22 AM, Christoph Blank <cb@unused.at> wrote:
> Hi,
>
> I have a text like this:
>
> "This {is|was} a {good|nice} day" and want to generate possible
> combinations like:
>
> This is a good day
> This was a good day
> This is a nice day
> This was a nice day
>
> I tried using zsh functionality to output this, but it doesn't seem that
> easy.
> Does anyone know an easy way how to do this in ruby?

There was a similar question on this list recently, I'll try to find
it later for reference. For a ruby quiz
(http://rubyquiz.com/qu...) I built a regexp "generator". It
adds a method to the Regexp to generate all possible strings that
match the regexp. If you use my code:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Then this works:

irb(main):001:0> require 'quiz143'
=> true
irb(main):002:0> a = "This {is|was} a {good|nice} day"
=> "This {is|was} a {good|nice} day"
irb(main):006:0> s = a.gsub(/\{(.*?)\}/, "(\\1)")
=> "This (is|was) a (good|nice) day"
irb(main):008:0> Regexp.new(s).generate
=> ["This is a good day", "This is a nice day", "This was a good day",
"This was a nice day"]

I'm changing your string to a valid regexp, changing all occurrences
of {} with () (depending on your case you might want to change that).
Then I create a regexp with that and call my method.

This supports many constructs from regexps, which might be overkill
for your problem. For sure there will be easier solutions for your
specific case. Anyway, I hope you find it interesting.

Jesus.

Christoph Blank

2/11/2009 10:47:00 AM

0

Wow, this is really useful! Especially in a general way like that.

Thanks a lot for the quick and helpful answer!

greetings,
Christoph

Jesús Gabriel y Galán wrote:
> On Wed, Feb 11, 2009 at 11:22 AM, Christoph Blank <cb@unused.at> wrote:
>> This was a nice day
>>
>> I tried using zsh functionality to output this, but it doesn't seem that
>> easy.
>> Does anyone know an easy way how to do this in ruby?
>
> There was a similar question on this list recently, I'll try to find
> it later for reference. For a ruby quiz
> (http://rubyquiz.com/qu...) I built a regexp "generator". It
> adds a method to the Regexp to generate all possible strings that
> match the regexp. If you use my code:
>
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>
> Then this works:
>
> irb(main):001:0> require 'quiz143'
> => true
> irb(main):002:0> a = "This {is|was} a {good|nice} day"
> => "This {is|was} a {good|nice} day"
> irb(main):006:0> s = a.gsub(/\{(.*?)\}/, "(\\1)")
> => "This (is|was) a (good|nice) day"
> irb(main):008:0> Regexp.new(s).generate
> => ["This is a good day", "This is a nice day", "This was a good day",
> "This was a nice day"]
>
> I'm changing your string to a valid regexp, changing all occurrences
> of {} with () (depending on your case you might want to change that).
> Then I create a regexp with that and call my method.
>
> This supports many constructs from regexps, which might be overkill
> for your problem. For sure there will be easier solutions for your
> specific case. Anyway, I hope you find it interesting.
>
> Jesus.

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

Giampiero Zanchi

2/11/2009 11:01:00 AM

0

ciao
I'm a beginner...
this code produces a partial result; try to improve and finalize it

my_text = "This {is|was} a {good|nice} day"
r = /\{(.*?)\}/
elem = my_text.split(r)
elem.map! {|x| x.split("|")}
p elem

output:
[["This "], ["is", "was"], [" a "], ["good", "nice"], [" day"]]

curious to see the complete solution

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

Jesús Gabriel y Galán

2/11/2009 11:12:00 AM

0

On Wed, Feb 11, 2009 at 11:47 AM, Christoph Blank <cb@unused.at> wrote:
> Wow, this is really useful! Especially in a general way like that.
>
> Thanks a lot for the quick and helpful answer!

BTW, I have found the other post that was similar. I don't know how to
find the link to the archive but
if you search for this subject: "Describing degerate dna strings" you
might be able to find it.
People answered with custom solutions for that exact problem (which
was pretty similar), so you might get other ideas there.

Choi, Junegunn

2/11/2009 11:53:00 AM

0

Hi, this is my code just for unfolding braces in a string. It also
tries to handle nested braces like Dir.glob. Seems working fine.

def unfold(string)
def recursion(input, list)
num_groups = 0
input.scan(/\{([^{}]*?)\}/) do | group |
prefix, suffix = $`, $'
num_groups += 1
tokens = group[0].split(/[,|]/)
tokens = [''] if tokens.empty?
tokens.each do | token |
recursion(prefix + token + suffix, list)
end
break
end
list << input if num_groups == 0
end
list = []
recursion(string, list)
list.uniq
end

puts unfold("This {is|was} a {good|nice} day")

# A more complex one
puts unfold('Who is {th{is,at},she,he,it} {?,!}{}')

Giampiero Zanchi

2/11/2009 2:42:00 PM

0

my_text = "This {is|was} a {good|nice} day"
r = /\{(.*?)\}/
elem = my_text.split(r).map! {|x| x.split("|")}
comb = []
elem.each_index do |i|
if comb.empty?
elem[i].each {|x| comb << x}
else
comb_size = comb.size
0...comb_size.times do |j|
elem[i].each { |x| s = comb.first + x ; comb.push(s) }
comb.shift
end
end
end
p comb

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

William James

2/12/2009 1:16:00 AM

0

Christoph Blank wrote:

> Hi,
>
> I have a text like this:
>
> "This {is|was} a {good|nice} day" and want to generate possible
> combinations like:
>
> This is a good day
> This was a good day
> This is a nice day
> This was a nice day
>
> I tried using zsh functionality to output this, but it doesn't seem
> that easy.
> Does anyone know an easy way how to do this in ruby?
>
> Thank you.

def comb ary
ary.inject([[]]){|old,lst|
lst.inject([]){|new,e| new + old.map{|c| c + [ e ] }}}
end

s = "This {is|was|will be} a {good|nice} day to {surf|swim}."

f = nil
a,b = s.split( /\{(.*?)\}/ ).partition{f=!f}

a = a.join( "%s" ) + "\n"
b.map!{|s| s.split "|"}

comb(b).each{|x| printf a % x }

--- output ---
This is a good day to surf.
This was a good day to surf.
This will be a good day to surf.
This is a nice day to surf.
This was a nice day to surf.
This will be a nice day to surf.
This is a good day to swim.
This was a good day to swim.
This will be a good day to swim.
This is a nice day to swim.
This was a nice day to swim.
This will be a nice day to swim.

Giampiero Zanchi

2/12/2009 9:55:00 AM

0

William's code is amazing!
A one line solution, inspired by his ideas:
"This {is|was} a {good|nice} day".split(/\{(.*?)\}/).map! {|x|
x.split("|")}.inject([[]]){|old,lst| lst.inject([]){|new,e| new +
old.map{|c| c + [ e ] }}}.each {|x| p x.join}

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