# No.26 Perl Sample (code key - 45) # Learning Perl: Count sentences (センテンス数を数える) # Tomonori Nagano # Last Update: January 18, 2008 # # This file is encoded in Unicode (UTF-8). If you see gibberish characters, # please re-encode the file in utf-8. # # this sample script is simplified for pedagogical purposes. Those who are # interested in advanced Perl programming are encouraged to consult with # relevant sections of "Perl Cookbook" by Christiansen & Torkington my $numSentences = 0 ; my $text = "" ; while(<>){ # this reads the default file line by line chomp ; # delete the end-of-line character $text .= $_ ; # combine all the text into one line } # use REGEX (regular expressions). Assuming that the period followed by an # upper case letter is the end-of-sentence symbol $text =~ s/\.\s?([A-Z])/\ \1/g ; # translate a period into a end-of-sentence tag
print $text ; @sentences = split("\",$text) ; foreach (@sentences) { $numSentences++ ; } print "numSentences\t$numSentences\n" ;