create the program to read the lines of the file, split each line into words, count the words and total their length. You should skip any numbers that are in the file and strip any punctuation from the words. Consider the following characters as punctuation , . ' : ; ? ! \ " ( ). Some of the words in the files may contain apostrophes (') but ignore them for this part of the exercise. For example, "NASA's" would be counted as a single word of length 6. You should print out the number of words and their average length. (8 pts). (3) The final step is to implement a function remove_apos() function, which has a string as a parameter, and returns a string with any apostrophes removed.in the string. These should be removed so that only the letters in a word are counted. For example, the word "NASA's" should be treated as "NASAs" and would have a length of 5.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section: Chapter Questions
Problem 2PP: (Data processing) a. Store the following data in a file, or use the numbers.dat file provided on...
icon
Related questions
Question

In this exercise, you will build a program to process files containing paragraphs. The aim of the program is to simply count the number of words and compute their length. The file, however, may contain numbers (e.g. "2018") and contains punctuation that should be ignored.

There are two data files that you will use to test your program: shortpar.txt and cassini.txt.

(1) Prompt the user to enter the name of a file. Store the text in a string and output the name of the file. (4 pts)

(2) Now create the program to read the lines of the file, split each line into words, count the words and total their length. You should skip any numbers that are in the file and strip any punctuation from the words. Consider the following characters as punctuation , . ' : ; ? ! \ " ( ). Some of the words in the files may contain apostrophes (') but ignore them for this part of the exercise. For example, "NASA's" would be counted as a single word of length 6. You should print out the number of words and their average length. (8 pts).

(3) The final step is to implement a function remove_apos() function, which has a string as a parameter, and returns a string with any apostrophes removed.in the string. These should be removed so that only the letters in a word are counted. For example, the word "NASA's" should be treated as "NASAs" and would have a length of 5. (16 pts)

I'm stuck on how to get rid of the punctuation becuase it looks like my program is still counting it as well as how to program the remove_apos() function.

1
2 # Define your functions here
3 def count_words(file):
4
count = 0
5
for line in file:
6
line = line.split()
for word in line:
punc_string -', . ': ; ? !\"()0123456789'
if word in punc_string:
8
10
count + 0
11
else:
12
count+=1
13
return count
14
15 def avg_length(file):
num_words = 0
16
17
sum = 0
18
for line in file:
line = line.split()
for word in line:
punc_string -'
if word in punc_string:
num_words += 0
19
20
21
V: ; ? ! V"( )0123456789'
22
23
24
sum += 0
else:
25
26
num_words +1
27
sum += len(word)
28
avg - sum/num_words
return avg
29
30
31
32 def remove_apos():
33
34 if -name - '_main_':
35
36
# Prompt the user for the name of the file and open it for reading
file_name = input("Enter name of file:\n")
print("File to be processed is: "+ file_name)
37
38
39
40
copied_file = open(file_name,'r')
data = copied_file.readlines(O
copied_file.close()
41
42
43
44
num_words = count_words(data)
45
avg_length_word = avg_length(data)
46
47
print("The number of words is: ", num_words)
print("The average length of a word is: ", round(avg_length_word, 2))
48
49
Transcribed Image Text:1 2 # Define your functions here 3 def count_words(file): 4 count = 0 5 for line in file: 6 line = line.split() for word in line: punc_string -', . ': ; ? !\"()0123456789' if word in punc_string: 8 10 count + 0 11 else: 12 count+=1 13 return count 14 15 def avg_length(file): num_words = 0 16 17 sum = 0 18 for line in file: line = line.split() for word in line: punc_string -' if word in punc_string: num_words += 0 19 20 21 V: ; ? ! V"( )0123456789' 22 23 24 sum += 0 else: 25 26 num_words +1 27 sum += len(word) 28 avg - sum/num_words return avg 29 30 31 32 def remove_apos(): 33 34 if -name - '_main_': 35 36 # Prompt the user for the name of the file and open it for reading file_name = input("Enter name of file:\n") print("File to be processed is: "+ file_name) 37 38 39 40 copied_file = open(file_name,'r') data = copied_file.readlines(O copied_file.close() 41 42 43 44 num_words = count_words(data) 45 avg_length_word = avg_length(data) 46 47 print("The number of words is: ", num_words) print("The average length of a word is: ", round(avg_length_word, 2)) 48 49
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr