Quick note -- it's not actually working so well yet, even disregarding abbreviations. Consider the following input:

This is a sentence. This is another sentence. This is a third sentence, which also happens to be spanning a line. This is a sentence as well... I think. This is an abbreviation, cf. the list posted on Perlmonks. This is a sentence; this is the last sentence.

This produces:

This is a sentence. This is another sentence. This is a thirdsentence, which also happens to be spanning a line. This is asentence as well. I think. This is an abbreviation, cf. Perlmonks. This is a sentence;

As opposed to:

This is a sentence. This is another sentence. This is a third sentence, which also happens to be spanning a line. This is a sentence as well... I think. This is an abbreviation, cf. the list posted on Perlmonks. This is a sentence; this is the last sentence.

Note how the third and fourth one are missing a space and how lowercase characters following semicolons or periods aren't handled correctly.

Try the following -- add a space in your loop, and use a lookahead assertion to take a peek at what's following a period or colon:

#!/usr/bin/perl use feature qw/say/; use strict; use warnings; my $s; my @arr; while(<>) { chomp $_; $s .= $_ . " "; } @arr = $s =~ m/[A-Z].+?[.;](?=[^.;][A-Z]|\s*$)/g; foreach (@arr) { say; }

This produces:

This is a sentence. This is another sentence. This is a third sentence, which also happens to be spanning a line. This is a sentence as well... I think. This is an abbreviation, cf. the list posted on Perlmonks.

As you can see, it's not perfect -- it still splits if you have e.g. a run-on sentence followed by an "I", or in fact any uppercase word, e.g. a proper noun --, but it mostly handles abbreviations (arbitrary ones, even). What I'd do to fix the remaining edge cases is add another processing step after the regex where you loop over @arr, check if each element ends with a known abbreviation, and join that element with the next one if so.


In reply to Re: Splitting a Sentence by AppleFritter
in thread Splitting a Sentence by ardibehest

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.