Created
May 10, 2018 19:52
-
-
Save takdavid/bea4911f862e8d1ae964ac7376b069d8 to your computer and use it in GitHub Desktop.
Word count, keeping all-caps word forms, with better ordering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
binmode(STDIN, 'utf8'); | |
binmode(STDOUT, 'utf8'); | |
sub smart_lowercase($) | |
{ | |
my $word = shift; | |
return $word if (length($word) > 1 and uc($word) eq $word); | |
return lc $word; | |
} | |
my %counter = (); | |
foreach (<STDIN>) { foreach (split /[^-\w]+/, $_) { $counter{smart_lowercase $_} += 1; } } | |
my @ordered = sort { $counter{$b} <=> $counter{$b} || $a <=> $b } keys %counter; | |
foreach (@ordered) { print "$counter{$_}\t$_\n"; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment