|
Result of recent 'word frequencies' work |
TeamTerradactyl
Member #7,733
September 2006
|
I have finished my "word frequency" program with help of Peter Hull. It uses std::maps, std::vectors, etc. and I was asked to share this with the Allegro.cc group since it may benefit others. I only needed letters and numbers from [a-zA-Z0-9] and hyphens, so this doesn't do extended languages at all. I guess I could have used isalnum() instead of hard-coding the ASCII values, but if someone wishes to modify this and repost here for others to use, that'd be fine too. I didn't put any sort of copyright in this because I felt it was common-sense code. If anyone thinks it needs to be put under the GPL so Microsoft doesn't claim it for their own, go right ahead .
|
kazzmir
Member #1,786
December 2001
|
Nice work. And now to make you feel bad heres mostly the same program in ruby
On the following string: produces Now someone post a perl version. |
Thomas Fjellstrom
Member #476
June 2000
|
edit: fixes use IO::File; our %hash; for(split / /, join('', IO::File->new($ARGV[0])->getlines())) { chomp; $hash{$_} = 0 if !exists $hash{$_}; $hash{$_}++; } print "$_ : $hash{$_}\n" for( reverse sort { $hash{$a} cmp $hash{$b} } keys %hash ); or a little uglier: our %hash; for(split(/ /, `cat $ARGV[0]`)) { chomp; $hash{$_} = 0 if !exists $hash{$_}; $hash{$_}++; } print "$_ : $hash{$_}\n" for( reverse sort { $hash{$a} cmp $hash{$b} } keys %hash ); edit, I forgot to post the output from both: hello : 3 world : 3 and : 2 this : 1 thing : 1 stuff : 1 foo : 1 bar : 1 whatever : 1 is : 1 a : 1
-- |
Peter Wang
Member #23
April 2000
|
main = interact (unlines . map sho . sortBy cmp . map freq . group . sort . words) freq ws = (head ws, length ws) cmp (_,c1) (_,c2) = compare c2 c1 sho (w,c) = w ++ " " ++ show c
|
TeamTerradactyl
Member #7,733
September 2006
|
You suck. You all, each one of you, suck. You and your stupid "other languages." At least it's not Java
|
Thomas Fjellstrom
Member #476
June 2000
|
C++ + Qt4:
output: world : 3 hello : 3 and : 2 this : 1 whatever : 1 thing : 1 is : 1 stuff : 1 bar : 1 a : 1 foo : 1
-- |
|