1

My code is here

str = "Early in his first term in office, Obama signed into law economic stimulus legislation in response"
arr= str.split(" ")
set_element= arr.each_cons(2).to_a
sub_str = set_element.map {|i| i.join(' ')}

If i have a big string like very big string then this process take 6.50 sec because i want to this type of result

sub_str= ["Early in", "in his", "his first", "first term", "term in", "in office,", "office, Obama", "Obama signed", "signed into", "into law", "law economic", "economic stimulus", "stimulus legislation", "legislation in", "in response"]

Is it possible any another way with efficient way

3 Answers 3

7

Use scan instead of split and you can get your word pairs directly.

s.scan(/\S+(?:\s+\S+)?/)

EDIT: Just to assure myself that this was relatively efficient, I made a little micro-benchmark. Here's results for the answers seen to date:

ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
10 times on string of size 2284879
                 user     system      total        real
original     4.180000   0.070000   4.250000 (  4.272856)
sergio       2.090000   0.000000   2.090000 (  2.102469)
dbenhur      1.050000   0.000000   1.050000 (  1.042167)
Sign up to request clarification or add additional context in comments.

1 Comment

awesome i have no word for this
1
set_element = arr.each_cons(2).to_a

The line above creates a ton of temporary objects that you don't need. Try this, should be faster:

str = "Early in his first term in office, Obama signed into law economic stimulus legislation in response"
arr = str.split(" ")
sub_str = arr.each_with_object([]).with_index do |(el, memo), idx|
  if idx % 2 == 0
    memo << el
  else
    memo.last << ' ' << el
  end

end

sub_str # => ["Early in", "his first", "term in", "office, Obama", "signed into", "law economic", "stimulus legislation", "in response"]

Comments

0

You can try this. one step less :)

arr= str.scan(/\S+/)
s = []
arr.each_with_index { |x, i| s << (x + " " + arr[i + 1]) if arr[i+1] }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.