Question

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. 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 otuput, 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 create a sample file for your reference.

Here are some tips:

  • Documents/Comments of your program (Never more)
  • Testing your program by the creating 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
  • Remember the output file name of your program is words_index.txt.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the following program to analyze the words present in input file and added necessary comments in the program.

Program:

print(" This is a word analysis software")
fileName=input("Enter the input file name: ")

#empty dictionary declaration
finalDict ={}
#line number variable
number = 0

#exception handler if file is not present
try:
fh = open(fileName, "r")
except IOError:
print("Error: Unable to open input file to read data")
exit()

#read the file line one by one and update the dictionary
for line in fh:
number = number + 1
list1 = line.split()
#get the unique words from a line
list1 = set(list1)
for item in list1:
#if the word is not present in dictionary, create a new key for the words
#and update the line number as a list
if item not in finalDict:
listTemp=[]
listTemp.append(number)
finalDict[item] = listTemp
else:
#if word is present as a key then append the line number to the existing list
finalDict[item].append(number)

#uncomment the following code for debugging
#print(finalDict)

try:
f = open("words_index.txt", "w")
except IOError:
print("Error: Unable to open input file to read data")
exit()

f.write("WORDS Line Numbers\n")
for i in sorted (finalDict.keys()) :
f.write("%-10s " %(i))
for item in finalDict[i]:
f.write("%s " %(item))
f.write("\n")
  

Output:

This is a word analysis software
Enter the input file name: input.txt


cat words_index.txt

WORDS Line Numbers
asd 1 3
gfd 3
gfh 1
ghjgh 3
kgj 2
sdf 2
wer 2

Screen Shot:

input.txt

words_index.txt

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

  • (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...

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • 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...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • 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...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • 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...

  • 7.10 LAB: Sorting TV Shows (dictionaries and lists)

    Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since multiple shows could have the same number of seasons).Sort...

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