Write a function word_counts

Assignment Help Programming Languages
Reference no: EM13324583

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 = <<SAMPLE_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

Reference no: EM13324583

Questions Cloud

Find the heat absorbed by the gas during this process : A gas is allowed to expand isothermally until it reaches its final volume and its pressure is 2.97 atm. Find the heat absorbed by the gas during this process
In which sentence is the clause used as an adverb : Each one of the following sentences contains a clause. In which sentence is the clause used as an adverb?
Employees earnings and taxes : Employees Earnings and Taxes
Explain the solubility product constant for agcl : The solubility product constant for AgCl at 298 K is 1.82 × 10-10. Calculate E° for the process
Write a function word_counts : Write a function word_counts 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:
Find how far do you go during the whole trip : Starting from rest, you move with a constant acceleration of 2.1 m/s2 for 15.0 s and then move with an acceleration of -2.1 m/s2 for another 15.0 s. How far do you go during the whole trip
What gain or loss must be recognized on the distribution : What gain or loss must be recognized on the distribution, and who must recognize it?
Calculate the new volume of the gas : An ideal gas occupies a volume of 2.85 m3 at STP. The pressure of the gas in increased to 3.71 atm. Calculate the new volume of the gas
What impact did early white immigrants into tennessee : What impact did early white "immigrants" into Tennessee have on the imperial wars in North America prior to and during the American War of Independence?

Reviews

Write a Review

Programming Languages Questions & Answers

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd