Question

In: Computer Science

In Python. The file “essay.txt” attached to this assignment includes an essay. The essay includes a...

In Python. The file “essay.txt” attached to this assignment includes an essay. The essay includes a couple of sections that are separated by two consecutive newline characters (i.e. ‘\n’) that are shown as empty lines between the sections if you open the file in a text editor like Notepad. Each section starts with a title followed by a couple of paragraphs; the title and the paragraphs are separated by a newline character. Each paragraph includes a couple of sentences that are ended with either a period or a question mark which is followed by a space character if the paragraph has more sentences. Explore the explained organization of the file by opening the file in Notepad before moving on to the programming part. Use this file to write a program that:

1. Displays the number of sections, paragraphs, lines, words, and characters (excluding the white-space and punctuation characters) included in this essay.

2. Lists all the distinct words included in the essay in a lexical order (i.e. alphabetical order regardless of the length of each word.)

3. Displays a sorted list of distinct words included in the essay including the number of occurrences of each word in the essay on the screen. The output list is a two-level sorted list that is first sorted by the length of the words and second all the words of a specific length are alphabetically sorted. A sample output of this step could be like:

Length Word Number of Occurrences

1 a 24

1 I 5

2   am 5

Hint: In order to access all the words of a specific length more efficiently, you may need to create a dictionary including the items in the format of “length : listOfWords” where length is of the type of int that is used to retrieve a list including all the words of that length as the value. Assumptions and requirements

1. The file does not include any punctuation mark other than period, comma, or question mark.

2. There is either one space or a comma followed by a space between two consecutive words in a sentence.

3. There is no digit or apostrophe character included in the file.

4. This program is case-insensitive for the first letter of the words, however, a word that is all in uppercase is considered as a distinct word (i.e. it is an abbreviation). For example, the words “Air” and “air” are considered as the same word, however, the word “AIR” is not the same as any of the two previous words.

Essay.txt

What is pollution?
Environmental pollution occurs when pollutants contaminate the natural surroundings. Pollution disturbs the balance of our ecosystems, affect our normal lifestyles and gives rise to human illnesses and global warming. Pollution has reached its peak due to the development and modernization in our lives. With the development of science and technology, there has been a huge growth of human potentials. People have become prisoners of their own creations.
We waste the bounties of our nature without a thought that our actions cause serious problems. We must deepen our knowledge of nature`s laws and broaden our understanding of the laws of the human behavior in order to deal with pollution problems. So, it is very important to know different types of pollutions, their effects and causes on humanity and the environment we live in.

Types, causes, and effects of pollution
Air pollution is one of the most dangerous forms of pollution. A biological, chemical, and physical alteration of the air occurs when smoke, dust, and any harmful gases enter into the atmosphere and make it difficult for all living beings to survive as the air becomes contaminated. Burning of fossil fuels, agriculture related activities, mining operations, exhaust from industries and factories, and household cleaning products entail air pollution. People release a huge amount of chemical substances in the air every day. The effects of air pollution are alarming. It causes global warming, acid rains, respiratory and heart problems, and eutrophication. A lot of wildlife species are forced to change their habitat in order to survive.
Soil pollution occurs when the presence of pollutants, contaminants, and toxic chemicals in the soil is in high concentration that has negative effect on wildlife, plants, humans, and ground water. Industrial activity, waste disposal, agricultural activities, acid rain, and accidental oil spill are the main causes of soil pollution. This type of contamination influence health of humans, affects the growth of plants, decreases soil fertility, and changes the soil structure.
Water pollution is able to lead our world on a path of destruction. Water is one of the greatest natural resources of the whole humanity. Nothing will be able to live without water. However, we do not appreciate this gift of nature and pollute it without thinking. The key causes of the water pollution are: industrial waste, mining activities, sewage and waste water, accidental oil leakage, marine dumping, chemical pesticides and fertilizers, burning of fossil fuels, animal waste, urban development, global warming, radioactive waste, and leakage from sewer lines. There is less water available for drinking, cooking, irrigating crops, and washing.

Light pollution
Light pollution occurs because of the prominent excess illumination in some areas. Artificial lights disrupt the world`s ecosystems. They have deadly effects on many creatures including mammals, plants, amphibians, insects, and birds. Every year many bird species die colliding with needlessly illuminated buildings. Moreover, artificial lights can lead baby sea turtles to their demise.
Noise pollution takes place when noise and unpleasant sounds cause temporary disruption in the natural balance. It is usually caused by industrialization, social events, poor urban planning, household chores, transportation, and construction activities. Noise pollution leads to hearing problems, health issues, cardiovascular issues, sleeping disorders, and trouble communicating. Moreover, it affects wildlife a lot. Some animals may suffer from hearing loss while others become inefficient at hunting. It is very important to understand noise pollution in order to lower its impact on the environment.
Radioactive pollution is the presence of radioactive substances in the environment. It is highly dangerous when it occurs. Radioactive contamination can be caused by breaches at nuclear power plants or improper transport of radioactive chemicals. Radioactive material should be handled with great care as radiation destroys cells in living organisms that can result in illness or even death.

Solutions to pollution problems
Environmental pollution has negatively affected the life of both animals and human-beings. The only way to control current environmental issues is to implement conservation methods and create sustainable development strategies. We should find some effective solutions in order to restore our ecological balance.
First of all, we should make sustainable transportation choices. We should take advantage of public transportation, walk or ride bikes whenever possible, consolidate our trips, and consider purchasing an electric car. It is very important to make sustainable food choices. Choose local food whenever possible; buy organically grown vegetables and fruits or grow your own.
People should conserve energy. Turn off electronics and lights when you are not in the room. Consider what small changes can lead to big energy savings. Use energy efficient devices. It is also essential to understand the concept of reduce, Reuse and Recycle. Try to buy used items whenever possible. Choose products with minimal packaging. Buy reusable items. Remember that almost everything that you purchase can be recycled.
Conserve water as much as possible. Dispose of toxic waste properly. Do not use herbicides and pesticides. Use natural, environmentally friendly chemicals for your everyday chores.

Conclusion
Environmental pollution is one of the biggest problems caused by human activities that we should overcome to see a tomorrow and guarantee our descendants a healthy life.  There are many environmental concerns for communities around the world to address. We should always remember that pollution problems affect us all so each of us has to do his or her best to help restore ecological balance to this beautiful place we call home. Learn about the major polluters in your area to protect the air and water where you live. Encourage people to stop pollution, tell them everything you know about this problem, and protest local polluters together. The masses should be educated on the danger of different types of pollution. People should know everything about all consequences of the environmental

Solutions

Expert Solution

Prorgram

import collections
FileName = "Essay.txt"
fi = open(FileName)
NewLine,NumSection,NumParagraph,LineCnt,NumWords,NumChar=0,0,0,0,0,0
ParaCnt = False
dictCnt = {}
for line in fi:
LineCnt+=1
#NewLine = 0
lstword = line.split()
if len(lstword)==0:
NewLine+=1
ParaCnt = False
else:
NewLine+=1
if ParaCnt == True:
NumParagraph+=1
for word in lstword:
if word.endswith(".") or word.endswith("?"):
w = word[:-1].capitalize()
if w in dictCnt:
dictCnt[w]+=1
else:
dictCnt[w]=1
else:
w = word.capitalize()
if w in dictCnt:
dictCnt[w]+=1
else:
dictCnt[w]=1
if NewLine == 2:
NumSection+=1
ParaCnt = True
NewLine=0
k = sorted(dictCnt.items(), key = lambda x : (x[1],x[0]))
for v in k:
NumWords+=v[1]
NumChar+=(len(v[0])*v[1])
print("Sorted List")
print("-"*50)
print(k)
print("-"*50)
print("\n\nSummary")
print("-"*50)
print("Number of Lines : %s" %str(LineCnt))
print("Number of Words : %s" %str(NumWords))
print("Number of Characters : %s" %str(NumChar))
print("Number of sections : %s" %str(NumSection))
print("Number of Paragraph : %s" %str(NumParagraph))

