1: #This is the main function
2: def mainFunction():
3: print("Please enter the message (no punctuations please):")
4: message = input(">") #will asign message with the user's message
5: fileWrite(message) #will write the to a file
6: fileCheck("TextFile2.txt") #will check if the file exists or not
7: messageWords = seperateWordsList("TextFile1.txt") #will create a list of all words in the message
8: AllWords, AllDefs = dictionaryList("TextFile2.txt") #will create two lists - one containing the words, the other containing the definitions
9: finalWords = compare(messageWords, AllWords, AllDefs) # the final list of words for the message
10: printMessage(finalWords) #will print the message
11:
12: #This will write the message to a file
13: def fileWrite(message):
14: fileObj = open("TextFile1.txt","w") #creates the file object with the name "TextFile1.txt"
15: fileObj.write(message) #writes a message to the file
16: fileObj.close() #closes the file objects
17:
18: #will check if the file exists or not
19: def fileCheck(fileName):
20: try:
21: fileObj = open(fileName) #will try to open the file
22: except IOError: #will handle the exception
23: print("The file could not be opened.")
24: print("Either the file does not exist, or you have entered the wrong name.")
25: os.system("pause")
26: os.system("cls")
27: mainFunction()
28:
29: #will seperate words and return a list of words
30: def seperateWordsList(fileName):
31: fileObj = open(fileName)
32: fileContents = fileObj.read()
33: AllWords = fileContents.split() #will split the entire file contents into words
34: return AllWords
35:
36: #This function is to return two lists - one containing all the short forms, the other containing the definitions
37: def dictionaryList(fileName):
38: fileObj = open(fileName)
39: AllWords = []
40: AllDefs = []
41: for line in iter(fileObj): #This for loop will read the file line by line
42: words = line.split() #This will split the sentence into a list of words
43: AllWords.append(words[0]) #appends the short form to this list
44: s = ""
45: for x in range(2,len(words)):
46: s = s + words[x] + " "
47: AllDefs.append(s[0:len(s) - 1]) #appends the definition to this list
48: return (AllWords, AllDefs)
49:
50: #this function will compare message words and those in dictionary
51: def compare(messageWords, AllWords, AllDefs):
52: for x in range(0, len(messageWords)):
53: word = messageWords[x]
54: for y in range(0, len(AllWords)):
55: if(word.upper() == AllWords[y]):
56: messageWords[x] = AllDefs[y] #This will replace the word with the dictonary definition if it's true
57:return (messageWords)
58:
59: #will print the message based on the list finalWords
60: def printMessage(finalWords):
61: message = ""
62:for word in finalWords:
63: message = message + " " + word
64: print(message[1:]) #will remove the inital space
65:
66: mainFunction()