Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bowsersenior/943480 to your computer and use it in GitHub Desktop.
Save bowsersenior/943480 to your computer and use it in GitHub Desktop.
Calculates word frequencies from the text of Jonathan Swift’s, A Modest Proposal from http://tomayko.com/writings/awkward-ruby
# with AWK
curl -s http://www.gutenberg.org/files/1080/1080.txt |
awk '
BEGIN { FS="[^a-zA-Z]+" }
{
for (i=1; i<=NF; i++) {
word = tolower($i)
words[word]++
}
}
END {
for (w in words)
printf("%3d %s\n", words[w], w)
}
' |
sort -rn
# OR with ruby
curl -s http://www.gutenberg.org/files/1080/1080.txt |
ruby -ne '
BEGIN { $words = Hash.new(0) }
$_.split(/[^a-zA-Z]+/).each { |word| $words[word.downcase] += 1 }
END {
$words.each { |word, i| printf "%3d %s\n", i, word }
}
' |
sort -rn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment