Split a sentence by punctuations using Python

Discussion in 'Python' started by chad, Mar 5, 2004.

  1. chad

    chad Guest

    I want to split sentences by using punctuations, numeric numbers as
    the delimiters.

    For example, suppose I have a text that contains sentences like so:

    "To help you get there a bit faster, I will be driving at 120 miles an
    hour (I am just kidding). Is that OK?"

    Now, I want to get the following segments from this text:

    To help you get there a bit faster
    I will be driving at
    miles an hour
    I am just kidding
    Is that OK

    You know, string.split does not work for this objective. So how can i
    achieve this? Thanks.
     
    chad, Mar 5, 2004
    #1
    1. Advertising

  2. chad> You know, string.split does not work for this objective. So how
    chad> can i achieve this?

    Check the re module docs.

    Skip
     
    Skip Montanaro, Mar 5, 2004
    #2
    1. Advertising

  3. chad

    Peter Otten Guest

    chad wrote:

    > I want to split sentences by using punctuations, numeric numbers as
    > the delimiters.
    >
    > For example, suppose I have a text that contains sentences like so:
    >
    > "To help you get there a bit faster, I will be driving at 120 miles an
    > hour (I am just kidding). Is that OK?"
    >
    > Now, I want to get the following segments from this text:
    >
    > To help you get there a bit faster
    > I will be driving at
    > miles an hour
    > I am just kidding
    > Is that OK
    >
    > You know, string.split does not work for this objective. So how can i
    > achieve this? Thanks.


    >>> s

    'To help you get there a bit faster, I will be driving at 120 miles an hour
    (I am just kidding). Is that OK?'
    >>> r = re.compile("[,.?()\\d]+ *")
    >>> print "\n".join(r.split(s))

    To help you get there a bit faster
    I will be driving at
    miles an hour
    I am just kidding
    Is that OK

    >>>


    Is this OK? Note the trailing empty line. If this is not desired, just
    remove the last list item if empty.

    Peter
     
    Peter Otten, Mar 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. www
    Replies:
    2
    Views:
    729
    Andreas Leitgeb
    Jan 30, 2007
  2. basi
    Replies:
    0
    Views:
    100
  3. Pranjal Jain
    Replies:
    1
    Views:
    300
    Peter Szinek
    Apr 26, 2008
  4. Brian  Wilkins

    Perl Split on a Long Sentence Question

    Brian Wilkins, Oct 9, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    99
    -berlin.de
    Oct 9, 2006
  5. angel
    Replies:
    6
    Views:
    224
    Tad McClellan
    Nov 13, 2006
Loading...

Share This Page