Alice_in_wonderland.txt problem

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Alice_in_wonderland.txt problem

This post has NOT been accepted by the mailing list yet.
So I'm doing a problem on the Alice_in_wonderland.txt where I have to write a program that reads a piece of text from a file specified by the user, counts the number of occurrences of each word, and writes a sorted list of words and their counts to an output file. The list of words should be sorted based on the counts, so that the most popular words appear at the top. Words with the same counts should be sorted alphabetically.

My code right now is

word_count = {}
file = open ('alice_in_wonderland.txt', 'r')
full_text = file.read().replace('--',' ')
full_text_words = full_text.split()

for words in full_text_words:
        stripped_words = words.strip(".,!?'`\"- ();:")
            word_count[stripped_words] += 1
        except KeyError:
            word_count[stripped_words] = 1

ordered_keys = word_count.keys()
print ("All the words and their frequency in", 'alice in wonderland')
for k in ordered_keys:
    print (k, word_count[k])

The Output here is just all of the words in the document NOT SORTED by amount of occurrence.
I need help sorting this output of words in the Alice_in_wonderland.txt, as well as help asking the user for the input information about the files.

If anyone could give me some guidance you will really be helping me out.

Please and Thank you