#Programmer: Sriram Pemmaraju #Date: March 13, 2012 #Version 4 #---------------------------------------------------------------------------- # Takes a line s and replaces all punctuation marks given # in the list punctuationMarks by blanks; returns the modified list def filterOutPunctuation(punctuationMarks, s): for mark in punctuationMarks: s = s.replace(mark, " ") return s #---------------------------------------------------------------------------- # Turns the lower method into a function def toLower(s): return s.lower() #---------------------------------------------------------------------------- # Turns all the words in a list to lower case def makeListLower(wordList): return map(toLower, wordList) #---------------------------------------------------------------------------- # Main program begins here # Open the file "War and Peace" fin = open("smallWar.txt", "r") wordList = [] # List of all non-letter characters, We will replace these characters in each line by blanks punctuationMarks = map(chr, range(0, ord("A")) + range(ord("Z")+1, ord("a")) + range(ord("z")+1, 127)) # Loop that processes each line of the file for line in fin: newLine = filterOutPunctuation(punctuationMarks, line) wordList = wordList + makeListLower(newLine.split()) #Close the input file fin.close() #Block of code that produces output fout = open("dictionary4.txt", "w") wordList.sort() previousWord = "" # keeps track of the word most recently output for word in wordList: # only print out new words if word != previousWord: fout.write(word+"\n") previousWord = word fout.close()