Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use warnings 'all';
- # Read the text:
- open my $ifh, '<', 'text.txt'
- or die "Cannot open file: $!";
- local $/;
- my $text = <$ifh>;
- # Find all the words, and count how many times they appear:
- my %words = ( );
- map { $words{$_}++ }
- grep { length > 1 && $_ =~ m/^[@a-z-']+$/i }
- map { s/[",.]//g; $_ }
- split /s/, $text;
- print "Words, sorted by frequency:n";
- my (@data_line);
- format FMT =
- @<<<<<<<<<<<<<<<<<<<<<<... @########
- @data_line
- .
- local $~ = 'FMT';
- # Sort them by frequency:
- map { @data_line = ($_, $words{$_}); write(); }
- sort { $words{$b} <=> $words{$a} }
- grep { $words{$_} > 2 }
- keys(%words);
- john@ubuntu-pc1:~/Desktop$ perl frequency.pl
- Words, sorted by frequency:
- for 32
- Jan 27
- am 26
- of 21
- your 21
- to 18
- in 17
- the 17
- Get 13
- you 13
- OTRS 11
- today 11
- PSM 10
- Card 10
- me 9
- on 9
- and 9
- Offline 9
- with 9
- Invited 9
- Black 8
- get 8
- Web 7
- Starred 7
- All 7
- View 7
- Obama 7
- perl -ne'$h{$1}++while m/b(w{3,})b/g}{printf"%-20s %5dn",$_,$h{$_}for sort{$h{$b}<=>$h{$a}}grep{$h{$_}>2}keys%h'
- perl -ne'$h{$1}++while m/b(w{3,})b/g}{printf"%-20s %5dn",$_,$h{$_}for sort{$h{$b}<=>$h{$a}}grep{$h{$_}>2}keys%h'
- perl -ne'$h{$1}++while m/b(w{3,})b/g}{printf"%-20s %5dn",$_,$h{$_}for sort{$h{$b}<=>$h{$a}or$a cmp$b}grep{$h{$_}>2}keys%h'
Add Comment
Please, Sign In to add comment