Skip to content

Commit

Permalink
Moved sicial adapters into the adapter module.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jul 25, 2015
1 parent 19b3eb0 commit f2e469d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 88 deletions.
2 changes: 2 additions & 0 deletions chatterbot/adapters/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .io import IOAdapter
from .terminal import TerminalAdapter
from .twitter import TwitterAdapter
from .github import GitHubAdapter
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from chatterbot.adapters.io import IOAdapter
import requests

class GitHub(object):

class GitHubAdapter(IOAdapter):

def __init__(self, github):
self.github = github
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
from chatterbot.adapters.io import IOAdapter
import requests
from requests_oauthlib import OAuth1


class Twitter(object):
class TwitterAdapter(IOAdapter):

def __init__(self, twitter):

Expand Down
20 changes: 15 additions & 5 deletions chatterbot/apis/__init__.py → chatterbot/utils/clean.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
def clean(text):
import re

def clean_whitespace(text):
"""
A function for cleaning a string of text.
Returns valid ASCII characters.
Remove any extra whitespace and line breaks as needed.
"""
import re, sys, unicodedata

# Replace linebreaks with spaces
text = text.replace("\n", " ").replace("\r", " ").replace("\t", " ")

Expand All @@ -14,6 +13,17 @@ def clean(text):
# Remove consecutive spaces
text = re.sub(" +", " ", text)

return text

def clean(text):
"""
A function for cleaning a string of text.
Returns valid ASCII characters.
"""
import sys, unicodedata

text = clean_whitespace(text)

# Remove links from message
#text = re.sub(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', '', text)

Expand Down
42 changes: 21 additions & 21 deletions examples/twitter.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
'''
Check for online mentions on social media sites.
Respond to mentions on twitter.
The bot will follow the user who mentioned it and
favorite the post in which the mention was made.
'''

from chatterbot.apis.twitter import Twitter
chatbot = ChatBot("ChatterBot",
storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter",
logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter",
io_adapter="chatterbot.adapters.io.TwitterAdapter",
database="../database.db")

chatbot = ChatBot("ChatterBot")
for mention in chatbot.get_mentions():

if "twitter" in kwargs:
twitter_bot = Twitter(kwargs["twitter"])
'''
Check to see if the post has been favorited
We will use this as a check for whether or not to respond to it.
Only respond to unfavorited mentions.
'''

for mention in twitter_bot.get_mentions():
if not mention["favorited"]:
screen_name = mention["user"]["screen_name"]
text = mention["text"]
response = chatbot.get_response(text)

'''
Check to see if the post has been favorited
We will use this as a check for whether or not to respond to it.
Only respond to unfavorited mentions.
'''
print(text)
print(response)

if not mention["favorited"]:
screen_name = mention["user"]["screen_name"]
text = mention["text"]
response = chatbot.get_response(text)
chatbot.follow(screen_name)
chatbot.favorite(mention["id"])
chatbot.reply(mention["id"], response)

print(text)
print(response)

follow(screen_name)
favorite(mention["id"])
reply(mention["id"], response)
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from unittest import TestCase
from chatterbot.apis.twitter import Twitter
from chatterbot.algorithms.twitter import remove_leeding_usernames
from chatterbot.algorithms.twitter import remove_trailing_usernames
from chatterbot.adapters.io import TwitterAdapter


class TwitterTests(TestCase):
Expand All @@ -12,7 +10,7 @@ def test_consumer_stored(self):
"CONSUMER_SECRET": "nullvoidnullvoidnullvoid"
}

chatbot = Twitter(twitter=TWITTER)
chatbot = TwitterAdapter(twitter=TWITTER)

self.assertEqual(TWITTER["CONSUMER_KEY"], chatbot.consumer_key)
self.assertEqual(TWITTER["CONSUMER_SECRET"], chatbot.consumer_secret)
Expand Down
56 changes: 0 additions & 56 deletions tests/test_apis.py

This file was deleted.

54 changes: 54 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
from unittest import TestCase

from chatterbot.utils.clean import clean_whitespace
from chatterbot.utils.clean import clean
from chatterbot.utils.module_loading import import_module


Expand All @@ -8,3 +11,54 @@ class UtilityTests(TestCase):
def test_import_module(self):
# TODO
self.assertTrue(True)


class CleanWhitespaceTests(TestCase):

def test_clean_whitespace(self):
text = '\tThe quick \nbrown fox \rjumps over \vthe \alazy \fdog\\.'
clean_text = clean_whitespace(text)
normal_text = 'The quick brown fox jumps over \vthe \alazy \fdog\\.'

self.assertEqual(clean_text, normal_text)

def test_leading_or_trailing_whitespace_removed(self):

text = ' The quick brown fox jumps over the lazy dog. '
clean_text = clean_whitespace(text)
normal_text = 'The quick brown fox jumps over the lazy dog.'

self.assertEqual(clean_text, normal_text)

def test_consecutive_spaces_removed(self):

text = 'The quick brown fox jumps over the lazy dog.'
clean_text = clean_whitespace(text)
normal_text = 'The quick brown fox jumps over the lazy dog.'

self.assertEqual(clean_text, normal_text)


class CleanTests(TestCase):

def test_html_characters_restored(self):

# implicit concatenation
text = 'The quick brown fox <b>jumps</b> over'
' the <a href="http://lazy.com">lazy</a> dog.'

normal_text = 'The quick brown fox <b>jumps</b> over'
' the <a href="http://lazy.com">lazy</a> dog.'

clean_text = clean(text)

self.assertEqual(clean_text, normal_text)

def test_non_ascii_chars_replaced(self):

text = u"Klüft skräms inför på fédéral électoral große"
clean_text = clean(text)
normal_text = "Kluft skrams infor pa federal electoral groe"

self.assertEqual(clean_text, normal_text)

0 comments on commit f2e469d

Please sign in to comment.