Last active
November 9, 2015 21:16
-
-
Save alexggordon/ee89c10f7837dc9ade91 to your computer and use it in GitHub Desktop.
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/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