# Takes a line s and replaces all punctuation marks given
# in the list punctuationMarkes by blanks; returns the modified list
def filterOutPunctuation(punctuationMarks, s):
	for mark in punctuationMarks:
		s = s.replace(mark, " ")
	return s


# Returns the list obtained by deleting all duplicates
# from L
def removeDuplicates(L):
	L.sort()
	newL = []
	for i in range(len(L)-1):
		if L[i] != L[i+1]:
			newL.append(L[i])

	newL.append(L[len(L)-1]) # The last word in L has to be explicitly appended

	return newL
		
# Makes all the words in the wordList have lower case
def makeLower(wordList):
	for i in range(len(wordList)):
		wordList[i] = wordList[i].lower()

#Main program
fileNames = ["alice.txt", "carol.txt", "hyde.txt", "war.txt", "gulliver.txt", "treasure.txt"]
L = []
punctuationMarks = map(chr, range(0, ord("A")) + range(ord("Z")+1, ord("a")) + range(ord("z")+1, 127))

# Loop that processes all 6 input text files
for name in fileNames:
	f = open(name)	
	bigString = f.read()	# read the entire text file in one go
	bigString = filterOutPunctuation(punctuationMarks, bigString)	
	wordList = bigString.split()
	makeLower(wordList)	
	L.extend(wordList)
	f.close()

L = removeDuplicates(L)

#Block of code that produces output
f = open("dictionary.txt", "w")
for word in L:
	f.write(word+"\n")
f.close()

