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

must use python Write a nested for loop that displays the following output using the same...
must use python Write a nested for loop that displays the following output using the same integer from part a:                                                     1                                                 1   2   1                                             1   2   4   2   1                                         1   2   4   8   4   2   1                                     1   2   4   8 16   8   4   2   1                                 1   2   4   8 16 32 16   8   4   2   1                             1   2   4   8 16 32 64 32 16   8   4   2   1                         1   2   4  ...
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.
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,...
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...
use Python datetime module, strftime Write a program that processes the following textfile and prints the...
use Python datetime module, strftime Write a program that processes the following textfile and prints the report shown below. Textfile: Ask the user for the filename and BE SURE TO CHECK THAT IT EXISTS. The file will contain lines which each contain 2 strings (bookTitle and publicationDate) separated by a comma and a space. Note that the title of the book might contain spaces! Example of the textfile: Gone Girl, 5-24-2012 To Kill A Mockingbird, 7-11-1960 Harry Potter and the...
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...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT