#!/usr/bin/python

# uncontract.py by Nadeem Abdul Hamid
# based on 'lamonizer.py c. 2005 Henry Finucane'
#
# This program demonstrates Python dictionaries, file i/o,
# command-line arguments, string operations, if/elif, ..<..<..,
# 'and' logical operator, exception handling,
# multi-line strings using """

"""
This program will expand contractions in a text file.
"""

import sys


# a dictionary of contractions:
contractions = {"I've" : "I have",\
		"I'll" : "I will",\
		"I'm" : "I am",\
		"won't" : "will not",\
		"couldn't" : "could not",\
		"shouldn't" : "should not",\
		"can't" : "cannot",\
		"they've" : "they have",\
		"they're" : "they are",\
		"you're" : "you are",\
		"don't" : "do not",\
		"didn't" : "did not",\
		"doesn't" : "does not",\
		"hasn't" : "has not",\
		"hadn't" : "had not",\
		"isn't" : "is not",\
		"weren't" : "were not",\
		"I've" : "I have",\
		"I'd" : "I would",\
		"aren't" : "are not",\
		"haven't" : "have not" }


def uncontract( string, contr_list=contractions ):
    for c in contr_list:
        if c in string:
            string = string.replace( c, contr_list[ c ] );
    return string


def main(argv=sys.argv):
    writeBack = False;   # write result back to input file -- careful!
    args = len(argv);
    
    if 1 < args <= 3:    # need 1 or 2 command-line args.
        if args==3 and argv[1] == '-w':
            writeBack = True
            target = argv[2]
        elif args==2:
            target = argv[1]

        try:
            f = file( target, 'r' )
            stuff = f.read()
            f.close()
            
            if writeBack:
                f = file( target, 'w' )
                f.write( uncontract( string=stuff ) )
                f.close()
            else:
                print uncontract( stuff )
        except:
            print "Sorry, file", target, "does not exist."

    else:
### print usage message if wrong arguments
        print \
"""
  uncontract.py, 2006 Nadeem Abdul Hamid
  Usage:
         uncontract.py [-w] targetFile
         Where <targetFile> is the plain  text file from which contractions
         should be removed. If the -w flag is specified, the result will be
         written back to the same file.  By default,  the result is written
         to standard output.
"""
### end usage message



if __name__ == '__main__':
    main();
