#!/usr/bin/env python
# Calvin Smith
# Craft assignment 3, part 3
# 2/09/03

"""Read text and check it against a dictionary, printing
the misspelled words and how many times they are misspelled.
Use the following format to run the script: python spelcheck.py < input.txt
"""

import sys

dictionary = []

def loaddictionary(dictionaryfilename):
    """Set the dictionary to be used."""
    global dictionary
    f = open(dictionaryfilename, 'r')
    dictionarylines = f.readlines()
    dictionary = [x.replace('\n', '') for x in dictionarylines]
    f.close()
    
def addword(word):
    """Add a word to the dictionary."""
    pass

def deleteword(word):
    """Delete a word from the dictionary."""
    pass

def checktext(text):
    """Spellcheck a chunk of text.

        Returns a dictionary of misspelled words, with the key being the word and
        the value being how many times it is misspelled. If no errors, an empty list
        is returned.
    """
    mispeled = {}
    text = text.replace('\n', ' ')
    text = removenonwordchars(text)
    words = text.split()
    for word in words:
        if checkword(word) != None:
            if mispeled.get(word, None):
                mispeled[word] = mispeled[word] + 1
            else:
                mispeled[word] = 1
    return mispeled

def removenonwordchars(text):
    """Remove non-word characters such as numbers and punctuation from text."""
    for symbol in '()[].,-?"&$#@!%^*_=+/\\;:0123456789':
        text = text.replace(symbol, '')
    return text   

def checkword(word):
    """Spellcheck an individual word.

        Return the mispelled word or None.
    """
    global dictionary
    isword = 0
    try:
        dictionary.index(word)
        return None
    except ValueError:
        try:
            dictionary.index(word.lower())
            return None
        except ValueError:
            return word


def main(text):
    loaddictionary('dict.txt')
    mispeled = checktext(text)
    words = mispeled.keys()
    words.sort()
    for word in words:
        print word, '(' + str(mispeled[word]) + ')'

if __name__ == "__main__":
    main(sys.stdin.read())