perl_lover_always has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Encode; use Storable; use Lingua::StopWords qw( getStopWords ); my $stopwords = getStopWords('en'); my $file_in = shift; my $file_out = shift; open (FILEIN,'<utf8',$file_in); #open (OUTPUT, '>utf8',$file_out); my $line_count=0; my %hash; for my $line (<FILEIN>){ $line_count++; my @ngrams=produce_ngrams($line); foreach my $ngram (@ngrams) { push(@{ $hash{$ngram} }, $line_count); } } store \%hash, $file_out; my %stat = (); %stat = %{retrieve($file_out)}; #foreach my $n (keys %stat) { print "$n: @{$stat{$n}}\n"; } sub produce_ngrams { my $string=shift; $string =~ s/[[:punct:]]//g; $string=lc($string); my @unigrams = split ' ',$string; #@unigrams= join ' ', grep { !$stopwords->{$_} } @unigrams; my @bigrams=(); my @trigrams=(); my @qgrams=(); my @cgrams=(); my @ngrams=(); for my $i (0..$#unigrams-1) { my $bigram = join " ", $unigrams[$i], $unigrams[$i+1]; push @bigrams, $bigram; } if ($#unigrams >= 2) { for my $i (0..$#unigrams-2) { my $trigram = join " ", $unigrams[$i], $unigrams[$i+1], $unigr +ams[$i+2]; push @trigrams, $trigram; }} if ($#unigrams >= 3) { for my $i (0..$#unigrams-3) { my $qgram = join " ", $unigrams[$i], $unigrams[$i+1], $unigram +s[$i+2],$unigrams[$i+3]; push @qgrams, $qgram; }} if ($#unigrams >= 4) { for my $i (0..$#unigrams-4) { my $cgram = join " ", $unigrams[$i], $unigrams[$i+1], $unigram +s[$i+2],$unigrams[$i+3],$unigrams[$i+4]; push @cgrams, $cgram; }} push @ngrams, @unigrams,@bigrams,@trigrams,@qgrams,@cgrams; return @ngrams; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: statistics of a large text
by tilly (Archbishop) on Jan 26, 2011 at 14:48 UTC | |
by perl_lover_always (Acolyte) on Jan 26, 2011 at 15:25 UTC | |
by tilly (Archbishop) on Jan 26, 2011 at 15:44 UTC | |
by perl_lover_always (Acolyte) on Jan 26, 2011 at 17:13 UTC | |
by perl_lover_always (Acolyte) on Jan 27, 2011 at 09:59 UTC | |
by tilly (Archbishop) on Jan 27, 2011 at 15:05 UTC | |
| |
by perl_lover_always (Acolyte) on Jan 31, 2011 at 14:51 UTC | |
by tilly (Archbishop) on Feb 01, 2011 at 07:21 UTC | |
by perl_lover_always (Acolyte) on Feb 08, 2011 at 11:05 UTC | |
by tilly (Archbishop) on Feb 08, 2011 at 15:32 UTC | |
|
Re: statistics of a large text
by toolic (Bishop) on Jan 26, 2011 at 14:09 UTC | |
|
Re: statistics of a large text
by eff_i_g (Curate) on Jan 26, 2011 at 14:42 UTC | |
|
Re: statistics of a large text
by JavaFan (Canon) on Jan 26, 2011 at 16:04 UTC | |
|
Re: statistics of a large text
by BrowserUk (Patriarch) on Jan 26, 2011 at 16:32 UTC | |
by perl_lover_always (Acolyte) on Jan 26, 2011 at 17:12 UTC | |
|
Re: statistics of a large text
by planetscape (Chancellor) on Jan 26, 2011 at 23:04 UTC | |
by perl_lover_always (Acolyte) on Jan 27, 2011 at 16:22 UTC | |
|
Re: statistics of a large text
by locked_user sundialsvc4 (Abbot) on Jan 26, 2011 at 20:13 UTC |