Created
July 29, 2020 15:04
-
-
Save Enoch2090/94c4ff1dab673e7416bad5287c4547b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# enoch2090.me | |
# Register for your api key at https://www.wordsapi.com | |
import requests | |
headers = { | |
'x-rapidapi-host': "wordsapiv1.p.rapidapi.com", | |
'x-rapidapi-key': "YOUR API KEY" | |
} | |
url = "https://wordsapiv1.p.rapidapi.com/words/" | |
def print_res(words): | |
if len(words) == 0: | |
print("No result found.") | |
else: | |
print("Found", len(words), "relevant words:\n") | |
out = '' | |
for word in words: | |
out += word + " " | |
print(out) | |
def find_rhyme(rhyme): | |
response = requests.request("GET", url+rhyme+"/rhymes", headers=headers) | |
words = response.json()['rhymes']['all'] | |
return words | |
def find_syn(synon): | |
response = requests.request("GET", url+synon+"/synonyms", headers=headers) | |
words = response.json()['synonyms'] | |
return words | |
def find_hastypes(typeword): | |
response = requests.request( | |
"GET", url+typeword+"/hasTypes", headers=headers) | |
words = response.json()['hasTypes'] | |
return words | |
while 1: | |
cmd = input('Enter command:\n') | |
cmds = cmd.split(' ') | |
cmd_set = list(map(int, ['syn' in cmds, 'rhm' in cmds, 'exit' in cmds])) | |
if cmd_set[2] == 1: | |
break | |
elif cmd_set == [1, 0, 0]: # Grammar: syn SYNWORD | |
words = set(find_syn(cmds[1])) | |
try: | |
words.remove(str(cmds[1])) | |
except: | |
pass | |
words = list(words) | |
print_res(words) | |
elif cmd_set == [0, 1, 0]: # Grammar: rhm RHMWORD LEVEL | |
words = set(find_rhyme(cmds[1])) | |
try: | |
words.remove(str(cmds[1])) | |
except: | |
pass | |
words = list(words) | |
print_res(words) | |
elif cmd_set == [1, 1, 0]: # Grammar: syn SYNWORD rhm RHMWORD | |
words_syn = find_syn(cmds[1]) | |
words_rhyme = find_rhyme(cmds[3]) | |
words = set(words_syn).intersection( | |
set(words_rhyme)) | |
try: | |
words.remove(str(cmds[1])) | |
words.remove(str(cmds[3])) | |
except: | |
pass | |
words = list(words) | |
print_res(words) | |
else: | |
print("Invalid command!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment