Question

In: Computer Science

Write a Python program for the following: A given author will use roughly use the same...

Write a Python program for the following:

A given author will use roughly use the same proportion of, say, four-letter words in something she writes this year as she did in whatever she wrote last year. The same holds true for words of any length. BUT, the proportion of four-letter words that Author A consistently uses will very likely be different than the proportion of four-letter words that Author B uses. Theoretically, then, authorship controversies can sometimes be resolved by computing the proportion of 1-letter, 2-letter, 3-letter, ..., 13-letter words in the writing and then comparing it with the same statistics from known authors.

Your task is to write a Python program that computes the above statistics from any text file. Note that apostrophes do not count in the word length. For example, "he's" is a three-letter word. Words like hard-hearted should be replaced with two words with a space between them (hard hearted).

Name of input file: romeo_and_juliet.txt

Proportion of 1- letter words: 4.8% (1231 words)

Proportion of 2- letter words: 16.1% (4177 words)

Proportion of 3- letter words: 20.3% (5261 words)

Proportion of 4- letter words: 24.3% (6295 words)

Proportion of 5- letter words: 15.0% (3889 words)

Proportion of 6- letter words: 7.9% (2048 words)

Proportion of 7- letter words: 5.2% (1352 words)

Proportion of 8- letter words: 3.7% (953 words)

Proportion of 9- letter words: 1.5% (378 words)

Proportion of 10- letter words: 0.7% (190 words)

Proportion of 11- letter words: 0.3% (71 words)

Proportion of 12- letter words: 0.1% (20 words)

Proportion of 13- (or more) letter words: 0.0% (12 words)

Here the program is tested on the full text of Romeo and Juliet but it should work for any file. The sample run above shows the actual proportion and count of different sized words in the file. Hint: make sure to replace each character in ",.!?;:][-\"" in the text with a space before doing any splitting.

Solutions

Expert Solution

PROGRAM:

f=open("demo.txt")
st=f.read() #reading and storing data of a file
for i in ",.!?;:][-\"": #replace special characters
st=st.replace(i," ")
ss=st.split()
size=len(ss)
o=t=th=f=fi=s=se=e=n=te=el=tw=thir=0
for i in ss: #iterating through words and adding based on its length
if len(i)==1:
o=o+1
elif len(i)==2:
t=t+1
elif len(i)==3:
th=th+1
elif len(i)==4:
f=f+1
elif len(i)==5:
fi=fi+1
elif len(i)==6:
s=s+1
elif len(i)==7:
se=se+1
elif len(i)==8:
e=e+1
elif len(i)==9:
n=n+1
elif len(i)==10:
te=te+1
elif len(i)==11:
el=el+1
elif len(i)==12:
tw=tw+1
elif len(i)>12:
thir=thir+1
print("proportion of 1-letter words:",round((o/size)*100,1),"% (",o,"words)") #printing the proportion
print("proportion of 2-letter words:",round((t/size)*100,1),"% (",t,"words)")
print("proportion of 3-letter words:",round((th/size)*100,1),"% (",th,"words)")
print("proportion of 4-letter words:",round((f/size)*100,1),"% (",f,"words)")
print("proportion of 5-letter words:",round((fi/size)*100,1),"% (",fi,"words)")
print("proportion of 6-letter words:",round((s/size)*100,1),"% (",s,"words)")
print("proportion of 7-letter words:",round((se/size)*100,1),"% (",se,"words)")
print("proportion of 8-letter words:",round((e/size)*100,1),"% (",e,"words)")
print("proportion of 9-letter words:",round((n/size)*100,1),"% (",n,"words)")
print("proportion of 10-letter words:",round((t/size)*100,1),"% (",te,"words)")
print("proportion of 11-letter words:",round((el/size)*100,1),"% (",el,"words)")
print("proportion of 12-letter words:",round((tw/size)*100,1),"% (",tw,"words)")
print("proportion of 13(or more)-letter words:",round((thir/size)*100,1),"% (",thir,"words)")
  
SCREENSHOT: if any indentation errors please verify with the screen shot

file used for the following output:

OUTPUT:


Related Solutions

Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar date after January 1, 1900, which was a Monday. This program will need to account for leap years, which occur in every year that is divisible by 4, except for years that are divisible by 100 but are not divisible by 400. For example, 1900 was not a leap year, but 2000 was a leap year.
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: Reads information...
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: 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...
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
Write a Python program, in a file called StickFigure.py, which, given a value for the total...
Write a Python program, in a file called StickFigure.py, which, given a value for the total height of a stick figure, uses a recursive function to generate the stick figure pattern as shown below. The recursive function is used to print the entire figure. Note that the head, arms and legs stay the same, but the body can be different lengths, depending on what the user enters as the height of the figure. The "body" length is always 3 lines...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT