Challenge

Project 1 English Dictionary App

import json
from difflib import get_close_matches

# function to add to JSON
def write_json(new_data, filename='words.txt'):
with open(filename,'r+') as file:
# First we load existing data into a dict.
file_data = json.load(file)
# Join new_dat3a with file_data
file_data.update(new_data)
# Sets file's current position at offset.
file.seek(0)
# convert back to json.
json.dump(file_data, file, indent = 4)

# load the data of json file
# (here file name is dict.json)
data=json.load(open("words.txt"))

# create a function to get the the meaning
def Get_meaning(word):
if word in data:
# as data will be in the form of dictionary
# and this condition is to check if the
# meaning of the words are more than one
# and the value of each key is in the form of list
if len(data[word])>1:
# print each meaning
for i in data[word]:
print(i)
# else will print the only element in the list
else:
print(data[word][0])
# we will check the input in all forms
elif word.lower() in data:
if len(data[word.lower()])>1:
# print each meaning
for i in data[word.lower()]:
print(i)
# else will print the only element in the list
else:
print(data[word.lower()][0])
elif word.upper() in data:
if len(data[word.upper()])>1:
# print each meaning
for i in data[word.upper()]:
print(i)
# else will print the only element in the list
else:
print(data[word.upper()][0])
elif word.title() in data:
if len(data[word.title()])>1:
# print each meaning
for i in data[word.title()]:
print(i)
# else will print the only element in the list
else:
print(data[word.title()][0])
# now if these three doesnt match with the input
# then we will find the closest match
else:
# get close match will return us the list of all
# close words with the input
close_match=get_close_matches(word,data.keys())
# if there are 1 or more elements
if len(close_match)>0:
# now we will print the meaning of closest words
# that is the first element
decide=input("Enter yes or no to move forward: ")
if decide=="yes":
# print the meaning of the closest word
print(f"The closest word to this is {close_match[0]}")
for i in data[close_match[0]]:
print(i)
else:
print("There is no word related to it")
else:
print("Cant Found this word")

while (True):
print('')
print('Main Menu')
print('\t1. Add a new word')
print('\t2. find the meaning')
print('\t3. update a word')
print('\t4. Exit')
print('')

n = int(input('>>> Enter your choice: \n')) #to take user dissison as per main menu

if n == 1:
print('To add word with its meaning in dictnary \n')
word = input('Write a word : ')
meaning = input('Write meaning of world : ')
class_list = {}
class_list[word] = [meaning]
write_json(class_list)
print('New word added succesfully.')
continue
elif n == 2:
word=input("Enter the word: ")
print('the meaning of',word,'is :',end=' ')
Get_meaning(word)
continue
elif n==3:
print('To update Dictnary ')
a_file = open('words.txt','r')
my_dict=json.load(a_file)
a_file.close()
word = input('Write a word to update meaning : ')
meaning = input('Write the correct mening : ')
my_dict.update({word: [meaning]})
f2 = open('words.txt','w')
json.dump(my_dict,f2)
f2.close()
print("dictnary updated succesfully.")
continue
elif n == 4 or n<1 or n>4:
print('Thank You! for using dictionary ')
break

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.