| 1 | 4 | ▶ | use strict; use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | package Lingua::EN::Sentence::Offsets; | ||||||||||||||||||||||||||||
| 3 | { | ||||||||||||||||||||||||||||
| 4 | 4 | $Lingua::EN::Sentence::Offsets::VERSION = '0.01_05'; | |||||||||||||||||||||||||||
| 5 | } | ||||||||||||||||||||||||||||
| 6 | 4 | require Exporter; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | #ABSTRACT: Finds sentence boundaries, and returns their offsets. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | my ($EOS,$AP,$P,$PAP,@ABBREVIATIONS); | |||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Carp qw/cluck/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use feature qw/say/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use utf8::all; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Data::Dump qw/dump/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | our @EXPORT_OK = qw/ | |||||||||||||||||||||||||||
| 18 | get_sentences | ||||||||||||||||||||||||||||
| 19 | get_offsets | ||||||||||||||||||||||||||||
| 20 | add_acronyms | ||||||||||||||||||||||||||||
| 21 | get_acronyms | ||||||||||||||||||||||||||||
| 22 | set_acronyms | ||||||||||||||||||||||||||||
| 23 | initial_offsets | ||||||||||||||||||||||||||||
| 24 | offsets2sentences | ||||||||||||||||||||||||||||
| 25 | remove_false_eos | ||||||||||||||||||||||||||||
| 26 | adjust_offsets | ||||||||||||||||||||||||||||
| 27 | split_unsplit_stuff | ||||||||||||||||||||||||||||
| 28 | /; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | $EOS="\001";$P = q/[\.!?]/;$AP = q/(?:'|"|»|\)|\]|\})?/;$PAP = $P.$AP; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | my @PEOPLE = ( 'jr', 'mr', 'mrs', 'ms', 'dr', 'prof', 'sr', "sens?", "reps?", 'gov', | |||||||||||||||||||||||||||
| 34 | "attys?", 'supt', 'det', 'rev' ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | my @ARMY = ( 'col','gen', 'lt', 'cmdr', 'adm', 'capt', 'sgt', 'cpl', 'maj' ); | |||||||||||||||||||||||||||
| 38 | 4 | my @INSTITUTES = ( 'dept', 'univ', 'assn', 'bros' ); | |||||||||||||||||||||||||||
| 39 | 4 | my @COMPANIES = ( 'inc', 'ltd', 'co', 'corp' ); | |||||||||||||||||||||||||||
| 40 | 4 | my @PLACES = ( 'arc', 'al', 'ave', "blv?d", 'cl', 'ct', 'cres', 'dr', "expy?", | |||||||||||||||||||||||||||
| 41 | 'dist', 'mt', 'ft', | ||||||||||||||||||||||||||||
| 42 | "fw?y", "hwa?y", 'la', "pde?", 'pl', 'plz', 'rd', 'st', 'tce', | ||||||||||||||||||||||||||||
| 43 | 'Ala' , 'Ariz', 'Ark', 'Cal', 'Calif', 'Col', 'Colo', 'Conn', | ||||||||||||||||||||||||||||
| 44 | 'Del', 'Fed' , 'Fla', 'Ga', 'Ida', 'Id', 'Ill', 'Ind', 'Ia', | ||||||||||||||||||||||||||||
| 45 | 'Kan', 'Kans', 'Ken', 'Ky' , 'La', 'Me', 'Md', 'Is', 'Mass', | ||||||||||||||||||||||||||||
| 46 | 'Mich', 'Minn', 'Miss', 'Mo', 'Mont', 'Neb', 'Nebr' , 'Nev', | ||||||||||||||||||||||||||||
| 47 | 'Mex', 'Okla', 'Ok', 'Ore', 'Penna', 'Penn', 'Pa' , 'Dak', | ||||||||||||||||||||||||||||
| 48 | 'Tenn', 'Tex', 'Ut', 'Vt', 'Va', 'Wash', 'Wis', 'Wisc', 'Wy', | ||||||||||||||||||||||||||||
| 49 | 'Wyo', 'USAFA', 'Alta' , 'Man', 'Ont', 'Qué', 'Sask', 'Yuk'); | ||||||||||||||||||||||||||||
| 50 | 4 | my @MONTHS = ('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec','sept'); | |||||||||||||||||||||||||||
| 51 | 4 | my @MISC = ( 'vs', 'etc', 'no', 'esp' ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | @ABBREVIATIONS = (@PEOPLE, @ARMY, @INSTITUTES, @COMPANIES, @PLACES, @MONTHS, @MISC ); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | sub get_offsets { | ||||||||||||||||||||||||||
|
Subroutine
get_offsets: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 4 | my ($text) = @_; | |||||||||||||||||||||||||||
| 58 | 4 | ▶ | return [] unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 4 | my $offsets = initial_offsets($text); | |||||||||||||||||||||||||||
| 60 | 4 | $offsets = remove_false_eos($text,$offsets); | |||||||||||||||||||||||||||
| 61 | 4 | $offsets = split_unsplit_stuff($text,$offsets); | |||||||||||||||||||||||||||
| 62 | 4 | $offsets = adjust_offsets($text,$offsets); | |||||||||||||||||||||||||||
| 63 | 4 | return $offsets; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | sub get_sentences { | ||||||||||||||||||||||||||
|
Subroutine
get_sentences: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 4 | my ($text) = @_; | |||||||||||||||||||||||||||
| 70 | 4 | my $offsets = get_offsets($text); | |||||||||||||||||||||||||||
| 71 | 4 | my $sentences = offsets2sentences($text,$offsets); | |||||||||||||||||||||||||||
| 72 | 4 | return $sentences; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub add_acronyms { | ||||||||||||||||||||||||||
|
Subroutine
add_acronyms: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | push @ABBREVIATIONS, @_; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub get_acronyms { | ||||||||||||||||||||||||||
|
Subroutine
get_acronyms: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | return @ABBREVIATIONS; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub set_acronyms { | ||||||||||||||||||||||||||
|
Subroutine
set_acronyms: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 0 | @ABBREVIATIONS=@_; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub remove_false_eos { | ||||||||||||||||||||||||||
|
Subroutine
remove_false_eos: called CC=16 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 4 | my ($text,$offsets) = @_; | |||||||||||||||||||||||||||
| 94 | 4 | my $size = @$offsets; | |||||||||||||||||||||||||||
| 95 | 4 | my $new_offsets = [ sort { $a->[0] <=> $b->[0] } @$offsets ]; | |||||||||||||||||||||||||||
| 96 | 4 | ▶ | for(my $i=0; $i<$size-1; $i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 92 | my $start = $new_offsets->[$i][0]; | |||||||||||||||||||||||||||
| 98 | 92 | my $end = $new_offsets->[$i][1]; | |||||||||||||||||||||||||||
| 99 | 92 | my $length = $end-$start; | |||||||||||||||||||||||||||
| 100 | 92 | my $s = substr($text,$start,$length); | |||||||||||||||||||||||||||
| 101 | 92 | my $j=$i+1; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 92 | my $unsplit = 0; | |||||||||||||||||||||||||||
| 104 | 92 | ▶ | $unsplit = 1 if $s =~ /(?:[^-\w]|^)\w$PAP\s$/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 92 | ▶ | $unsplit = 1 if $s =~ /[^-\w]\w$P$/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # don't split after a white-space followed by a single letter followed | ||||||||||||||||||||||||||||
| 108 | # by a dot followed by another whitespace. | ||||||||||||||||||||||||||||
| 109 | 92 | ▶ | $unsplit = 1 if $s =~ /\s\w\.\s+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # fix: bla bla... yada yada | ||||||||||||||||||||||||||||
| 112 | 92 | my $t = substr($text,$offsets->[$j][0], $offsets->[$j][1]-$offsets->[$j][0]); | |||||||||||||||||||||||||||
| 113 | 92 | ▶ | $unsplit = 1 if $s =~ /\.\.\.\s*$/s and $t =~ /^\s*[[:lower:]]/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$s =~ /\.\.\.\s*$/su and $t =~ /^\s*[[:lower:]]/su
MC/DC0%0/2
$s =~ /\.\.\.\s*$/su and $t =~ /^\s*[[:lower:]]/su
$s =~ /\.\.\.\s*$/su
$t =~ /^\s*[[:lower:]]/su
Truth table33%1/3
A and B
A= $s =~ /\.\.\.\s*$/suB= $t =~ /^\s*[[:lower:]]/su
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # fix "." "?" "!" | ||||||||||||||||||||||||||||
| 116 | 92 | ▶ | $unsplit = 1 if $s =~ m{['"]$P['"]\s+$}s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | ## fix where abbreviations exist | ||||||||||||||||||||||||||||
| 119 | 92 | ▶ | foreach (@ABBREVIATIONS){ $unsplit = $1 if $s =~ /\b$_$PAP\s$/is; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # don't break after quote unless its a capital letter. | ||||||||||||||||||||||||||||
| 122 | 92 | ▶ | $unsplit = 1 if $s =~ /["']\s*$/s and $t =~ /^\s*[[:lower:]]/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$s =~ /["']\s*$/su and $t =~ /^\s*[[:lower:]]/su
MC/DC0%0/2
$s =~ /["']\s*$/su and $t =~ /^\s*[[:lower:]]/su
$s =~ /["']\s*$/su
$t =~ /^\s*[[:lower:]]/su
Truth table33%1/3
A and B
A= $s =~ /["']\s*$/suB= $t =~ /^\s*[[:lower:]]/su
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # don't break: text . . some more text. | ||||||||||||||||||||||||||||
| 125 | 92 | ▶ | $unsplit = 1 if $s =~ /\s\.\s$/s and $t =~ /^\s*/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$s =~ /\s\.\s$/su and $t =~ /^\s*/su
MC/DC0%0/2
$s =~ /\s\.\s$/su and $t =~ /^\s*/su
$s =~ /\s\.\s$/su
$t =~ /^\s*/su
Truth table33%1/3
A and B
A= $s =~ /\s\.\s$/suB= $t =~ /^\s*/su
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 92 | ▶ | $unsplit = 1 if $s =~ /\s$PAP\s$/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 92 | ▶ | _merge_forward($new_offsets,$i) if $unsplit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 4 | $new_offsets = [ grep { defined } @$new_offsets ]; | |||||||||||||||||||||||||||
| 132 | 4 | return $new_offsets; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | #for(my $i=$size-1; $i>=0; $i--){ splice @$offsets, $i,1 unless defined($offsets->[$i]); } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 18 | ▶ | sub _merge_forward { | ||||||||||||||||||||||||||
|
Subroutine
_merge_forward: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 138 | 18 | my ($offsets,$i) = @_; | |||||||||||||||||||||||||||
| 139 | 18 | my $j = $i+1; | |||||||||||||||||||||||||||
| 140 | 18 | ▶ | return $offsets unless defined($offsets->[$j]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 18 | $offsets->[$j][0] = $offsets->[$i][0]; | |||||||||||||||||||||||||||
| 143 | 18 | delete $offsets->[$i]; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | #splice @$offsets, $i, 1; | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 4 | ▶ | sub split_unsplit_stuff { | ||||||||||||||||||||||||||
|
Subroutine
split_unsplit_stuff: called CC=8 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 4 | my ($text,$offsets) = @_; | |||||||||||||||||||||||||||
| 151 | 4 | my $size = @$offsets; | |||||||||||||||||||||||||||
| 152 | 4 | ▶ | for(my $i=0; $i<$size; $i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 78 | my $start = $offsets->[$i][0]; | |||||||||||||||||||||||||||
| 154 | 78 | my $length = $offsets->[$i][1]-$start; | |||||||||||||||||||||||||||
| 155 | 78 | my $s = substr($text,$start,$length); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 78 | my $split_points = []; | |||||||||||||||||||||||||||
| 158 | 78 | ▶ | while($s =~ /((?:\D|^)\d+$P)(\s+)/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 7 | my $end = $+[1]; | |||||||||||||||||||||||||||
| 160 | 7 | my $begin = $-[2]; | |||||||||||||||||||||||||||
| 161 | 7 | push @$split_points,[$start+$end,$start+$begin]; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 78 | ▶ | while($s =~ /($PAP\s)(\s*\()/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | my $end = $+[1]; | |||||||||||||||||||||||||||
| 165 | 0 | my $begin = $-[2]; | |||||||||||||||||||||||||||
| 166 | 0 | push @$split_points,[$start+$end,$start+$begin]; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 78 | ▶ | while($s =~ /('\w$P)(\s)/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | my $end = $+[1]; | |||||||||||||||||||||||||||
| 170 | 0 | my $begin = $-[2]; | |||||||||||||||||||||||||||
| 171 | 0 | push @$split_points,[$start+$end,$start+$begin]; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 78 | ▶ | while($s =~ /(\sno\.)(\s+)(?!\d)/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | my $end = $+[1]; | |||||||||||||||||||||||||||
| 175 | 0 | my $begin = $-[2]; | |||||||||||||||||||||||||||
| 176 | 0 | push @$split_points,[$start+$end,$start+$begin]; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 78 | ▶ | while($s =~ /([ap]\.m\.\s+)([[:upper:]])/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | my $end = $+[1]; | |||||||||||||||||||||||||||
| 180 | 0 | my $begin = $-[2]; | |||||||||||||||||||||||||||
| 181 | 0 | push @$split_points,[$start+$end,$start+$begin]; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 78 | ▶ | _split_sentence($offsets,$i, [ sort { $a->[0] <=> $b->[0] } @$split_points ]) if @$split_points; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 4 | return $offsets; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | #sub _split_sentence { | ||||||||||||||||||||||||||||
| 192 | # my ($offsets,$i,$end1,$start2) = @_; | ||||||||||||||||||||||||||||
| 193 | # my $end2 = $offsets->[$i][1]; | ||||||||||||||||||||||||||||
| 194 | # $offsets->[$i][1] = $end1; | ||||||||||||||||||||||||||||
| 195 | # $start2 //= $end1; | ||||||||||||||||||||||||||||
| 196 | # push $offsets, [$start2, $end2]; | ||||||||||||||||||||||||||||
| 197 | #} | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 26 | ▶ | sub _split_sentence { | ||||||||||||||||||||||||||
|
Subroutine
_split_sentence: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 201 | 26 | my ($offsets,$i,$split_points) = @_; | |||||||||||||||||||||||||||
| 202 | 26 | my ($end,$start) = @{shift @$split_points}; | |||||||||||||||||||||||||||
| 203 | 26 | my $last = $offsets->[$i][1]; | |||||||||||||||||||||||||||
| 204 | 26 | $offsets->[$i][1] = $end; | |||||||||||||||||||||||||||
| 205 | 26 | ▶ | while(my $p = shift @$split_points){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 73 | push @$offsets, [$start,$p->[0]]; | |||||||||||||||||||||||||||
| 207 | 73 | $start = $p->[1]; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 26 | push @$offsets, [$start, $last]; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 4 | ▶ | sub adjust_offsets { | ||||||||||||||||||||||||||
|
Subroutine
adjust_offsets: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 214 | 4 | my ($text,$offsets) = @_; | |||||||||||||||||||||||||||
| 215 | 4 | my $size = @$offsets; | |||||||||||||||||||||||||||
| 216 | 4 | ▶ | for(my $i=0; $i<$size; $i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 85 | my $start = $offsets->[$i][0]; | |||||||||||||||||||||||||||
| 218 | 85 | my $end = $offsets->[$i][1]; | |||||||||||||||||||||||||||
| 219 | 85 | my $length = $end - $start; | |||||||||||||||||||||||||||
| 220 | 85 | my $s = substr($text,$start,$length); | |||||||||||||||||||||||||||
| 221 | 85 | ▶ | if ($s !~ /\w+/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 1 | delete $offsets->[$i]; | |||||||||||||||||||||||||||
| 223 | 1 | next; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 84 | $s =~ /^(\s*).*?(\s*)$/s; | |||||||||||||||||||||||||||
| 226 | 84 | ▶ | if(defined($1)){ $start += length($1); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 84 | ▶ | if(defined($2)){ $end -= length($2); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 84 | $offsets->[$i] = [$start, $end]; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 4 | my $new_offsets = [ grep { defined } @$offsets ]; | |||||||||||||||||||||||||||
| 231 | 4 | return $new_offsets; | |||||||||||||||||||||||||||
| 232 | #for(my $i=$size-1; $i>=0; $i--){ splice @$offsets, $i,1 unless defined($offsets->[$i]); } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 4 | ▶ | sub initial_offsets { | ||||||||||||||||||||||||||
|
Subroutine
initial_offsets: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 237 | 4 | my ($text) = @_; | |||||||||||||||||||||||||||
| 238 | 4 | my $end; | |||||||||||||||||||||||||||
| 239 | 4 | my $text_end = length($text); | |||||||||||||||||||||||||||
| 240 | 4 | my $offsets = [[0,$text_end]]; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 4 | my @patterns = ( | |||||||||||||||||||||||||||
| 243 | qr{(\n\s*\n)}, | ||||||||||||||||||||||||||||
| 244 | qr{$PAP\s()}, | ||||||||||||||||||||||||||||
| 245 | qr{\s\w$P()} | ||||||||||||||||||||||||||||
| 246 | ); | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 4 | my $split = 1; | |||||||||||||||||||||||||||
| 249 | 4 | ▶ | while($split){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 8 | $split = 0; | |||||||||||||||||||||||||||
| 251 | 8 | foreach my $pat (@patterns){ | |||||||||||||||||||||||||||
| 252 | 24 | my $size = @$offsets; | |||||||||||||||||||||||||||
| 253 | 24 | ▶ | for(my $i=0; $i<$size; $i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 388 | my $start = $offsets->[$i][0]; | |||||||||||||||||||||||||||
| 255 | 388 | my $length = $offsets->[$i][1]-$start; | |||||||||||||||||||||||||||
| 256 | 388 | my $s = substr($text,$start,$length); | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 388 | my $split_points = []; | |||||||||||||||||||||||||||
| 259 | 388 | ▶ | while($s =~ /(?<!^)$pat(?!$)/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 92 | my $end = $-[1]; | |||||||||||||||||||||||||||
| 261 | 92 | my $begin = $+[1]; | |||||||||||||||||||||||||||
| 262 | 92 | push @$split_points,[$start+$end,$start+$begin]; | |||||||||||||||||||||||||||
| 263 | 92 | $split = 1; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 388 | ▶ | _split_sentence($offsets,$i,[ sort { $a->[0] <=> $b->[0] } @$split_points ]) if @$split_points; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 4 | return $offsets; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 4 | ▶ | sub offsets2sentences { | ||||||||||||||||||||||||||
|
Subroutine
offsets2sentences: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 275 | 4 | my ($text, $offsets) = @_; | |||||||||||||||||||||||||||
| 276 | 4 | my $sentences = []; | |||||||||||||||||||||||||||
| 277 | 4 | foreach my $o ( sort {$a->[0] <=> $b->[0]} @$offsets) { | |||||||||||||||||||||||||||
| 278 | 84 | my $start = $o->[0]; | |||||||||||||||||||||||||||
| 279 | 84 | my $length = $o->[1]-$o->[0]; | |||||||||||||||||||||||||||
| 280 | 84 | push @$sentences, substr($text,$start,$length); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | 4 | return $sentences; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 4 | 1; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =pod | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =head1 NAME | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | Lingua::EN::Sentence::Offsets - Finds sentence boundaries, and returns their offsets. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =head1 VERSION | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | version 0.01_05 | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | use Lingua::EN::Sentence::Offsets qw/get_offsets get_sentences/; | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | my $offsets = get_offsets($text); ## Get the offsets. | ||||||||||||||||||||||||||||
| 304 | foreach my $o (@$offsets) { | ||||||||||||||||||||||||||||
| 305 | my $start = $o->[0]; | ||||||||||||||||||||||||||||
| 306 | my $length = $o->[1]-$o->[0]; | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | my $sentence = substr($text,$start,$length) ## Get a sentence. | ||||||||||||||||||||||||||||
| 309 | # ... | ||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | ### or | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | my $sentences = get_sentences($text); | ||||||||||||||||||||||||||||
| 315 | foreach my $sentence (@$sentences) { | ||||||||||||||||||||||||||||
| 316 | ## do something with $sentence | ||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head1 METHODS | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =head2 get_offsets | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | Takes text input and returns reference to array containin pairs of character | ||||||||||||||||||||||||||||
| 324 | offsets, corresponding to the sentences start and end positions. | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =head2 get_sentences | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Takes text input and splits it into sentences. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =head2 add_acronyms | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | user can add a list of acronyms/abbreviations. | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head2 get_acronyms | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | get defined list of acronyms. | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =head2 set_acronyms | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | run over the predefined acronyms list with your own list. | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =head2 remove_false_eos | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head2 split_unsplit_stuff | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Finds additional split points in the middle of previously defined sentences. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head2 adjust_offsets | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | Minor adjusts to offsets (leading/trailing whitespace, etc) | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head2 initial_offsets | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | First naive delimitation of sentences | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =head2 offsets2sentences | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Given a list of sentence boundaries offsets and a text, returns an array with the text split into sentences. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Based on the original module L<Lingua::EN::Sentence>, from Shlomo Yona (SHLOMOY) | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | L<Lingua::EN::Sentence>, L<Text::Sentence> | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Andre Santos <andrefs@cpan.org> | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | This software is copyright (c) 2012 by Andre Santos. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 377 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | __END__ | ||||||||||||||||||||||||||||