Method to get string combinations


#1

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.


#2

On Wed, Feb 11, 2009 at 11:22 AM, Christoph B. removed_email_address@domain.invalid 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/quiz143.html) 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-talk/274375

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.


#3

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 B. removed_email_address@domain.invalid 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/quiz143.html) 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-talk/274375

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.


#4

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®
elem.map! {|x| x.split("|")}
p elem

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

curious to see the complete solution


#5

On Wed, Feb 11, 2009 at 11:47 AM, Christoph B. removed_email_address@domain.invalid 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.


#6

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} {?,!}{}’)


#7

my_text = “This {is|was} a {good|nice} day”
r = /{(.*?)}/
elem = my_text.split®.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


#8

Christoph B. 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?

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.


#9

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}