Saturday, January 25, 2020

Project #1 - Dictionary Program

Here is the first project for my portfolio.

This is a program that functions as a dictionary.

When a word is typed into the prompt the program will return the definition.

The program is pulling data from a json file.

If the word is typed incorrectly the program will find a closely spelled word match and ask you if it is the word you are looking for. It will ask yes or no. If you type "Y" it will return the definition of the word. If you type "N" it will return a string that says the word does not exist and to double check it.

There are some additional features added, like the program will know to look for words with capitals even if the entered word was not capitalized.

import json
from difflib import get_close_matches

# Loading Data into a Python data type.
data = json.load(open("data.json"))


def translate(w):
    w = w.lower()
    if w in data:
        return data[w]
    elif w.title() in data:  # If user entered "texas" this will check for "Texas" as well.        return data[w.title()]
    elif w.upper() in data:  # In case user enters words like USA or NATO        return data[w.upper()]
    elif len(get_close_matches(w, data.keys())) > 0:
        yn = input("Did you mean %s? Enter Y if yes and N if no: " % get_close_matches(w, data.keys())[0])
        if yn == "Y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "N":
            return "The word doesn't exist. Please double check it."        
        elif yn == "n":
            return "The word doesn't exist. Please double check it."        
        else:
            return "We didn't understand your entry."    
    else:
        return "That word doesn't exist. Please check your spelling."


# User input.
word = input("Enter word: ")
output = translate(word)
if type (output) == list:
    for item in output:
        print(item)

else:
    print(output)

No comments:

Post a Comment

Project #8 - Stock Tracking Graph

For my 8th project, I made a program that pulls stock data from Yahoo Finance and displays it in a graph. For this example, I chose GOOG whi...