Question

Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name...

Problem 3 Write a function named repeat_words that takes two string parameters:

1. in_file: the name of an input file that exists before repeat_words is called

2. out_file: the name of an output file that repeat_words creates

Assume that the input file is in the current working directory and write the output file to that directory.

For each line of the input file, the function repeat_words should write to the output file all of the words that appear more than once on that line. Each word should be lower cased and stripped of leading and trailing punctuation. Each repeated word on a line should be written to the corresponding line of the output file only once, regardless of the number of times the word is repeated.

For example, if the following is the content of the file catInTheHat.txt:

Too wet to go out and too cold to play ball.

So we sat in the house.

We did nothing at all.

So all we could do was to Sit! Sit! Sit! Sit!

The following function call:

inF = 'catInTheHat.txt'

outF = 'catRepWords.txt'

repeat_words(inF, outF)

should create the file catRepWords.txt with the content:

too to

sit

Hint: Be sure to test your solution with input in which some repeated words on a line are a mixture of upper and lower case, and in which repeated words sometimes are preceded or followed by punctuation.

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

Thanks for posting the question, we are glad to help you. Below is the complete code in python. I had to write a small utility function to strip off and punctuation symbols at the end.

The code uses list() dictionary to track the words and finally outputs the repeated words in the output file. Refer the screenshot for code indentation and output sample

Note : you may need to make changes to the below line based on the file path available in your system

Thats It. Thank you so much :)

-------------------------------------------------------------------------------------------------------------------------------

def repeat_words(inF, outF):
  
ouput_lines=[]
with open(inF,'r') as input_file:
for line in input_file:
words = line.split()
word_dict={}
for word in words:
word = remove_punctuation_from_end(word).lower()
if word_dict.get(word)==None:
word_dict[word]=1
else:
word_dict[word]= word_dict.get(word) + 1
words_freq_greater_than_one=[]
for word,count in word_dict.items():
if count>1:
words_freq_greater_than_one.append(word)
  
if len(words_freq_greater_than_one)>0:   
ouput_lines.append(words_freq_greater_than_one)   
  
with open(outF,'w') as output_file:
for line in ouput_lines:
for word in line:
output_file.write(word + " ")
output_file.write("\r\n")

def remove_punctuation_from_end(word):
  
last_letter = word[-1]
if(last_letter.isalpha()):
return word
else:
return remove_punctuation_from_end(word[:-1])

inF = "D:\\catInTheHat.txt"
outF = "D:\\catRepWords.txt"
repeat_words(inF, outF)

----------------------------------------------------------------------------------------------------------------------------------

Image screenshot

Console S3 |โร Hierarch View far ward t- words: tf wra dict.ord) ne: Too wet to go out and too cold to play ball So we sal in Lhe house we did nnthing at all So all we could do was to Sit! Sit! Sit! Sit! ward diemwrd dirt.cet word)1 for ward,count in wr dict.iter(: autput_file.writer autput_file.nritel ri) too to sit ρ last-letter wrd[-11

thank you so much :) Please post your comments if you have any query, i'll be happy to assist you.

A thumbs up please !!!

Add a comment
Know the answer?
Add Answer to:
Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name...
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
  • 2. Write a function file_copy() that takes two string parameters: in file and out_file and copies...

    2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different...

    Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when uniqueWords is called. uniquewords creates the output file. Input. The function uniquewords takes two parameters: ◆ inFile, a string that is the name of text file that is to be read. The file that inFile refers...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...

    Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file exists when wordLengths is called; wordLengths must create the file outFile. The input file contains only letters and white space For each line of the input file, wordLengths should identify the length of the longest word...

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". The file must be named: words_in_both.py...

  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...

  • Write a recursive function that takes a string as input. This recursive function should open a...

    Write a recursive function that takes a string as input. This recursive function should open a file by the name of the string, and read the first word. Then attempt to open a file by the word you just read and repeat this process. This ends, when there is no file with the name you are looking for. At this point, cout the last word in the current file (the one where the first word was not a different file)....

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". You can use Python's split() funciton,...

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