Program 3 should first tell users that this is a word analysis program. 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. If 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. The program reads the contents of the text file, strip
emove new-line and punctuation characters, and then create a dictionary in which the key-value pairs are described as follows:
· Key. the words found in the file.
· 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.
Your program must write/create at least 4 functions:
1. a function gets the input file name from use
2. a function creates the dictionary
3. (Bonus, 10 Points) a function to write the top 6 words by the number they appear in the input file by descending order with the number to the output file “words_index.txt”.
4. a function to write the alphabetical listing of the words with their line numbers to the output file “words_index.txt”.
5. the main() function which the program starts here.
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 sample files, input file “romeo.txt” and output file “romeo_index.txt”.
· Remember the output file name of your program is words_index.txt.
· For this program, not running one (syntax e
or) will receive NO point.
· Be sure each line of the code and its comment is your own.
· No late-submission or extension at all for this program as we are near the end of the semester.
Text File to be analyzed: romeo.txt
The top 6 Most Common Words:
and:3
the:2
sun:2
is:2
yonder:1
The alphabetical listing of the words:
Arise : 3
But : 1
It : 2
Juliet : 2
Who : 4
already : 4
and : 2, 3, 4
eaks : 1
east : 2
envious : 3
fair : 3
grief : 4
is : 2, 4
kill : 3
light : 1
moon : 3
pale : 4
sick : 4
soft : 1
sun : 2, 3
the : 2, 3
through : 1
what : 1
window : 1
with : 4
yonder : 1
But $soft! What light through yonder window
eaks?
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!