Question
I'm having trouble trying to get my desired output to be what I have below here. I also need help changing my while true for
I'm having trouble trying to get my desired output to be what I have below here. I also need help changing my while true for my loop below. I have my textfile below and my code. Can someone help me?
#OUPUT I NEED.
45 1 fun 6 is 2 is45 1 isn't 3 it 3 not 4 or 7 sit 1 this 12 was 2 wasn't 1
#THIS IS MY TEXT FILE
This, is! fun? isn't THIS (This) This, is! fun? Isn'T THIS This. This, is45! fun? 45, isn't THIS This. Was it this or fun OR, !NOT? Was it this or FuN OR, "NOT? Wasn't IT this sIT or fun OR, NOT? Not Or
#THIS IS MY PROGRAM.
from os import path # file_exist function to check file exist or not def file_exist(filename): if path.exists(filename): return True else: return False
#define punctuation def strip_punctuation(data): #the input from the file runs trough takes the puntuation off. # and then returns the words. unwanted_chars = '''!()-[]{};:'"\,<>./?@#$%^&*_~ ''' words = "" for char in data : if char not in unwanted_chars: words += char return words
#splits the sentences into invidual words. def prepareDictionary(words): words = words.lower() words_list = list(map(str,words.split(" "))) words_dict ={} #Count number of times each word comes up in list of words (in dictionary) for i in words_list: if i not in words_dict: words_dict[i] = 0 words_dict[i] += 1 return words_dict
#Used to add the word to the dictinary. def Add_word(word,wordsDictionary): # add user word in dictionary if word.lower() not in wordsDictionary: wordsDictionary[word] = 0 wordsDictionary[word] += 1 print(f"{word} is added in Dictionary")
#Deltes the word from the dictinarys, using pop. def Delete_word(word, wordsDictionary): if word.lower() in wordsDictionary: wordsDictionary.pop(word) print(f"{word} is deleted from Dictionary.") else: print(f"{word} not exist in Dictionary")
#Checks to see if the word is in the dictionary def Check_word(word,wordsDictionary): if word.lower() in wordsDictionary: print(f"{word} is already in Dictionary") else: print(f"{word} is not in Dictionary")
#display list of words and frequency def Display(wordsDictionary): print("{:<15} {:<15}".format('WORDS', 'FREQUENCY')) for i in sorted(wordsDictionary.keys()): print("{:<15} {:<15}".format(i, wordsDictionary[i]))
def main(): file = input("Enter the file :- ") if file_exist(file) == True: print(" "+f"File {file} exists.") with open(file, 'r+') as f: # load the file informtaion to be read file_content = f.read() # call strip_punctuation function that removes the unwanted #characters in string words_string = strip_punctuation(file_content) # return the dictionary of words and its frequency wordsDictionary = prepareDictionary(words_string) # Menu for perform Operation on Dictionary while True: print(" Please select the Operation from Menu " "1.Add word " "2.Check word " "3.Delete word " "4.Print word " "5.Quit " "6.") ch = int(input("Enter your choice: ")) if (ch == 1): # take word from user and add it to in Dictionary new_word = input("Enter your word which you want to add : ").lower() Add_word(new_word, wordsDictionary) elif (ch == 2): # take word from user and check it into Dictionary new_word = input("Enter your word which you want to check : ").lower() Check_word(new_word, wordsDictionary) elif (ch == 3): # take word from user and delete it from dictionary new_word = input("Enter your word which you want to Delete: ").lower() Delete_word(new_word, wordsDictionary) elif (ch == 4): # print the dictionary Display(wordsDictionary) elif (ch == 5): exit() else: print("Please enter valid choice") else: print(" "+f"File {file} is not exist, please enter valid file name.") main() # call main method for continue calling main until file is not valid if __name__ == '__main__': #call main method main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started