Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | May 2017 |
Last Sign in: | 306 Weeks Ago |
Questions Answered: | 66690 |
Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Problem 3
Write a function word_count(s) that parses the string s into words and returns an array (of arrays of the form [word, count]) representing the frequency of each word. Words are case-insensitive: 'Hello', 'HELLO', and 'hello' are instances of the same word. It is helpful to write helper functions in addition to word_count.
The following processes the first paragraph of Alice in Wonderland. Note that res2 is an array of those words that occur at least twice in this paragraph.
some_text =
Alice was beginning to get very tired of sitting by her sister on the bank,and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it," and what is the use of a book," thought Alice
"without pictures or conversation?"
SAMPLE_TEXT
Â
res = word_count(some_text) print res, "\n\n"
res2 = res.select { |pair| pair[1] > 1 } res2.sort.each do |key, count| puts "#{key}: #{count}" end
Â
[["of", 3], ["or", 3], ["the", 3], ["alice", 2], ["and", 2], ["book",
2], ["had", 2], ["her", 2], ["it", 2], ["pictures", 2], ["sister", 2],
["to", 2], ["was",2], ["a", 1], ["bank", 1], ["beginning", 1], ["but",
1], ["by", 1], ["conversation", 1], ["conversations", 1], ["do", 1],
["get", 1], ["having", 1], ["in", 1],["into", 1], ["is", 1], ["no",
1], ["nothing", 1], ["on", 1], ["once", 1], ["peeped", 1], ["reading",
1], ["she", 1], ["sitting", 1], ["thought", 1], ["tired",1], ["twice", 1], ["use", 1], ["very", 1], ["what", 1], ["without", 1]]
alice: 2 and: 2 book: 2 had: 2 her: 2 it: 2 of: 3 or: 3
pictures: 2
sister: 2
the: 3
to: 2
was: 2
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll