Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary Issue Fix #1106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 7 additions & 73 deletions jarviscli/plugins/dictionary.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import nltk
from nltk.corpus import wordnet
from plugin import plugin

nltk.data.path.append("jarviscli/data/ntlk")

import requests

@plugin('dictionary')
def dictionary(jarvis, s):
Expand All @@ -16,71 +12,9 @@ def dictionary(jarvis, s):
else:
word = s

syns = wordnet.synsets(word)
if len(syns) == 0:
jarvis.say("Don't recognise that word")
return

synonyms = set()
antonyms = set()

count = 0
for meaning in syns:
count = count + 1
jarvis.say("{:>3}. {}".format(count, meaning.definition()))

for synonym in meaning.lemmas():
if synonym.name() != word:
synonyms.add(synonym.name())
for antonym in synonym.antonyms():
antonyms.add(antonym.name())

jarvis.say('\nSynonyms:\n' + ", ".join(synonyms))
jarvis.say('\nAntonyms:\n' + ", ".join(antonyms))

# detail loop
def input_detail_id():
jarvis.say("")
synlen = len(syns)
detail_id = jarvis.input("Details of meaning (1-{}): ? ".format(synlen))
if detail_id == '':
return None

try:
detail_id = int(detail_id)
except ValueError:
return input_detail_id()

if detail_id <= 0 or detail_id > synlen:
jarvis.say("Choose Value between 1 and {}".format(synlen))
return input_detail_id()

return detail_id

detail_id = input_detail_id()
while detail_id is not None:
meaning = syns[detail_id - 1]

synonyms = [synonym for synonym in meaning.lemma_names()
if synonym != word]
examples = meaning.examples()

antonyms = set()
for synonym in meaning.lemmas():
for antonym in synonym.antonyms():
antonyms.add(antonym.name())

jarvis.say('')
jarvis.say('== {}. =='.format(detail_id))
jarvis.say("Meaning : {}".format(meaning.definition()))
if len(synonyms) > 0:
jarvis.say("Synonyms : {}".format(", ".join(synonyms)))
if len(antonyms) > 0:
jarvis.say("Antonyms : {}".format(", ".join(antonyms)))
if len(examples) > 0:
if len(examples) == 1:
jarvis.say("Examples : {}".format(examples[0]))
else:
jarvis.say("Examples :\n-{}".format("\n- ".join(examples)))

detail_id = input_detail_id()
api_url = 'https://api.api-ninjas.com/v1/dictionary?word={}'.format(word)
response = requests.get(api_url, headers={'X-Api-Key': 'dDcouxccX+Ne1OKZj+rRrw==CW8Mg7Tvlk3u8omW'})
if response.status_code == requests.codes.ok:
print(response.text)
else:
print("Error:", response.status_code, response.text)