Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use Python 3 for this question. In this question, we are going to do some analysis of Donald Trump's election speeches, and compare them

Please use Python 3 for this question.

In this question, we are going to do some analysis of Donald Trump's election speeches, and compare them to some of Barack Obama's speeches.

In speeches.txt (included in the tab to the right) there are two speeches: one by Obama (more here), and another by Trump (from here). Linguists have said that Trump's language is simple. One interpretation of this is that he uses short words more frequently. We will write a function that takes a string and returns the top-5 most frequent word lengths in the string to find out whether this is the case.

For this problem you should write two functions. The first function count_lengths should take a string text, and return a default dictionary containing the frequency counts of each word length. Your function should split text up into words based whitespace using the split method, without removing punctuation or changing the case of any of the "words".

The second function top5_word_lengths should take a string text, and return the list of the (up to) five most-frequent word lengths. It should operate by first calling count_lengths over text to generate a dictionary of word lengths, then sorting the word lengths in terms in descending order of frequency, and returning the top 5 (or less in the instance that there aren't 5). In the case of a tie in frequency, the word lengths should be sub-sorted in descending order of word length.

The two functions will be tested separately.

top5_word_lengths should return word-lengths in descendingorder in the event that they have the same frequency. If there are fewer than 5 word lengths it should return a shorter list of the sorted word lengths.

Your program should behave as follows:

image text in transcribed

Try running your program on the speeches by copy-pasting them from speeches.txt as an argument to your function.

speech_obama = """My fellow citizens: I stand here today humbled by the task before us, grateful for the trust you have bestowed, mindful of the sacrifices borne by our ancestors. I thank President Bush for his service to our nation, as well as the generosity and cooperation he has shown throughout this transition. Forty-four Americans have now taken the presidential oath. The words have been spoken during rising tides of prosperity and the still waters of peace. Yet, every so often the oath is taken amidst gathering clouds and raging storms. At these moments, America has carried on not simply because of the skill or vision of those in high office, but because We the People have remained faithful to the ideals of our forbearers, and true to our founding documents. So it has been. So it must be with this generation of Americans."""

speech_trump= """We we have had, no matter where we go you know, its a movement, folks. This is a movement. No matter where we go, its amazing. We go to Dallas. We go to Oklahoma. We go all over. We went to, the other night, Iowa packed. Every place New Hampshire, packed. Every place we go to, its like this. Its amazing. And just great, great people. And they actually had to tell over 3,000 people, go home. Well come back. Well do another one. But they had to go home. Would you like to leave and well let them come in? No?"""

count_lengths(one one was a racehorse two two was one too) defaultdict (Kclass int'>, 11: 1, 3: 8, 9: 11) >>> top5 _word lengths"one one was a racehorse two two was one too >>top5_wordlengths ("buffalo buffalo buffalo chicken buffalo") >>> top5 word lengths("the quick brown fox jumped over a lazy dog") [3, 5, 4, 6, 11

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

Students also viewed these Databases questions

Question

What risks does going global offer a small business owner?

Answered: 1 week ago

Question

2. How were various roles filled?

Answered: 1 week ago