labyrinth-cs-proj/wordle/dictionary.py

94 lines
3.2 KiB
Python
Raw Normal View History

2022-11-30 12:57:18 +00:00
import pickle
2022-11-25 20:24:29 +00:00
import requests
2022-11-30 12:57:18 +00:00
with open("credentials.pickle", "rb") as f:
try:
while True:
d = pickle.load(f)
if d["credtype"] == "oxfordapi":
app_id = d["app_id"]
app_key = d["app_key"]
except EOFError:
pass
2022-11-25 20:24:29 +00:00
language = "en-us"
2022-11-30 12:57:18 +00:00
2022-11-25 20:24:29 +00:00
def defnsyn(w):
2022-11-30 15:57:28 +00:00
"""Returns definition and synonym of said word"""
2022-11-30 12:57:18 +00:00
url = (
r"https://od-api.oxforddictionaries.com:443/api/v2/entries/"
+ language
+ "/"
+ w.lower()
)
r = requests.get(url, headers={"app_id": app_id, "app_key": app_key})
2022-11-25 20:24:29 +00:00
if r.status_code != 200:
return None, None
res = r.json()
s1 = res["results"][0]["lexicalEntries"]
lexicalCategories = []
synonyms = []
defn = ""
2022-11-30 12:57:18 +00:00
try:
if len(s1) > 1:
for i in range(len(s1)):
lexicalCategories.append(s1[i]["lexicalCategory"]["id"])
if "verb" in lexicalCategories:
baseindex = s1[lexicalCategories.index("verb")]["entries"][0]["senses"][
0
]
defn = baseindex["shortDefinitions"][0]
if "synonyms" in baseindex:
no = (
2
if len(baseindex["synonyms"]) > 3
else len(baseindex["synonyms"])
)
while no:
synonyms.append(baseindex["synonyms"][no]["text"])
no -= 1
synonyms.reverse()
elif "noun" in lexicalCategories:
baseindex = s1[lexicalCategories.index("noun")]["entries"][0]["senses"][
0
]
defn = baseindex["shortDefinitions"][0]
if "synonyms" in baseindex:
no = (
3
if len(baseindex["synonyms"]) > 3
else len(baseindex["synonyms"])
)
while no:
synonyms.append(baseindex["synonyms"][no]["text"])
no -= 1
synonyms.reverse()
else:
baseindex = s1[0]["entries"][0]["senses"][0]
defn = baseindex["shortDefinitions"][0]
if "synonyms" in baseindex:
no = (
3
if len(baseindex["synonyms"]) > 3
else len(baseindex["synonyms"])
)
while no:
synonyms.append(baseindex["synonyms"][no]["text"])
no -= 1
synonyms.reverse()
2022-11-25 20:24:29 +00:00
else:
2022-11-30 12:57:18 +00:00
baseindex = s1[0]["entries"][0]["senses"][0]
defn = baseindex["shortDefinitions"][0]
2022-11-25 20:24:29 +00:00
if "synonyms" in baseindex:
no = 3 if len(baseindex["synonyms"]) > 3 else len(baseindex["synonyms"])
while no:
synonyms.append(baseindex["synonyms"][no]["text"])
no -= 1
synonyms.reverse()
2022-11-30 12:57:18 +00:00
except:
print("err")
defn = synonyms = ""
2022-11-25 20:24:29 +00:00
return defn, synonyms