Output:

python readFile.py
Sorted List
--------------------------------------------------
[('Actions', 1), ('Activity,', 1), ('Address', 1), ('Advantage', 1), ('Affected', 1), ('Agricultural', 1), ('Agriculture', 1), ('Alarming', 1), ('All,', 1), ('Almost', 1), ('Also', 1), ('Alteration', 1), ('Always', 1), ('Amount', 1), ('Amphibians,', 1), ('An', 1), ('Animal', 1), ('Any', 1), ('Appreciate', 1), ('Are:', 1), ('Area', 1), ('Areas', 1), ('Around', 1), ('Atmosphere', 1), ('Available', 1), ('Baby', 1), ('Beautiful', 1), ('Because', 1), ('Becomes', 1), ('Been', 1), ('Behavior', 1), ('Beings', 1), ('Best', 1), ('Big', 1), ('Biggest', 1), ('Bikes', 1), ('Biological,', 1), ('Bird', 1), ('Birds', 1), ('Both', 1), ('Bounties', 1), ('Breaches', 1), ('Broaden', 1), ('Buildings', 1), ('Call', 1), ('Car', 1), ('Cardiovascular', 1), ('Care', 1), ('Causes,', 1), ('Cells', 1), ('Change', 1), ('Chemical,', 1), ('Chores', 1), ('Chores,', 1), ('Cleaning', 1), ('Colliding', 1), ('Communicating', 1), ('Communities', 1), ('Concentration', 1), ('Concept', 1), ('Concerns', 1), ('Conclusion', 1), ('Consequences', 1), ('Conservation', 1), ('Consolidate', 1), ('Construction', 1), ('Contaminants,', 1), ('Contaminate', 1), ('Contaminated', 1), ('Control', 1), ('Cooking,', 1), ('Create', 1), ('Creations', 1), ('Creatures', 1), ('Crops,', 1), ('Current', 1), ('Danger', 1), ('Day', 1), ('Deadly', 1), ('Deal', 1), ('Death', 1), ('Decreases', 1), ('Deepen', 1), ('Demise', 1), ('Descendants', 1), ('Destroys', 1), ('Destruction', 1), ('Development,', 1), ('Devices', 1), ('Die', 1), ('Difficult', 1), ('Disorders,', 1), ('Disposal,', 1), ('Dispose', 1), ('Disrupt', 1), ('Disruption', 1), ('Disturbs', 1), ('Drinking,', 1), ('Due', 1), ('Dumping,', 1), ('Dust,', 1), ('Each', 1), ('Ecosystems', 1), ('Ecosystems,', 1), ('Educated', 1), ('Effect', 1), ('Effective', 1), ('Efficient', 1), ('Electric', 1), ('Electronics', 1), ('Encourage', 1), ('Entail', 1), ('Enter', 1), ('Environmentally', 1), ('Essential', 1), ('Eutrophication', 1), ('Even', 1), ('Events,', 1), ('Everyday', 1), ('Excess', 1), ('Exhaust', 1), ('Factories,', 1), ('Fertility,', 1), ('Fertilizers,', 1), ('Find', 1), ('First', 1), ('Forced', 1), ('Forms', 1), ('Friendly', 1), ('Fruits', 1), ('Gases', 1), ('Gift', 1), ('Gives', 1), ('Great', 1), ('Greatest', 1), ('Ground', 1), ('Grow', 1), ('Grown', 1), ('Guarantee', 1), ('Habitat', 1), ('Handled', 1), ('Harmful', 1), ('Healthy', 1), ('Heart', 1), ('Help', 1), ('Her', 1), ('Herbicides', 1), ('High', 1), ('Highly', 1), ('His', 1), ('Home', 1), ('However,', 1), ('Human-beings', 1), ('Hunting', 1), ('Illness', 1), ('Illnesses', 1), ('Illuminated', 1), ('Illumination', 1), ('Impact', 1), ('Implement', 1), ('Improper', 1), ('Including', 1), ('Industrialization,', 1), ('Industries', 1), ('Inefficient', 1), ('Influence', 1), ('Insects,', 1), ('Into', 1), ('Irrigating', 1), ('Issues', 1), ('Key', 1), ('Knowledge', 1), ('Leads', 1), ('Leakage', 1), ('Leakage,', 1), ('Learn', 1), ('Less', 1), ('Lifestyles', 1), ('Lines', 1), ('Lives', 1), ('Loss', 1), ('Lower', 1), ('Main', 1), ('Major', 1), ('Mammals,', 1), ('Marine', 1), ('Masses', 1), ('Material', 1), ('May', 1), ('Methods', 1), ('Minimal', 1), ('Modernization', 1), ('Most', 1), ('Much', 1), ('Must', 1), ('Natural,', 1), ('Nature`s', 1), ('Needlessly', 1), ('Negative', 1), ('Negatively', 1), ('Normal', 1), ('Nothing', 1), ('Nuclear', 1), ('Off', 1), ('Only', 1), ('Operations,', 1), ('Organically', 1), ('Organisms', 1), ('Others', 1), ('Overcome', 1), ('Packaging', 1), ('Path', 1), ('Peak', 1), ('Physical', 1), ('Planning,', 1), ('Plants', 1), ('Pollutants', 1), ('Pollutants,', 1), ('Pollute', 1), ('Pollution,', 1), ('Pollutions,', 1), ('Poor', 1), ('Possible,', 1), ('Possible;', 1), ('Potentials', 1), ('Power', 1), ('Prisoners', 1), ('Problem,', 1), ('Prominent', 1), ('Properly', 1), ('Protect', 1), ('Protest', 1), ('Public', 1), ('Purchase', 1), ('Purchasing', 1), ('Radiation', 1), ('Rain,', 1), ('Rains,', 1), ('Reached', 1), ('Recycle', 1), ('Recycled', 1), ('Reduce,', 1), ('Related', 1), ('Release', 1), ('Resources', 1), ('Respiratory', 1), ('Result', 1), ('Reusable', 1), ('Reuse', 1), ('Ride', 1), ('Rise', 1), ('Room', 1), ('Savings', 1), ('Science', 1), ('Sea', 1), ('See', 1), ('Serious', 1), ('Sewage', 1), ('Sewer', 1), ('Sleeping', 1), ('Small', 1), ('Smoke,', 1), ('So', 1), ('So,', 1), ('Social', 1), ('Sounds', 1), ('Spill', 1), ('Stop', 1), ('Strategies', 1), ('Structure', 1), ('Suffer', 1), ('Surroundings', 1), ('Take', 1), ('Takes', 1), ('Technology,', 1), ('Tell', 1), ('Temporary', 1), ('Them', 1), ('They', 1), ('Thinking', 1), ('Thought', 1), ('Together', 1), ('Tomorrow', 1), ('Transport', 1), ('Transportation', 1), ('Trips,', 1), ('Trouble', 1), ('Try', 1), ('Turn', 1), ('Turtles', 1), ('Type', 1), ('Types,', 1), ('Understanding', 1), ('Unpleasant', 1), ('Used', 1), ('Usually', 1), ('Vegetables', 1), ('Walk', 1), ('Warming', 1), ('Washing', 1), ('Water,', 1), ('Way', 1), ('Where', 1), ('While', 1), ('Whole', 1), ('Wildlife,', 1), ('Will', 1), ('World`s', 1), ('Year', 1), ('Able', 2), ('Accidental', 2), ('Acid', 2), ('Activities', 2), ('Affect', 2), ('Affects', 2), ('Animals', 2), ('Artificial', 2), ('At', 2), ('Become', 2), ('Burning', 2), ('Cause', 2), ('Changes', 2), ('Chemical', 2), ('Choices', 2), ('Choose', 2), ('Conserve', 2), ('Consider', 2), ('Contamination', 2), ('Dangerous', 2), ('Different', 2), ('Ecological', 2), ('Every', 2), ('Food', 2), ('Fossil', 2), ('Fuels,', 2), ('Growth', 2), ('Have', 2), ('Health', 2), ('Hearing', 2), ('Household', 2), ('Huge', 2), ('Humanity', 2), ('Humans,', 2), ('Industrial', 2), ('Issues,', 2), ('Items', 2), ('Its', 2), ('Laws', 2), ('Life', 2), ('Light', 2), ('Living', 2), ('Local', 2), ('Lot', 2), ('Mining', 2), ('Moreover,', 2), ('Nature', 2), ('Oil', 2), ('Own', 2), ('Pesticides', 2), ('Place', 2), ('Polluters', 2), ('Possible', 2), ('Presence', 2), ('Problems,', 2), ('Products', 2), ('Remember', 2), ('Restore', 2), ('Solutions', 2), ('Species', 2), ('Substances', 2), ('Survive', 2), ('Toxic', 2), ('Transportation,', 2), ('Types', 2), ('Understand', 2), ('Urban', 2), ('Us', 2), ('Warming,', 2), ('What', 2), ('Wildlife', 2), ('World', 2), ('About', 3), ('Activities,', 3), ('All', 3), ('Buy', 3), ('By', 3), ('Caused', 3), ('Chemicals', 3), ('Development', 3), ('Do', 3), ('Energy', 3), ('Environment', 3), ('Everything', 3), ('From', 3), ('Global', 3), ('Important', 3), ('Know', 3), ('Lead', 3), ('Lights', 3), ('Live', 3), ('Make', 3), ('Many', 3), ('Natural', 3), ('Not', 3), ('One', 3), ('Plants,', 3), ('Some', 3), ('Sustainable', 3), ('There', 3), ('Use', 3), ('Very', 3), ('Waste,', 3), ('Whenever', 3), ('Without', 3), ('Your', 3), ('As', 4), ('Balance', 4), ('Causes', 4), ('Effects', 4), ('For', 4), ('Human', 4), ('Noise', 4), ('Order', 4), ('Their', 4), ('This', 4), ('Waste', 4), ('You', 4), ('Are', 5), ('Be', 5), ('Can', 5), ('Has', 5), ('Occurs', 5), ('Or', 5), ('People', 5), ('Problems', 5), ('Soil', 5), ('With', 5), ('Environmental', 6), ('On', 6), ('Radioactive', 6), ('When', 6), ('Air', 7), ('That', 7), ('Water', 8), ('A', 9), ('Should', 9), ('We', 10), ('It', 11), ('Our', 11), ('Is', 15), ('In', 16), ('Pollution', 25), ('To', 28), ('And', 38), ('The', 40), ('Of', 41)]
--------------------------------------------------


Summary
--------------------------------------------------
Number of Lines : 22
Number of Words : 952
Number of Characters : 5280
Number of sections : 4
Number of Paragraph : 11


Related Solutions

ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
Python: The attached file (books.csv) contains a list of some of the world's most famous books,...
Python: The attached file (books.csv) contains a list of some of the world's most famous books, including author, title and approximate year written/published. Your task is to write a program to do the following: Create a class "Book()" that can store the values passed as arguments at creation time as attributes called "title", "year", and "author". NOTE: Be sure to convert the year data to an integer, and to deal with any data that cannot be converted by setting the...
Please also go over the Excel file attached to this assignment in order to familiarize yourself...
Please also go over the Excel file attached to this assignment in order to familiarize yourself with the different ways Excel can be used to solve Time Value of Money/Dividend Discount Model problems. There are three worksheets in the Excel file. This Excel file with examples is just that: a file to show you some examples of using Excel to solve TVM /DDM problems. Do not confuse this posted Excel file with the separate Excel file you need to create...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
In Python. A file concordance tracks the unique words in a file and their frequencies. Write...
In Python. A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies. Below is an example file along with the program input and output: Input : test.txt output : 3/4 1, 98 1, AND 2, GUARANTEED 1,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT