In: Computer Science
Python 3 Program
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 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 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 . The output file of the Kennedy_index.txt, Kennedy_index_B.txt, Kennedy_index_C.txt, for input file “kennedy.txt”
Remember the output file name of your program is words_index.txt.
For this program, not running one (syntax error) will receive NO point.
Example of original text- 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
New text example - Kennedy_index.txt
Text File to be analyzed: Kennedy.txt We: 1 a: 1, 2, 4 an: 3 as: 4, 5, 6 beginning: 4 but: 2 celebration: 2 change: 6 end: 3 freedom: 3 not: 1 observe: 1 of: 2, 3 party: 2 renewal: 5 signifying: 5 symbolizing: 3 today: 1 victory: 1 well: 4, 5
example- Kenndy_index_B.txt
Text File to be analyzed: kennedy.txt We : 1 a : 1, 2, 4 an : 3 as : 4, 5, 6 beginning : 4 but : 2 celebration : 2 change : 6 end : 3 freedom : 3 not : 1 observe : 1 of : 2, 3 party : 2 renewal : 5 signifying : 5 symbolizing : 3 today : 1 victory : 1 well : 4, 5
example- Kenndy_index_C.txt
Text File to be analyzed: kennedy.txt We : 1 a : 1, 2, 4 an : 3 as : 4, 5, 6 beginning : 4 but : 2 celebration : 2 change : 6 end : 3 freedom : 3 not : 1 observe : 1 of : 2, 3 party : 2 renewal : 5 signifying : 5 symbolizing : 3 today : 1 victory : 1 well : 4, 5
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You ! Please update the below two lines -
filename = 'Kennedy.txt'
output_filename='words_index.txt'
=========================================================================== def read_file(filename): try: with open(filename, 'r') as infile: word_line_dict = {} line_number = 1 for line in infile.readlines(): for word in line.strip().split(): if word in word_line_dict.keys(): lines_list = word_line_dict.get(word) lines_list.append(line_number) else: lines_list = [line_number] word_line_dict[word] = lines_list line_number += 1 return word_line_dict except: return None def write_to_file(filename, word_dict): with open(filename, 'w+') as outfile: for word in sorted(word_dict.keys()): lines=word_dict.get(word) lines.sort() outfile.write('{}: {}\n'.format(word, ','.join([str(l) for l in lines]))) def main(): filename = 'Kennedy.txt' word_dict = read_file(filename) if word_dict is None: print('Error: File could not be read or file not available') return print(word_dict) output_filename='words_index.txt' write_to_file(output_filename,word_dict) main()
=======================================================================