Question

Pythong Program 4 should first tell users that this is a word analysis software. For any...

Pythong Program 4 should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time.

Ask a user to enter the name of a text file. Using try/except for invalid user input. Then the program reads the contents of the text file and create a dictionary in which the key-value pairs are described as follows:

  • Key. The key are the individual words found in the file.
  • Value. Each value is a list that contains the line numbers in the file where the word (the key) is found. Be aware that a list may have only one element.

Once the dictionary has been built, the program should create another text file for output, named “words_index.txt”. Next, write the contents of the dictionary to the file as an alphabetical listing of the words that are stored as keys in the dictionary (sorting the keys), along with the line numbers where the words appear in the original file. Please see the sample file for your reference.

Looking to seeing everyone to submit a well-done program! Here are some tips:

  • Documents/Comments of your program (Never more)
  • Testing your program by the given two files, Kennedy.txt and romeo.txt. The output file of the Kennedy_index.txt, Kennedy_index_B.txt, Kennedy_index_C.txt, for input file “kennedy.txt” and the output file romeo_index.txt, romeo_index_B.txt, romeo_index_C.txt, for input file romeo.txt have been posted for your reference.
  • Remember the output file name of your program is words_index.txt.

KENNEDY.TXT

We observe today not a victory
of party but a celebration
of freedom symbolizing an end
as well as a beginning
signifying renewal as well
as change

ROMEO.TXT

But soft what light through yonder window breaks

It is the east and Juliet is the sun

Arise fair sun and kill the envious moon

Who is already sick and pale with grief

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

Here is the code:

import collections

title = "* This is a word analysis software *"
stars = len(title)

print("*"*stars)
print(title)
print("*"*stars)
print()

filename = input("Enter name of the file to read: ")

try:
myfile = open(filename+".txt",'r+')
wordcount = {}
line = 1

for i in myfile.readlines():
words = i.split(" ")
for j in words:
j = j.rstrip()
if j in wordcount:
if line in wordcount[j]:
continue
wordcount[j] += [ line ]
else:
wordcount[j] = [ line ]
line += 1

sortedKeys = sorted((wordcount.items()))

od = collections.OrderedDict(sortedKeys)

words_index = open('word_index.txt','w')

for i, j in od.items():
words_index.write(f"{i} : {j} \n")
words_index.close()
except:
print("\nInvalid file name! Check whether file exists in the same directory or not! ")

-------------------------------------------------------------------------------------------------------------------------------------------------------------The Complete Code:

import collections title = * This is a word analysis software * stars = len(title) print(**stars) print(title) print(**

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

i) Exception handling for incorrect filenames:

* ****** D Files oi main.py saved 1 import collections * This is a word analysis software + IIIIIIIIIIIIIIII main.py title =

ii) taking user input (correct filename):

Files main.py E saved 1 import collections * This is a word analysis software * ************* ********* main.py demo.txt titl

iii) Input and Outputfiles:

Files e main.py demo.txt demo.txt saved 1 we observe today not a victory 2 of party but a celebration 3 of freedom symbolizin

| Files: è main.py demo.txt word_index.txt : to 00 word_index.txt saved 1 a : [1, 2, 4] 2 an : [3] as : [4, 5, 6] beginning :

Add a comment
Know the answer?
Add Answer to:
Pythong Program 4 should first tell users that this is a word analysis software. For any...
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
  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program...

    Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the input file to be spell checked. The program will read in the words for the dictionary, then will read the input file and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is, add...

  • Write a program that employs the four letter word dictionary to check the spelling of an...

    Write a program that employs the four letter word dictionary to check the spelling of an input word (test word). You will need to save the dictionary file to a folder on your computer. For this program you will prompt the user to enter a four letter word (or four characters). Then using a loop read each word from the dictionary and compare it to the input test word. If there is a match then you have spellchecked the word....

  • in c++ please. Write a program that reads the contents of a text file. The program...

    in c++ please. Write a program that reads the contents of a text file. The program should create a map in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the map would contain an element with "the" as the key and 128 as the value. The program should either display the frequency of each word or create...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Problem B: Clean up your potty mouth: blogging software Online blogs and other written content generation...

    Problem B: Clean up your potty mouth: blogging software Online blogs and other written content generation are very popular. However, as we try different ways to allow anybody on the internet to create and share written content, we run into a problem. A lot of people on the internet have foul potty-mouths and write lots of bad words. The common solution to this is to perform some form of automatic content filtering to either remove or obscure inappropriate text on...

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