I'm currently building an interactive dictionary. I'm using a sample JSON file (i.e. data.json) as a template. But that's not it. My aim is to build an app where I can store the knowledge that I acquire during my learning process.
And I'd like you to take a look at my code to see if there are areas where I can improve the performance of my program.
Code
import json
from difflib import get_close_matches
data = json.load(open('data.json', 'r'))
def newdict(word, definition):
data.update({word: [definition]})
with open('data.json', 'w') as file:
json.dump(data, file)
def tr(word):
gcm = get_close_matches(word, data, 1, 0.8)
if word in data:
print('· ' + word + ':')
for index, definition in enumerate(data[word]):
print(str(index+1) + '.', definition)
elif word.title() in data:
print('· ' + word.title() + ':')
for index, definition in enumerate(data[word.title()]):
print(str(index + 1) + '.', definition)
elif word.upper() in data:
print('· ' + word.upper() + ':')
for index, definition in enumerate(data[word.upper()]):
print(str(index + 1) + '.', definition)
elif len(gcm) > 0:
print('· ' + gcm[0].capitalize() + ':')
for index, definition in enumerate(data[gcm[0]]):
print(str(index+1) + '.', definition)
else:
print("'%s' definition isn't available at the moment." % word.capitalize())
x = input('Would you like to add "%s" to the dictionary? ' % word.capitalize())
x = x.lower()
if x == 'yes':
y = input('Type in the meaning of %s: ' % word)
newdict(word, y)
else:
print('Ok, as you wish!')
w = input('Enter a word: ')
w = w.lower()
while w != '0':
tr(w)
w = input('\nEnter a word: ')
w = w.lower()
Is there room for improvement?