| 1 | ## -*- Mode: CPerl -*- | ||||||||||||||||||||
| 2 | ## File: Lingua::TT::Sentence.pm | ||||||||||||||||||||
| 3 | ## Author: Bryan Jurish <TT/IO.pm> | ||||||||||||||||||||
| 4 | ## Descript: TT I/O: sentences | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | package Lingua::TT::Sentence; | ||||||||||||||||||||
| 8 | 1 | ▶ | use Lingua::TT::Token; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | ##============================================================================== | ||||||||||||||||||||
| 12 | ## Globals & Constants | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | ##============================================================================== | ||||||||||||||||||||
| 15 | ## Constructors etc. | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | ## $sent = CLASS_OR_OBJECT->new(@tokens) | ||||||||||||||||||||
| 18 | ## + $sent: ARRAY-ref | ||||||||||||||||||||
| 19 | ## [$tok1, $tok2, ..., $tokN] | ||||||||||||||||||||
| 20 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 21 | 0 | my $that = shift; | |||||||||||||||||||
| 22 | 0 | ▶ | return bless([@_], ref($that)||$that); | ||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | ## $sent = CLASS_OR_OBJECT->newFromString($str) | ||||||||||||||||||||
| 26 | 0 | ▶ | sub newFromString { | ||||||||||||||||||
|
Subroutine
newFromString: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 27 | 0 | return $_[0]->new()->fromString($_[1]); | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | ## $sent2 = $sent->copy($depth) | ||||||||||||||||||||
| 31 | ## + creates a copy of $sent | ||||||||||||||||||||
| 32 | ## + if $deep is 0, only a shallow copy is created (tokens are shared) | ||||||||||||||||||||
| 33 | ## + if $deep is >=1 (or <0), sentences are copied as well (tokens are copied) | ||||||||||||||||||||
| 34 | 0 | ▶ | sub copy { | ||||||||||||||||||
|
Subroutine
copy: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | 0 | my ($sent,$deep) = @_; | |||||||||||||||||||
| 36 | 0 | my $sent2 = bless([],ref($sent)); | |||||||||||||||||||
| 37 | 0 | ▶ | @$sent2 = $deep ? (map {bless([@$_],ref($_))} @$sent) : @$sent; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 0 | return $sent2; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | ##============================================================================== | ||||||||||||||||||||
| 42 | ## Methods: Access | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | ## $bool = $sent->isEmpty() | ||||||||||||||||||||
| 45 | ## + true iff $sent has no non-empty tokens | ||||||||||||||||||||
| 46 | 0 | ▶ | sub isEmpty { | ||||||||||||||||||
|
Subroutine
isEmpty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 47 | 0 | return !grep {!$_->isEmpty} @{$_[0]}; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | ## $sent = $sent->rmEmptyTokens() | ||||||||||||||||||||
| 51 | ## + removes empty & undefined tokens from @$sent | ||||||||||||||||||||
| 52 | 0 | ▶ | sub rmEmptyTokens { | ||||||||||||||||||
|
Subroutine
rmEmptyTokens: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 53 | 0 | ▶ | @{$_[0]} = grep {defined($_) && !$_->isEmpty} @{$_[0]}; | ||||||||||||||||||
|
Condition0%0/3
defined $_ && !$_->isEmpty
MC/DC0%0/2
defined $_ && !$_->isEmpty
defined $_
!$_->isEmpty
Truth table0%0/3
A && not(B)
A= defined $_B= !$_->isEmpty
| |||||||||||||||||||||
| 54 | 0 | return $_[0]; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | ## $sent = $sent->rmComments() | ||||||||||||||||||||
| 58 | ## + removes comment pseudo-tokens from @$sent | ||||||||||||||||||||
| 59 | 0 | ▶ | sub rmComments { | ||||||||||||||||||
|
Subroutine
rmComments: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 60 | 0 | ▶ | @{$_[0]} = grep {!defined($_) || !$_->isComment} @{$_[0]}; | ||||||||||||||||||
|
Condition0%0/3
defined $_ && $_->isComment
MC/DC0%0/2
defined $_ && $_->isComment
defined $_
$_->isComment
Truth table0%0/3
A && B
A= defined $_B= $_->isComment
| |||||||||||||||||||||
| 61 | 0 | return $_[0]; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | ## $sent = $sent->rmNonVanilla() | ||||||||||||||||||||
| 65 | ## + removes non-vanilla tokens from @$sent | ||||||||||||||||||||
| 66 | 0 | ▶ | sub rmNonVanilla { | ||||||||||||||||||
|
Subroutine
rmNonVanilla: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 67 | 0 | ▶ | @{$_[0]} = grep {defined($_) && $_->isVanilla} @{$_[0]}; | ||||||||||||||||||
|
Condition0%0/3
defined $_ && $_->isVanilla
MC/DC0%0/2
defined $_ && $_->isVanilla
defined $_
$_->isVanilla
Truth table0%0/3
A && B
A= defined $_B= $_->isVanilla
| |||||||||||||||||||||
| 68 | 0 | return $_[0]; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | ##============================================================================== | ||||||||||||||||||||
| 73 | ## Methods: I/O | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | ## $str = $sent->toString() | ||||||||||||||||||||
| 76 | ## + returns string representing $sent, but without terminating newline | ||||||||||||||||||||
| 77 | 0 | ▶ | sub toString { | ||||||||||||||||||
|
Subroutine
toString: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 78 | 0 | return join("\n", map {$_->toString} @{$_[0]})."\n"; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | ## $sent = $sent->fromString($str) | ||||||||||||||||||||
| 82 | ## + parses $sent from string $str | ||||||||||||||||||||
| 83 | 0 | ▶ | sub fromString { | ||||||||||||||||||
|
Subroutine
fromString: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 84 | #my ($sent,$str) = @_; | ||||||||||||||||||||
| 85 | 0 | @{$_[0]} = map {Lingua::TT::Token->newFromString($_)} split(/[\r\n]+/,$_[1]); | |||||||||||||||||||
| 86 | 0 | return $_[0]; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | ##============================================================================== | ||||||||||||||||||||
| 90 | ## Methods: Raw Text (heuristic) | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | ## $str = $sent->rawString() | ||||||||||||||||||||
| 93 | ## + get raw text for sentence | ||||||||||||||||||||
| 94 | ## + returns TEXT from first comment-line '%% $stxt=TEXT' or '%% Sentence ID\t=TEXT' if available | ||||||||||||||||||||
| 95 | ## + otherwise, heuristically generates raw-text string from sentence tokens using $sent->guessRawString() | ||||||||||||||||||||
| 96 | 0 | ▶ | sub rawString { | ||||||||||||||||||
|
Subroutine
rawString: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 97 | 0 | foreach (@{$_[0]}) { | |||||||||||||||||||
| 98 | 0 | ▶ | return $1 if ($_->[0] =~ /^%%(?:\s*\$stxt=| Sentence\b[^\t]*\t=)(.*)/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | 0 | return $_[0]->guessRawString(); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | ## $str = $sent->guessdRawString() | ||||||||||||||||||||
| 104 | ## + guess raw text for sentence | ||||||||||||||||||||
| 105 | 0 | ▶ | sub guessRawString { | ||||||||||||||||||
|
Subroutine
guessRawString: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 106 | 0 | my $sent = shift; ##-- ( \@tok1, \@tok2, ..., \@tokN ) | |||||||||||||||||||
| 107 | 0 | my @spaces = qw(); ##-- ( $space_before_tok1, ..., $space_before_tokN ) | |||||||||||||||||||
| 108 | 0 | my @toks = grep {$_->[0] !~ /^\%\%/} @$sent; ##-- remove comments | |||||||||||||||||||
| 109 | 0 | my @words = map {$_->[0]} @toks; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | foreach (@words) { | |||||||||||||||||||
| 112 | 0 | ▶ | $_ =~ s/_/ /g if ($_ =~ /^[0-9]{1,3}(?:_[0-9]{3})+(?:,[0-9]+)?$/); ##-- map underscore to space in separated numerals | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | ##-- insert boundary space | ||||||||||||||||||||
| 116 | 0 | @spaces = map {''} @words; | |||||||||||||||||||
| 117 | 0 | my ($i,$w1,$w2); | |||||||||||||||||||
| 118 | 0 | foreach $i (1..$#words) { | |||||||||||||||||||
| 119 | 0 | ($w1,$w2) = @words[($i-1),$i]; | |||||||||||||||||||
| 120 | 0 | ▶ | next if ($w2 =~ /^(?:[\]\)\%\.\,\:\;\!\?]|\'+|\'[[:alpha:]]+)$/); ##-- no token-boundary space BEFORE these text types | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | ▶ | next if ($w1 =~ /^(?:[\[\(]|\`+)$/); ##-- no token-boundary space AFTER these text types | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 0 | $spaces[$i] = ' '; ##-- default: add token-boundary space | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | ##-- dump raw text | ||||||||||||||||||||
| 126 | 0 | return join('', map {($spaces[$_],$words[$_])} (0..$#words)); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | ##============================================================================== | ||||||||||||||||||||
| 130 | ## Footer | ||||||||||||||||||||
| 131 | 1 | 1; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||