Create a gist now

Instantly share code, notes, and snippets.

Get full sentence for query
> 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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment