in reply to count trigrams of a whole file
UPDATE:
A quick fix (if your file isn't too large or if you have sufficient RAM) would be to populate @words like so:
while (<>) { push @words, split /\s/; }
That way, you can move onto your for loop and you should get the result you want. This was the result I got after making this change:
trigram frequencies in your text: iwentthere! 1 wentthere!she 1 there!shealso 1 shealsowent 1 alsowentthere. 1
ORIGINAL POST:
Hi lakssreedhar, I didn't change anything, but I felt the need to reformat your code for better readability:
@trigrams = (); while (<>) { @words = split /\s/, $_; for ( $i = 0 ; $i < $#words - 1 ; $i++ ) { $trigram = $words[$i] . $words[ $i + 1 ] . $words[ $i + 2 ]; $found = -1; if (@trigrams) { SEARCHtrigramINDEX: for ( $index = 0 ; $index <= $#trigrams ; $index++ ) { if ( $trigrams[$index] eq $trigram ) { $found = $index; last SEARCHtrigramINDEX; } } } if ( $found > -1 ) { $trigramfrequency[$found]++; } else { push @trigrams, $trigram; $trigramfrequency[$#trigrams]++; } } } print "trigram frequencies in your text:\n"; for ( $index = 0 ; $index <= @trigrams ; $index++ ) { print "$trigrams[$index] $trigramfrequency[$index]\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: count trigrams of a whole file
by lakssreedhar (Acolyte) on Dec 20, 2012 at 09:09 UTC | |
Re^2: count trigrams of a whole file
by lakssreedhar (Acolyte) on Dec 21, 2012 at 05:24 UTC | |
by frozenwithjoy (Priest) on Dec 21, 2012 at 05:42 UTC | |
by lakssreedhar (Acolyte) on Dec 21, 2012 at 06:11 UTC | |
by frozenwithjoy (Priest) on Dec 21, 2012 at 20:26 UTC | |
by Anonymous Monk on Jan 05, 2013 at 06:30 UTC | |
|
In Section
Seekers of Perl Wisdom