Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dictionary, accepting similar words solution

# Import the modules required
import json
from difflib import get_close_matches
  
# Loading data from json file
# in python dictionary
data = json.load(open("dictionary.json"))
  
def translate(w):
    # converts to lower case
    w = w.lower()
  
    if w in data:
        return data[w]
    # for getting close matches of word
    elif len(get_close_matches(w, data.keys())) > 0:             
        yn = input("Did you mean % s instead? Enter Y if yes, or N if no: " % get_close_matches(w, data.keys())[0])
        yn = yn.lower()
        if yn == "y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "n":
            return "The word doesn't exist. Please double check it."
        else:
            return "We didn't understand your entry."
    else:
        return "The word doesn't exist. Please double check it."
  
# Driver code
word = input("Enter word: ")
output = translate(word)
  
if type(output) == list:
    for item in output:
        print(item)
else:
    print(output)
input('Press ENTER to exit')
Comment

PREVIOUS NEXT
Code Example
Python :: ring Using This in the class region as Self 
Python :: function to find the mean of column in dataframe in python 
Python :: found django install path 
Python :: text to ascii art generator python 
Python :: word cloud mape python 
Python :: ring Trace library usage to pass an error 
Python :: Python cut down OS path to certain part 
Python :: python list insert out of range 
Python :: how to write stuff in python 
Python :: python alphabet to number 
Python :: Proper Case django template 
Python :: python covert vtt subtittle to text txt file 
Python :: how to add illegal characters to paths python 
Python :: print(1) in python 
Python :: separate array along axis 
Python :: print single pixel from numpy 
Python :: ROC plot for h2o package 
Python :: '.join(s) 
Python :: mail.send_message flask not working, SSL == 465 
Python :: orm odoo 
Python :: loop only to the 6th element python 
Python :: how to make an instagram report bot python 
Python :: correct code to read csv file in python 
Python :: python geodata visualize 
Python :: how do you change a class variable in python 
Python :: google.api_core.exceptions.ServiceUnavailable: 503 The datastore operation timed out, or the data was temporarily unavailable when using stream 
Python :: hide model field form 
Python :: threading pass keyword args example 
Python :: Python Tkinter Menu Widget Syntax 
Python :: Move Mouse Every Minute Using Python 3 & PyAutoGUI 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =