Search code examples
perltext-segmentation

convert paragraph into sentence using Perl


I'm doing Perl programming. I need to read a paragraph and print it out each sentence as a line.

Anyone know how to do it?

Below is my code:

#! /C:/Perl64/bin/perl.exe

use utf8;

if (! open(INPUT, '< text1.txt')){
die "cannot open input file: $!";
}

if (! open(OUTPUT, '> output.txt')){
die "cannot open input file: $!";
}

select OUTPUT;

while (<INPUT>){
print "$_";
}

close INPUT;
close OUTPUT;
select STDOUT;

Solution

  • If you are given the paragraph as a string, you can split() it on characters that mark the end of a sentence.

    for example:

    my @sentences = split /[.?!]/, $paragraph;