amit hetawal wrote:

>  
> Hello All,
>   I needed some logic behind the code for text analysis. its like i have
> to calculate the frequency of the each of the tri-grams present in a
> given piece of text
> i.e i need a way to get the number of occurence of 't' follwoed by 'he'
> for the word 'the' in whole of the text.
> 
> for text : ' hello how are you '
> i need to get occurences of 'h' follwed by 'el' then 'e' follwed by 'll'...
> and so on for each of the words present if a space is present then it is
> considered as a type of character only...
> Can you please help me with this...
> I was able to do it for bigrams where i had only 2 characters and used 2
> d arrays...but for 3 charcters i am still lost...
> Can anybody help me with this...

I would just make a hash and walk the string char by char using substr.
You could make it more generic if 3 isn't the only ossible length - I
just checked each char to make sure it wasn't a space rather than adding
a second length loop.

use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;

$_ = ' hello how are you hello ';

my %hash;
my $len = length $_;
for (my $ii = 0; $ii < $len; ++$ii) {
        my $char = substr $_, $ii, 1;
        next if $char eq ' ';
        my $char2 = substr $_, $ii+1, 1;
        next if $char2 eq ' ';
        my $char3 = substr $_, $ii+2, 1;
        next if $char3 eq ' ';
        ++$hash{$char.$char2.$char3};
}
print Data::Dumper->Dump([\%hash], [qw(\%hash)]);

__END__

Result:

$\%hash = {
  'are' => 1,
  'ell' => 2,
  'hel' => 2,
  'how' => 1,
  'llo' => 2,
  'you' => 1
};

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to