Skip to content

Commit

Permalink
Major rewrite - now using NLP for query processing
Browse files Browse the repository at this point in the history
  • Loading branch information
swapagarwal committed Apr 20, 2016
1 parent a13bd33 commit 9c1f53d
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 48 deletions.
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ACCESS_TOKEN = '<ACCESS_TOKEN>'
VERIFY_TOKEN = '<VERIFY_TOKEN>'
WIT_AI_SERVER_ACCESS_TOKEN = '<WIT_AI_SERVER_ACCESS_TOKEN>'
36 changes: 30 additions & 6 deletions modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import config
import os
import requests
import sys
from src import *

WIT_AI_SERVER_ACCESS_TOKEN = os.environ.get('WIT_AI_SERVER_ACCESS_TOKEN', config.WIT_AI_SERVER_ACCESS_TOKEN)

def process_query(input):
try:
r = requests.get('https://api.wit.ai/message?v=20160420&q=' + input, headers={
'Authorization': 'Bearer %s' % WIT_AI_SERVER_ACCESS_TOKEN
})
data = r.json()
intent = data['outcomes'][0]['intent']
entities = data['outcomes'][0]['entities']
confidence = data['outcomes'][0]['confidence']
if intent in src.__all__ and confidence > 0.5:
return intent, entities
else:
return None, {}
except:
return None, {}

def search(input):
for module in src.__all__:
if sys.modules['modules.src.' + module].match(input):
data = sys.modules['modules.src.' + module].process(input)
if data['success']:
return data['output']
return 'I\'m still learning. Check back later!'
intent, entities = process_query(input)
if intent is not None:
data = sys.modules['modules.src.' + intent].process(input, entities)
if data['success']:
return data['output']
else:
return 'Something didn\'t work as expected! I\'ll report this to my master.'
else:
return 'I\'m still learning. Check back later!'
2 changes: 1 addition & 1 deletion modules/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = [
'movie',
'dictionary',
# 'dictionary',
'joke',
'hello'
]
14 changes: 1 addition & 13 deletions modules/src/hello.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import re
import random

def match(input):
greetings = [
'hi',
'hello',
'jarvis',
'hi jarvis',
'hello jarvis',
'are you there'
]
input = re.sub(r'[^a-zA-Z\d\s]', '', input).lower()
return input in greetings

def process(input):
def process(input, entities=None):
greetings = [
'Welcome home, sir...',
'All wrapped up here, sir. Will there be anything else?',
Expand Down
5 changes: 1 addition & 4 deletions modules/src/joke.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import re
import requests

def match(input):
return bool(re.match(r'^.*jokes?\W*$', input))

def process(input):
def process(input, entities=None):
output = {}
try:
r = requests.get('https://api.chucknorris.io/jokes/random')
Expand Down
7 changes: 2 additions & 5 deletions modules/src/movie.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import re
import requests

def match(input):
return bool(re.match(r'^.*\s+movie$', input))

def process(input):
def process(input, entities):
output = {}
movie = re.match(r'^(?P<movie>.*)\s+movie$', input).group('movie')
try:
movie = entities['movie'][0]['value']
r = requests.get('http://www.omdbapi.com/?t=' + movie + '&plot=short&r=json')
data = r.json()
output['input'] = input
Expand Down
13 changes: 6 additions & 7 deletions modules/tests/test_hello.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from modules.src import hello
import modules

def test_joke():
assert(hello.match('Are you there?') == True)
assert(hello.match('Hi, Jarvis!') == True)
assert(hello.match('hello') == True)
assert(hello.match('jarvis jarvis') == False)
assert(hello.match('something random') == False)
def test_hello():
assert('hello' == modules.process_query('hello')[0])
assert('hello' == modules.process_query('Hi, Jarvis!')[0])
assert('hello' == modules.process_query('Are you there?')[0])
assert('hello' != modules.process_query('something random')[0])
11 changes: 5 additions & 6 deletions modules/tests/test_joke.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from modules.src import joke
import modules

def test_joke():
assert(joke.match('tell me a joke') == True)
assert(joke.match('random jokes') == True)
assert(joke.match('Do you know a joke?') == True)
assert(joke.match('joke is the') == False)
assert(joke.match('something random') == False)
assert('joke' == modules.process_query('tell me a joke')[0])
assert('joke' == modules.process_query('Do you know a joke?')[0])
assert('joke' == modules.process_query('random jokes')[0])
assert('joke' != modules.process_query('something random')[0])
11 changes: 5 additions & 6 deletions modules/tests/test_movie.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from modules.src import movie
import modules

def test_movie():
assert(movie.match('happyness movie') == True)
assert(movie.match('batman movie') == True)
assert(movie.match('happyness') == False)
assert(movie.match('movie') == False)
assert(movie.match('something random') == False)
assert('movie' == modules.process_query('batman movie')[0])
assert('movie' == modules.process_query('iron man 2 movie plot')[0])
assert('movie' == modules.process_query('What is the rating of happyness movie?')[0])
assert('movie' != modules.process_query('something random')[0])

0 comments on commit 9c1f53d

Please sign in to comment.