Method to get string combinations

C

Christoph Blank

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

Jesús Gabriel y Galán

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/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.
 
C

Christoph Blank

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 said:
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.
 
G

Giampiero Zanchi

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
 
J

Jesús Gabriel y Galán

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

Choi, Junegunn

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

Giampiero Zanchi

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.each {|x| comb << x}
else
comb_size = comb.size
0...comb_size.times do |j|
elem.each { |x| s = comb.first + x ; comb.push(s) }
comb.shift
end
end
end
p comb
 
W

William James

Christoph said:
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.
 
G

Giampiero Zanchi

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}
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top