Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'ping',
'quote',
'request',
'synonym',
'thanks',
'time',
'url',
Expand Down
44 changes: 44 additions & 0 deletions modules/src/synonym.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import requests
import requests_cache
from templates.text import TextTemplate
from templates.button import ButtonTemplate

def process(input, entities):
output = {}
try:
word = entities['synonym'][0]['value']

with requests_cache.enabled('synonym_cache', backend='sqlite', expire_after=86400):
r = requests.get('https://api.datamuse.com/words', params={
'rel_syn': word,
'max': 5
})
data = r.json()

if data:
synonyms = ', '.join([item['word'] for item in data])

template = TextTemplate()
template.set_text('Synonyms for "{}": {}'.format(word, synonyms))
text = template.get_text()

template = ButtonTemplate(text)
template.add_web_url('More Synonyms', 'https://www.thesaurus.com/browse/{}'.format(word))

output['input'] = input
output['output'] = template.get_message()
output['success'] = True
else:
raise ValueError("No synonyms found")

except Exception as e:
error_message = 'I couldn\'t find any synonyms for the word "{}".'.format(word)
error_message += '\nPlease try another word, like:'
error_message += '\n - happy'
error_message += '\n - sad'
error_message += '\n - fast'
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False

return output

30 changes: 30 additions & 0 deletions modules/tests/test_synonym.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import modules

def test_synonym():
# Tests for correctly identifying queries that should return synonyms
assert ('synonym' == modules.process_query('synonyms for happy')[0])
assert ('synonym' == modules.process_query('give me synonyms of joy')[0])
assert ('synonym' == modules.process_query('other words for sad')[0])

# Tests to ensure non-synonym queries are not mistaken
assert ('synonym' != modules.process_query('define happiness')[0])
assert ('synonym' != modules.process_query('something random')[0])

# Specific test cases to check if the synonyms returned are accurate
# These tests check for specific synonyms being included in the results
intent, synonyms = modules.process_query('synonyms for love')
assert ('synonym' == intent)
assert 'affection' in synonyms
assert 'devotion' in synonyms

intent, synonyms = modules.process_query('synonyms for strong')
assert ('synonym' == intent)
assert 'powerful' in synonyms
assert 'sturdy' in synonyms

# Test for a word with no synonyms
intent, synonyms = modules.process_query('synonyms for qwertyuiop')
assert ('synonym' == intent)
assert synonyms == "No synonyms found."