Question

Part 1 - Word Counts **Read in the text file** - In the cell below, read the #gettysburg_address.txt* file into Python. In [

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate - we can not consecrate - we can not hallow - this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us - that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion - that we here highly resolve that these dead shall not have died in vain - that this nation, under God, shall have a new birth of freedom - and that government of the people, by the people, for the people, shall not perish from the earth.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

difference between read() and readlines() is that read() return the n bytes of data from the file where n is specified inside the read().,if no parameter is passed it reads the entire path.

while readlines() reads all the lines in file and makes a list of it and returns it.

code for the above question -

 # function to get top 10 used words in file def solve(content): # splitting contents on the basis of comma(,) words = content.split(',') # join contents as string to further split according to spaces and storing in l as list l = ''.join(words).split(' ') # dictionary initialization dic = {} # storing word and its count in dictionary for i in l: # if word is in dictionary increase its count +1 if i in dic: dic[i]+=1 # if word not in dictionary,add it to dictionary and set count=1 else: dic[i]=1 # sorting dictionary according to the value in key,value pair in reversed order using lambda function # and storing into new sorted dictionary # here dic.items returns the total items or dictionary as list of (key,value) pair # key = lambda item: item[1] means sorting according to the second value in tuple. sorted_dictionary = {key: value for key,value in sorted(dic.items(), key=lambda item: item[1],reverse= True)} # priting the top 10 most occurred items from dictionary by cocnverting it into list first. print({key: sorted_dictionary[key] for key in list(sorted_dictionary)[:10]}) # openning file using with keyword in read mode with open("gettysburg_address.txt", "r") as f: # splitting contents of file on the basis of new line and storing in variable contents = f.read() print(contents) # passing contents list as string to the function for further processing solve(''.join(contents))

solve.py X solve.py > solve 1 # function to get top 10 used words in file def solve(content): # splitting contents on the bas

output -

PS C:\Users\dell(OneDrive\Desktop> python solve.py {that: 13, the: 9, to: 8, we: 8, a: 7, here: 7, -: 7, and:

python returns key,value pairs as tuple when dictionary.items() is called.

dic.items() = {list of (key,value) in dic}

if my answer helped then please upvote and comment for any queries

thankyou!

Add a comment
Know the answer?
Add Answer to:
Four score and seven years ago our fathers brought forth on this continent, a new nation,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective...

    SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective To gain experience with the operations involving binary search trees. This data structure as linked list uses dynamic memory allocation to grow as the size of the data set grows. Unlike linked lists, a binary search tree is very fast to insert, delete and search. Project Description When an author produce an index for his or her book, the first step in this process...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • "pHE FOURTH of March, 1865, started out to be dull and rainy. JL But later in...

    "pHE FOURTH of March, 1865, started out to be dull and rainy. JL But later in the day it cleared; and it turned out to be pleasant after all for the President's second inauguration. The streets were filled with milling crowds of people, with cav- alry patrols and police. The inauguration platform had been built on the east front of the Capitol; and here there was a vast sea of humanity, stretching as far as the eye could see, filHng...

  • Delivered August 28, 1963 by Dr. Martin Luther King Jr. I am happy to join with...

    Delivered August 28, 1963 by Dr. Martin Luther King Jr. I am happy to join with you today in what will go down in history as the greatest demonstration for freedom in the history of our nation. Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering...

  • The following five letters are representative of those the editors received and published in the issue...

    The following five letters are representative of those the editors received and published in the issue of March 29, 1999. Carefully read each letter, looking for slanted and charged language. List the language that you have identified. Then, list the verifiable facts that you have found. How do you know these facts are verifiable? How does the language used in each letter appeal to or discourage particular audiences? Please answer in 400 words. Kudos for your March 8 issue, "Voices...

  • Recursion and Trees Application – Building a Word Index Make sure you have read and understood...

    Recursion and Trees Application – Building a Word Index Make sure you have read and understood ·         lesson modules week 10 and 11 ·         chapters 9 and 10 of our text ·         module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...

  • Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make...

    Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics. This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT