Skip to content

Commit b14a52b

Browse files
committed
Add Wikipedia API for failover
1 parent dc63b93 commit b14a52b

File tree

8 files changed

+44
-32
lines changed

8 files changed

+44
-32
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ FROM python:3
44

55
ADD . /
66

7-
RUN pip install slackclient rasa_nlu scipy scikit-learn sklearn-crfsuite numpy spacy wolframalpha
7+
RUN pip install slackclient rasa_nlu scipy scikit-learn sklearn-crfsuite numpy spacy wolframalpha wikipedia
88
RUN python -m spacy download en
99

1010
CMD [ "python", "./bot.py" ]

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
### Overview
22

3-
Bot using RASA NLU to answer different questions. Using Wolfram Alpha to get answers.
3+
Bot using RASA NLU to answer different questions. Using Wolfram Alpha to get answers. Using Wikipedia as failover.
4+
5+
![bot.png](https://raw.githubusercontent.com/plutov/bot/master/bot.png)
46

57
### Run it with Docker
68

79
Get Slack API Token first: https://wizeline.slack.com/services/B861V31E3
810

911
Get Wolfram App ID: https://developer.wolframalpha.com/portal/myapps/
1012

13+
Wikipedia API doesn't require API key.
14+
15+
1116
```
1217
docker build -t bot . && docker run -e SLACK_TOKEN=<token> -e WOLFRAM_APP_ID=<app_id> bot
1318
```

bot.png

251 KB
Loading

bot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import sys
55
from slack.bot import SlackBot
66
from nlp.rasa import RasaNLP
7-
from wolfram.wolfram import Wolfram
7+
from dataprovider.dataprovider import DataProvider
88

99
try:
10-
w = Wolfram(os.environ.get("WOLFRAM_APP_ID"))
10+
dp = DataProvider(os.environ.get("WOLFRAM_APP_ID"))
1111

12-
r = RasaNLP(w, "rasa-config.json", "rasa-data.json", "./rasa-model")
12+
r = RasaNLP(dp, "rasa-config.json", "rasa-data.json", "./rasa-model")
1313
r.train()
1414

1515
b = SlackBot(os.environ.get("SLACK_TOKEN"), r)

dataprovider/dataprovider.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) 2017 Alex Pliutau
2+
3+
import wolframalpha
4+
import wikipedia
5+
import logging
6+
7+
class DataProvider(object):
8+
NOT_FOUND_MSG = "Sorry, I don't know this yet"
9+
10+
def __init__(self, app_id):
11+
self.wolfram_client = wolframalpha.Client(app_id)
12+
13+
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
14+
logging.info("connected to wolfram")
15+
16+
def get_short_answer(self, query):
17+
logging.info("searching in wolfram: {}".format(query))
18+
19+
try:
20+
wolfram_res = self.wolfram_client.query(query)
21+
logging.info("wolfram res: {}".format(wolfram_res))
22+
23+
return next(wolfram_res.results).text
24+
except:
25+
# use wikipedia as failover
26+
wikiepedia_res = wikipedia.summary(query, sentences=1)
27+
logging.info("wikipedia res: {}".format(wikiepedia_res))
28+
if wikiepedia_res:
29+
return wikiepedia_res
30+
31+
return self.NOT_FOUND_MSG

nlp/rasa.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class RasaNLP(object):
1919
INTENT_WHATIS = "whatis"
2020
ENTITY_QUERY = "query"
2121

22-
def __init__(self, wolfram, config_file, data_file, model_dir):
22+
def __init__(self, data_provider, config_file, data_file, model_dir):
2323
self.greeted = False
2424

25-
self.wolfram = wolfram
25+
self.data_provider = data_provider
2626
self.data_file = data_file
2727
self.model_dir = model_dir
2828
self.rasa_config = RasaNLUConfig(config_file)
@@ -60,4 +60,4 @@ def find_reply(self, msg):
6060
return random.choice(self.COULD_NOT_PARSE_MSGS)
6161

6262
def get_short_answer(self, query):
63-
return self.wolfram.get_short_answer(query)
63+
return self.data_provider.get_short_answer(query)

wolfram/wolfram.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)