Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is hard to explain but i need to create a \'dictionary\' that is \'scary\'.

ID: 3644276 • Letter: T

Question

This is hard to explain but i need to create a 'dictionary' that is 'scary'. So I have to write a function sdict() that will read an electronic version of a scary book,(so a file has to be read from a word file of a scary book) and it will pick up all the unique words that are in it, and along with that, it will write them in ALPHABETICAL order into a new file called dictionary.txt. Also, can't include the 1 and 2 letter words because there not 'considered scary'. Also, punctuatiion and numbers that appear need to be elimnated and digits as well, and those need to be replaced with blanks or empty strings.


You would like to create a scary dictionary, but you have a hard time finding the thousands of words that should go into such a dictionary. Your idea is to write a function scaryDict() that reads in an electronic version of a scary book, say Frankenstein by Mary W. Shelley, picks up all the unique words in it, and writes them in alphabetical order in a new file called dictionary.txt. You won't include the one- and two-letter words since not many of them are scary. Note that punctuation and numbers that appear in the text makes this task more complex, so you will want to eliminate all possible punctuation and digits in the file, replacing them with blanks or empty strings.

Explanation / Answer

import operator # for sorted(), to sort dictionary
import string    # for string.letters

def is_scary(w):
    return len(w) > 2

def add_to_dict(word, dictionary):
    dictionary[word] = dictionary[word] + 1 if word in dictionary else 1

def scaryDict(path):
    # open file to read
    try:
        f = open(path, 'r')
    except:
        print 'Cannot open {0}'.format(path)
        return
    # read file
    clean_content = ''
    words = []
    scary_dict = {}
    while True:
        letter = f.read(1)
        if not letter:
            break
        elif letter not in string.letters:
            clean_content += ' '
        else:
            clean_content += letter
    f.close()
    words = clean_content.split(' ')
    for word in words:
        if word and is_scary(word):
            add_to_dict(word.lower(), scary_dict)
    # sort by word
    p = sorted(scary_dict.iteritems(), key = operator.itemgetter(0))
    # open file to write
    try:
        f = open('dictionary.txt', 'w')
    except:
        print 'Cannot open dictionary.txt'
        return
    # write to file
    for word_tuple in p:
        f.write('{0}: {1} '.format(word_tuple[0], word_tuple[1]))
    f.close()
    print 'Done writing dictionary.txt'


path = raw_input('Enter file path: ')
scaryDict(path)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote