Skip to content

Commit

Permalink
Added chatterbot's weather logic adapter to package.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed May 12, 2016
1 parent 191c27f commit d3f4bcf
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
15 changes: 3 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,9 @@ chatterbot-weather

A ChatterBot logic adapter that returns information about the weather.

* Free software: ISC license
* Documentation: https://chatterbot-weather.readthedocs.org.

Features
--------
Installation
------------

* TODO

Credits
---------

This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.

.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage
pip install chatterbot-weather
1 change: 0 additions & 1 deletion chatterbot-weather/chatterbot-weather.py

This file was deleted.

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'
70 changes: 70 additions & 0 deletions chatterbot_weather/weather_adapter.py
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
chatterbot>=0.3.6
python-forecastio==1.3.4
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
test_requirements = history_file.read()

setup(
name='chatterbot-weather',
name='chatterbot_weather',
version='0.1.0',
description="A ChatterBot logic adapter that returns information about the weather.",
long_description=readme + '\n\n' + history,
author="Gunther Cox",
author_email='gunthercx@gmail.com',
url='https://github.com/gunthercox/chatterbot-weather',
packages=[
'chatterbot-weather',
'chatterbot_weather',
],
package_dir={'chatterbot-weather':
'chatterbot-weather'},
Expand Down

0 comments on commit d3f4bcf

Please sign in to comment.