| 1 | # Copyright 2009 Francesco Nidito. All rights reserved. | ||||||||||
| 2 | # | ||||||||||
| 3 | # This library is free software; you can redistribute it and/or | ||||||||||
| 4 | # modify it under the same terms as Perl itself. | ||||||||||
| 5 | |||||||||||
| 6 | package Algorithm::WordLevelStatistics; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | $VERSION = '0.03'; | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | 1 | my $class = shift; | |||||||||
| 15 | 1 | return bless { | |||||||||
| 16 | version => $VERSION, | ||||||||||
| 17 | }, $class; | ||||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | # computes the statistical level of a single word (given its spectrum and the total number of words in the text) | ||||||||||
| 21 | 2907 | ▶ | sub compute_spectrum { | ||||||||
|
Subroutine
compute_spectrum: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 22 | 2907 | my ($self, $N, $s) = @_; | |||||||||
| 23 | |||||||||||
| 24 | 2907 | my $n = @{$s}; | |||||||||
| 25 | |||||||||||
| 26 | 2907 | my $ls = { count => $n, C => 0, sigma_nor => 0 }; | |||||||||
| 27 | 2907 | ▶ | if( $n > 3 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | # position -> distance from preceding element in text | ||||||||||
| 29 | 973 | my @tmp = (); | |||||||||
| 30 | 973 | ▶ | for( my $i = 0; $i < ($n-1); ++$i ){ push @tmp, ($s->[$i+1] - $s->[$i]); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 973 | my ($avg, $sigma) = $self->_mean_and_variance( \@tmp ); | |||||||||
| 33 | 973 | $sigma = sqrt($sigma)/$avg; | |||||||||
| 34 | |||||||||||
| 35 | # normalize sigma using an hypothetical uniform distribution | ||||||||||
| 36 | 973 | my $p = $n/$N; | |||||||||
| 37 | 973 | $ls->{sigma_nor} = $sigma/sqrt(1.0-$p); | |||||||||
| 38 | |||||||||||
| 39 | # this is not simple: | ||||||||||
| 40 | 973 | $ls->{C} = ($ls->{sigma_nor} - (2.0*$n-1.0)/(2.0*$n+2.0)) * ( sqrt($n)*(1.0+2.8*$n**-0.865) ); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 2907 | return $ls; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # computes the statistical level of a group of words (given their spectra) | ||||||||||
| 47 | 1 | ▶ | sub compute_spectra { | ||||||||
|
Subroutine
compute_spectra: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 48 | 1 | my ($self, $s) = @_; | |||||||||
| 49 | |||||||||||
| 50 | # count the total number of "words" in text | ||||||||||
| 51 | 1 | my $N = 0; | |||||||||
| 52 | 1 | foreach my $i (keys(%{$s})){ $N += @{ $s->{$i} }; } | |||||||||
| 53 | |||||||||||
| 54 | # computes the level statistic for all terms | ||||||||||
| 55 | 1 | my %r = (); | |||||||||
| 56 | 1 | foreach my $i (keys(%{$s})){ | |||||||||
| 57 | 2907 | $r{$i} = $self->compute_spectrum( $N, $s->{$i} ); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | return \%r; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # fast, on-line algorithm to compute mean and variance: | ||||||||||
| 64 | # http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm | ||||||||||
| 65 | 973 | ▶ | sub _mean_and_variance { | ||||||||
|
Subroutine
_mean_and_variance: called CC=2 SCAR=6.9
| |||||||||||
| 66 | 973 | my ($self, $v) = @_; | |||||||||
| 67 | 973 | my ($n, $mean, $M2) = (0, 0, 0); | |||||||||
| 68 | |||||||||||
| 69 | 973 | foreach my $x (@{$v}) { | |||||||||
| 70 | 28079 | $n++; | |||||||||
| 71 | 28079 | my $delta = $x - $mean; | |||||||||
| 72 | 28079 | $mean += $delta/$n; | |||||||||
| 73 | 28079 | $M2 += $delta*($x - $mean); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 973 | my $variance = $M2/$n; | |||||||||
| 77 | |||||||||||
| 78 | 973 | return ($mean, $variance); | |||||||||
| 79 | } | ||||||||||
| 80 | 1 | 1; | |||||||||
| 81 | |||||||||||
| 82 | __END__ | ||||||||||