diff --git a/modules/src/__init__.py b/modules/src/__init__.py index bee0ffa9..82eb64d1 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -18,6 +18,7 @@ 'ping', 'quote', 'request', + 'synonym', 'thanks', 'time', 'url', diff --git a/modules/src/synonym.py b/modules/src/synonym.py new file mode 100644 index 00000000..74a25617 --- /dev/null +++ b/modules/src/synonym.py @@ -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 + diff --git a/modules/tests/test_synonym.py b/modules/tests/test_synonym.py new file mode 100644 index 00000000..238fd455 --- /dev/null +++ b/modules/tests/test_synonym.py @@ -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." + +