-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added chatterbot's weather logic adapter to package.
- Loading branch information
1 parent
191c27f
commit d3f4bcf
Showing
6 changed files
with
78 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
chatterbot-weather/__init__.py → chatterbot_weather/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .weather_adapter import WeatherLogicAdapter | ||
|
||
__author__ = 'Gunther Cox' | ||
__email__ = 'gunthercx@gmail.com' | ||
__version__ = '0.1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
from chatterbot.adapters.logic import LogicAdapter | ||
from chatterbot.conversation import Statement | ||
from chatterbot.utils.pos_tagger import POSTagger | ||
|
||
import forecastio | ||
import re | ||
|
||
|
||
class WeatherLogicAdapter(LogicAdapter): | ||
""" | ||
A logic adapter that returns information regarding the weather and | ||
the forecast for a specific location. Currently, only basic information | ||
is returned, but additional features are planned in the future. | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
super(WeatherLogicAdapter, self).__init__(**kwargs) | ||
|
||
self.tagger = POSTagger() | ||
self.forecastio_api_key = kwargs.get("forecastio_api_key") | ||
|
||
def process(self, statement): | ||
""" | ||
Returns the forecast for a location (using latitude and longitude). | ||
""" | ||
user_input = statement.text.lower() | ||
if "weather" not in user_input: | ||
return 0, Statement("") | ||
|
||
latitude = self.get_latitude(user_input) | ||
longitude = self.get_longitude(user_input) | ||
|
||
if latitude is not "" and longitude is not "": | ||
# @TODO: Add more options for getting weather. This could include | ||
# the current temperature, the current cloud cover, etc. This | ||
# might require removing the forecastio library (which is | ||
# probably a good idea). | ||
return 1, Statement("The forecast for tomorrow is: " + self.get_weather(latitude, longitude)) | ||
|
||
return 0, Statement("") | ||
|
||
def get_latitude(self, user_input): | ||
""" | ||
Returns the latitude extracted from the input. | ||
""" | ||
for token in self.tagger.tokenize(user_input): | ||
if "latitude=" in token: | ||
return re.sub("latitude=", "", token) | ||
|
||
return "" | ||
|
||
def get_longitude(self, user_input): | ||
""" | ||
Returns the longitude extracted from the input. | ||
""" | ||
for token in self.tagger.tokenize(user_input): | ||
if "longitude=" in token: | ||
return re.sub("longitude=", "", token) | ||
|
||
return "" | ||
|
||
def get_weather(self, latitude, longitude): | ||
""" | ||
Returns the weather for a given latitude and longitude. | ||
""" | ||
# @TODO: Find some way to suppress the warnings generated by this. | ||
forecast = forecastio.load_forecast(self.forecastio_api_key, latitude, longitude) | ||
|
||
return forecast.hourly().summary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
chatterbot>=0.3.6 | ||
python-forecastio==1.3.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters