From 0e3057ecb1199de232a0f9819830cdca12863735 Mon Sep 17 00:00:00 2001 From: Anshumaan Parashar Date: Thu, 15 Jun 2017 01:24:52 +0530 Subject: [PATCH 1/2] Add Urban Dictionary Feature --- modules/src/__init__.py | 1 + modules/src/urban.py | 42 +++++++++++++++++++++++++++++++++++++ modules/tests/test_urban.py | 7 +++++++ 3 files changed, 50 insertions(+) create mode 100644 modules/src/urban.py create mode 100644 modules/tests/test_urban.py diff --git a/modules/src/__init__.py b/modules/src/__init__.py index bf004743..0ca9f5a4 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -18,6 +18,7 @@ 'quote', 'request', 'time', + 'urban', 'url', 'video', 'weather', diff --git a/modules/src/urban.py b/modules/src/urban.py new file mode 100644 index 00000000..6a722119 --- /dev/null +++ b/modules/src/urban.py @@ -0,0 +1,42 @@ +import requests as rq +from bs4 import BeautifulSoup as bs +import re +import urllib + +from templates.text import TextTemplate + +def process(input, entities): + output = {} + try: + orig_phrase = entities['phrase'][0]['value'] + phrase = urllib.quotes_plus(orig_phrase) + page = rq.get("http://www.urbandictionary.com/define.php?term="+phrase) + soup = bs(page.content, 'html.parser') + meaning = soup.find('div', class_='meaning').text + list_aux = meaning.split('.') + if list_aux[0] == "\nThere aren't any definitions for " + orig_phrase +" + yet": + meaning = "There aren't any definitions for "+orig_phrase+" yet on + Urban Dictionary." + else: + meaning = re.sub(''', '\'', meaning) + meaning = meaning[:min(len(meaning),300)] + meaning += "....\n" + meaning += "See the meaning at Urban Dictionary :- + http://www.urbandictionary.com/define.php?term=" + phrase + output['input'] = input + output['output'] = TextTemplate('Meaning of ' + + orig_phrase + + 'from Urban Dictionary:\n' + meaning).get_message() + output['success'] = True + + except: + error_message = 'I couldn\'t find the meaning of that phrase.' + error_message += '\nPlease ask me something else, like:' + error_message += '\nurban lol' + error_message += '\nlol urban' + error_message += '\nslang lol' + output['error_msg'] = TextTemplate(error_message).get_message() + output['success'] = False + return output + diff --git a/modules/tests/test_urban.py b/modules/tests/test_urban.py new file mode 100644 index 00000000..c0ecabc4 --- /dev/null +++ b/modules/tests/test_urban.py @@ -0,0 +1,7 @@ +import modules + +def test_urban(): + assert('urban' == modules.process_query('urban lol')[0]) + assert('urban' == modules.process_query('lol urban')[0]) + assert('urban' == modules.process_query('slang lol')[0]) + assert('urban' != modules.process_query('something random')[0]) From ab115f98d66baf7deba8e99ad983390ac8544db7 Mon Sep 17 00:00:00 2001 From: Anshumaan Parashar Date: Fri, 16 Jun 2017 20:19:15 +0530 Subject: [PATCH 2/2] Added urbandictionary sample query --- modules/src/urban.py | 1 + modules/tests/test_urban.py | 1 + 2 files changed, 2 insertions(+) diff --git a/modules/src/urban.py b/modules/src/urban.py index 6a722119..2e0ea143 100644 --- a/modules/src/urban.py +++ b/modules/src/urban.py @@ -35,6 +35,7 @@ def process(input, entities): error_message += '\nPlease ask me something else, like:' error_message += '\nurban lol' error_message += '\nlol urban' + error_message += '\nurbandictionary lol' error_message += '\nslang lol' output['error_msg'] = TextTemplate(error_message).get_message() output['success'] = False diff --git a/modules/tests/test_urban.py b/modules/tests/test_urban.py index c0ecabc4..9a03d556 100644 --- a/modules/tests/test_urban.py +++ b/modules/tests/test_urban.py @@ -4,4 +4,5 @@ def test_urban(): assert('urban' == modules.process_query('urban lol')[0]) assert('urban' == modules.process_query('lol urban')[0]) assert('urban' == modules.process_query('slang lol')[0]) + assert('urban' == modules.process_query('urbandictionary lol')[0]) assert('urban' != modules.process_query('something random')[0])