public
Created

Get full sentence for query

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10
> text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'
> query = 'veniam'
> pos = text =~ /#{query}/
=> 142
> prev_period = text.rindex('.', pos) || -1
=> 123
> next_period = text.index('.', pos) || -1
=> 231
> sentence = text[prev_period+1..next_period]
=> " Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.