Skip to content

Instantly share code, notes, and snippets.

@alexggordon
Last active November 9, 2015 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexggordon/ee89c10f7837dc9ade91 to your computer and use it in GitHub Desktop.
Save alexggordon/ee89c10f7837dc9ade91 to your computer and use it in GitHub Desktop.
#! /usr/env/bin/ruby
# Convert a string into an array of arrays with the word and number
# of occurances of that word.
#
# @param [text] : String
# @return [array] : [[word, count], [word2, count2]]
def analyze_paragraph(text)
# we don't need anything except spaces and lower case letters
text = text.to_s.downcase.gsub(/[^0-9A-Za-z\s]/, '')
# use a hash as it has better performance, and easily converts
# to an array of arrays
array_of_words = Hash.new
text.split(" ")
.uniq
.each { |word| array_of_words[word] = text.scan(word).length }
return array_of_words.to_a
end
p analyze_paragraph("To be, or not to be.")
p analyze_paragraph("Hello World!")
p analyze_paragraph(1)
p analyze_paragraph("")
p analyze_paragraph(:symbol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment