On 10/17/06, Gavin Kistner <gavin.kistner@anark.com> wrote:
From: Ajithkumar Warrier [mailto:a.varier@gmail.com]
> I am a newbie and the answer to this might be too simple.
> How do I improve the example below and reduce the number of passes
> over the string?
>
> string = "This is an example string. The purpose is to save the
> delimiter during split. Does this work. Great!!!."
>
> re = /(\.\s+)(\D)/
> string.gsub!(re,'\1'+'#'+'\2')
> b = string.split('#')
> puts b
p string.scan( /\w[^.!?]+\S+/ )
#=> ["This is an example string.", "The purpose is to save the delimiter
during split.", "Does this work.", "Great!!!."]
I am a newbie and the answer to this might be too simple.
How do I improve the example below and reduce the number of passes
over the string?
string = "This is an example string. The purpose is to save the
delimiter during split. Does this work. Great!!!."
re = /(\.\s+)(\D)/
string.gsub!(re,'\1'+'#'+'\2')
b = string.split('#')
puts b
p string.scan( /\w[^.!?]+\S+/ )
#=> ["This is an example string.", "The purpose is to save the delimiter
during split.", "Does this work.", "Great!!!."]
>> string = "This is an example string. The purpose is to save the
delimiter during split. Does this work. Great!!!."
>> string = string + " It costs 0.1 dollars."
=> "This is an example string. The purpose is to save the\ndelimiter during split. Does this work. Great!!!. It costs 0.1 dollars."
>> string.scan( /\w[^.!?]+\S+/ )
=> ["This is an example string.", "The purpose is to save the\ndelimiter during split.", "Does this work.", "Great!!!.", "It costs 0.1", "do
llars."]
Is the current (1.8.5) regex engine some (other) well-known engine? For
example it is very like PCRE, but I take it that it is not PCRE. Just
curious. m.
For completeness, if you wanted to use this form and also wanted to
allow exclamation points and question marks as sentence delimiters in
addition to periods, you could use:
> p string.scan( /\w[^.!?]+\S+/ )
>
> #=> ["This is an example string.", "The purpose is to save the delimiter
> during split.", "Does this work.", "Great!!!."]
>> string = "This is an example string. The purpose is to save the
delimiter during split. Does this work. Great!!!."
>> string = string + " It costs 0.1 dollars."
=> "This is an example string. The purpose is to save the\ndelimiter
during split. Does this work. Great!!!. It costs 0.1 dollars."
>> string.scan( /\w[^.!?]+\S+/ )
=> ["This is an example string.", "The purpose is to save the\ndelimiter
during split.", "Does this work.", "Great!!!.", "It costs 0.1", "do
llars."]
Down this path leads the madness that is trying to use simple regexp to
parse something as complex as English grammar. That said, here's
another regexp that still works and fixes that particular case:
string = "This is an example string. The purpose is to save the
delimiter during split. Does this work. Great!!!."
string = string + " It costs 0.1 dollars."
p string.scan( /\w.+?[.!?]+(?=\s|\Z)/ )
#=> ["This is an example string.", "The purpose is to save the
delimiter during split.", "Does this work.", "Great!!!.", "It costs 0.1
dollars."]
It'll still fail on sentences with embedded quotes that have
sub-sentences within them.