| 1 | ## -*- Mode: CPerl -*- | ||||||||||||||||||||||||||||
| 2 | ## File: Lingua::TT::Unigrams.pm | ||||||||||||||||||||||||||||
| 3 | ## Author: Bryan Jurish <TT/IO.pm> | ||||||||||||||||||||||||||||
| 4 | ## Descript: TT Utils: unigrams | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Lingua::TT::Unigrams; | ||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Lingua::TT::Persistent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | ##============================================================================== | ||||||||||||||||||||||||||||
| 13 | ## Globals & Constants | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our @ISA = qw(Lingua::TT::Persistent); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | ##============================================================================== | ||||||||||||||||||||||||||||
| 18 | ## Constructors etc. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | ## $ng = CLASS_OR_OBJECT->new(%opts) | ||||||||||||||||||||||||||||
| 21 | ## + %opts, %$ng: | ||||||||||||||||||||||||||||
| 22 | ## wf => { $key=>$count, ... } | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 0 | my $that = shift; | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $ng = bless({ | ||||||||||||||||||||||||||
|
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
| |||||||||||||||||||||||||||||
| 26 | wf=>{}, | ||||||||||||||||||||||||||||
| 27 | @_ | ||||||||||||||||||||||||||||
| 28 | }, ref($that)||$that); | ||||||||||||||||||||||||||||
| 29 | 0 | return $ng; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | ## undef = $ng->clear() | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | my $ng = shift; | |||||||||||||||||||||||||||
| 35 | 0 | %{$ng->{wf}} = qw(); | |||||||||||||||||||||||||||
| 36 | 0 | return $ng; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | ##============================================================================== | ||||||||||||||||||||||||||||
| 40 | ## Methods: Access and Manipulation | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ## $ng1 = $ng1->add($ng2) | ||||||||||||||||||||||||||||
| 43 | ## + adds $ng2 counts to $ng | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 0 | my ($ng1,$ng2) = @_; | |||||||||||||||||||||||||||
| 46 | 0 | my ($w,$f); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | while (($w,$f)=each(%{$ng2->{wf}})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $ng1->{$w} += $f; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | return $ng1; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ##============================================================================== | ||||||||||||||||||||||||||||
| 54 | ## Methods: I/O | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 57 | ## Methods: I/O: Native | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ## $bool = $ng->saveNativeFh($fh,%opts) | ||||||||||||||||||||||||||||
| 60 | ## + saves to filehandle | ||||||||||||||||||||||||||||
| 61 | ## + implicitly sets $fh ':utf8' flag unless $opts{raw} is set | ||||||||||||||||||||||||||||
| 62 | ## + %opts: (none yet) | ||||||||||||||||||||||||||||
| 63 | ## sort => $HOW, ##-- 'freq', 'lex', or 'none' | ||||||||||||||||||||||||||||
| 64 | ## raw => $bool, ##-- save raw strings, without encoding? | ||||||||||||||||||||||||||||
| 65 | ## noids => $bool, ##-- suppress printing of ids? | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub saveNativeFh { | ||||||||||||||||||||||||||
|
Subroutine
saveNativeFh: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | my ($ng,$fh,%opts) = @_; | |||||||||||||||||||||||||||
| 68 | 0 | my @w = qw(); | |||||||||||||||||||||||||||
| 69 | 0 | my $wf = $ng->{wf}; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | my $sort = $opts{sort} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts{'sort'} || ''
MC/DC0%0/1
$opts{'sort'} || ''
$opts{'sort'}
Truth table0%0/2
A
A= $opts{'sort'}
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($sort =~ /^f/i) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | 0 | @w = sort {$wf->{$b} <=> $wf->{$a}} keys %$wf; | |||||||||||||||||||||||||||
| 73 | } elsif ($sort =~ /^l/i) { | ||||||||||||||||||||||||||||
| 74 | 0 | @w = sort {$a cmp $b} keys %$wf; | |||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 0 | @w = keys %$wf; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | warn(ref($ng).": unknown sort mode '$sort'") if ($sort && $sort !~ /^n/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sort and not $sort =~ /^n/i
MC/DC0%0/2
$sort and not $sort =~ /^n/i
$sort
not $sort =~ /^n/i
Truth table0%0/3
A and not(B)
A= $sortB= not $sort =~ /^n/i
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | foreach (@w) { | |||||||||||||||||||||||||||
| 80 | 0 | $fh->print($wf->{$_}, "\t", $_, "\n"); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | return $ng; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | ## $bool = $ng->loadNativeFh($fh) | ||||||||||||||||||||||||||||
| 86 | ## + loads from handle | ||||||||||||||||||||||||||||
| 87 | ## + implicitly sets $fh ':utf8' flag unless $opts{raw} is set | ||||||||||||||||||||||||||||
| 88 | ## + %opts: (none yet) | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub loadNativeFh { | ||||||||||||||||||||||||||
|
Subroutine
loadNativeFh: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($ng,$fh,%opts) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | $ng = $ng->new() if (!ref($ng)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | my $wf = $ng->{wf}; | |||||||||||||||||||||||||||
| 93 | 0 | my ($line,$f,$w); | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | while (defined($line=<$fh>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | chomp($line); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | next if ($line =~ /^\s*$/ || $line =~ /^\%\%/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$line =~ /^\s*$/ or $line =~ /^\%\%/
MC/DC0%0/2
$line =~ /^\s*$/ or $line =~ /^\%\%/
$line =~ /^\s*$/
$line =~ /^\%\%/
Truth table0%0/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^\%\%/
| |||||||||||||||||||||||||||||
| 97 | 0 | ($f,$w) = split(/\t/,$line,2); | |||||||||||||||||||||||||||
| 98 | 0 | $wf->{$w} += $f; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | return $ng; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | ##============================================================================== | ||||||||||||||||||||||||||||
| 104 | ## Footer | ||||||||||||||||||||||||||||
| 105 | 1 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||