-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
44 lines (31 loc) · 1.14 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json
from difflib import get_close_matches
# Open file: convert object to dictionery #
with open("data.json") as data_file:
data = json.load(data_file)
# Dictionery has words with their definitions #
# Translate: #
# Input: string #
# Output: definition #
def translate(word):
if type(word) != str:
return "Please give a word."
if not word.islower():
word = word.lower()
# Word exists in dictionery #
if data.has_key(word):
return data[word]
# Check if user gave a word that is similar to an existing word #
else:
matches = get_close_matches(word, data.keys(),cutoff= 0.8)
if matches:
answer = raw_input("Did you mean %s instead? Enter Y for yes or N for no: "% matches[0])
if answer == "Y" or answer == "y":
return data[matches[0]]
elif answer == "N" or answer == "n":
return "Sorry the word doesn't exists."
else:
return "Press Y or N. Please try again."
# No matches #
else:
return "Sorry the word doesn't exists."