Skip to content

Commit

Permalink
Update tests after utility module change
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Nov 26, 2016
1 parent 202da7e commit 44a3d1c
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 107 deletions.
4 changes: 2 additions & 2 deletions chatterbot/adapters/input/gitter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
from time import sleep
from chatterbot.adapters.input import InputAdapter
from chatterbot.conversation import Statement
from time import sleep
import requests


Expand Down Expand Up @@ -120,7 +120,7 @@ def remove_mentions(self, text):
Return a string that has no leading mentions.
"""
import re
from chatterbot.utils.clean import clean_whitespace
from chatterbot.utils import clean_whitespace
text_without_mentions = re.sub(r'@\S+', '', text)
return clean_whitespace(text_without_mentions)

Expand Down
2 changes: 1 addition & 1 deletion chatterbot/adapters/input/terminal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
from chatterbot.adapters.input import InputAdapter
from chatterbot.conversation import Statement
from chatterbot.utils.read_input import input_function
from chatterbot.utils import input_function


class TerminalAdapter(InputAdapter):
Expand Down
2 changes: 1 addition & 1 deletion chatterbot/adapters/logic/logic_adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals
from chatterbot.adapters import Adapter
from chatterbot.utils.module_loading import import_module
from chatterbot.utils import import_module


class LogicAdapter(Adapter):
Expand Down
7 changes: 6 additions & 1 deletion chatterbot/adapters/output/output_format_adapter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from __future__ import unicode_literals
from chatterbot.adapters.output import OutputAdapter
from chatterbot.utils.read_input import input_function


class OutputFormatAdapter(OutputAdapter):
"""
Return output from the bot in a specified format.
"""

JSON = 'json'
TEXT = 'text'
OBJECT = 'object'
VALID_FORMATS = (JSON, TEXT, OBJECT, )

def __init__(self, *args, **kwargs):
"""
Set the output format for this adapter.
"""
super(OutputFormatAdapter, self).__init__(**kwargs)
self.format = kwargs.get('output_format', 'object')

Expand Down
8 changes: 0 additions & 8 deletions chatterbot/adapters/output/terminal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import unicode_literals
from chatterbot.adapters.output import OutputAdapter
from chatterbot.utils.read_input import input_function


class TerminalAdapter(OutputAdapter):
Expand All @@ -9,13 +8,6 @@ class TerminalAdapter(OutputAdapter):
communicate through the terminal.
"""

def process_input(self, *args, **kwargs):
"""
Read the user's input from the terminal.
"""
user_input = input_function()
return user_input

def process_response(self, statement, confidence=None):
"""
Print the response to the user's input.
Expand Down
8 changes: 4 additions & 4 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import unicode_literals
import logging
from .adapters.storage import StorageAdapter
from .adapters.logic import LogicAdapter, MultiLogicAdapter
from .adapters.input import InputAdapter
from .adapters.output import OutputAdapter
from .utils.queues import ResponseQueue
from .utils.module_loading import import_module
import logging
from .tools.queues import ResponseQueue
from .utils import import_module


class ChatBot(object):
Expand Down Expand Up @@ -117,7 +117,7 @@ def remove_logic_adapter(self, adapter_name):
"""
for index, adapter in enumerate(self.logic.adapters):
if adapter_name == type(adapter).__name__:
del(self.logic.adapters[index])
del self.logic.adapters[index]
return True
return False

Expand Down
7 changes: 3 additions & 4 deletions chatterbot/conversation/comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def levenshtein_distance(statement, other_statement):
:rtype: float
"""
import sys
import warnings

# Use python-Levenshtein if available
try:
Expand Down Expand Up @@ -58,8 +57,8 @@ def synset_distance(statement, other_statement):
:return: The percent of similarity between the closest synset distance.
:rtype: float
"""
from chatterbot.utils.wordnet import Wordnet
from chatterbot.utils.tokenizer import Tokenizer
from chatterbot.tools.wordnet import Wordnet
from chatterbot.tools.tokenizer import Tokenizer
import itertools

wordnet = Wordnet()
Expand Down Expand Up @@ -180,4 +179,4 @@ def get_wordnet_pos(pos_tag):
ratio = len(set(lemmae_a).intersection(lemmae_b)) / float(len(set(lemmae_a).union(lemmae_b)))
except Exception as e:
print('Error', e)
return (ratio >= threshold)
return ratio >= threshold
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ def __init__(self):
# Download the stopwords data only if it is not already downloaded
stopwords_path = None
if os.name == 'nt':
stopwords_path = os.path.join(os.getenv('APPDATA'), 'nltk_data',
'corpora', 'stopwords.zip')
stopwords_path = os.path.join(
os.getenv('APPDATA'), 'nltk_data', 'corpora', 'stopwords.zip'
)
else:
stopwords_path = os.path.join(os.path.expanduser('~'), 'nltk_data',
'corpora', 'stopwords.zip')
stopwords_path = os.path.join(
os.path.expanduser('~'), 'nltk_data', 'corpora', 'stopwords.zip'
)
try:
if not os.path.isfile(stopwords_path):
find('stopwords.zip')
Expand All @@ -38,4 +40,4 @@ def remove_stopwords(self, language, tokens):
# Remove the stop words from the set of word tokens
tokens = set(tokens) - set(stop_words)

return tokens
return tokens
14 changes: 8 additions & 6 deletions chatterbot/utils/tokenizer.py → chatterbot/tools/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ def __init__(self):
# Download the punkt data only if it is not already downloaded
punkt_path = None
if os.name == 'nt':
punkt_path = os.path.join(os.getenv('APPDATA'), 'nltk_data',
'tokenizers', 'punkt.zip')
punkt_path = os.path.join(
os.getenv('APPDATA'), 'nltk_data', 'tokenizers', 'punkt.zip'
)
else:
punkt_path = os.path.join(os.path.expanduser('~'), 'nltk_data',
'tokenizers', 'punkt.zip')
punkt_path = os.path.join(
os.path.expanduser('~'), 'nltk_data', 'tokenizers', 'punkt.zip'
)
try:
if not os.path.isfile(punkt_path):
find('punkt.zip')
Expand All @@ -28,7 +30,7 @@ def get_tokens(self, text, language='english', exclude_stop_words=True):
Skips common stop words such as ("is, the, a, ...")
if 'exclude_stop_words' is True.
"""
from chatterbot.utils.stop_words import StopWordsManager
from chatterbot.tools.stop_words import StopWordsManager
from nltk import word_tokenize

stopwords = StopWordsManager()
Expand All @@ -38,4 +40,4 @@ def get_tokens(self, text, language='english', exclude_stop_words=True):
if exclude_stop_words:
tokens = stopwords.remove_stopwords(language, tokens)

return tokens
return tokens
12 changes: 7 additions & 5 deletions chatterbot/utils/wordnet.py → chatterbot/tools/wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ def __init__(self):
from nltk.data import find
from nltk import download
import os

# Download the wordnet data only if it is not already downloaded
wordnet_path = None
if os.name == 'nt':
wordnet_path = os.path.join(os.getenv('APPDATA'), 'nltk_data',
'corpora', 'wordnet.zip')
wordnet_path = os.path.join(
os.getenv('APPDATA'), 'nltk_data', 'corpora', 'wordnet.zip'
)
else:
wordnet_path = os.path.join(os.path.expanduser('~'), 'nltk_data',
'corpora', 'wordnet.zip')
wordnet_path = os.path.join(
os.path.expanduser('~'), 'nltk_data', 'corpora', 'wordnet.zip'
)
try:
if not os.path.isfile(wordnet_path):
find('wordnet.zip')
Expand Down
10 changes: 5 additions & 5 deletions docs/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ a collection of miscellaneous but useful functions.
Module Imports
--------------

.. autofunction:: chatterbot.utils.module_loading.import_module
.. autofunction:: chatterbot.utils.import_module

String cleaning
---------------

This package of utility contains methods that are usefull
for cleaning and normalizing strings of text.

.. autofunction:: chatterbot.utils.clean.clean_whitespace
.. autofunction:: chatterbot.utils.clean_whitespace

.. autofunction:: chatterbot.utils.clean.clean
.. autofunction:: chatterbot.utils.clean

Terminal input
--------------

.. autofunction:: chatterbot.utils.read_input.input_function
.. autofunction:: chatterbot.utils.input_function

Queues
------

.. autoclass:: chatterbot.utils.queues.ResponseQueue
.. autoclass:: chatterbot.tools.queues.ResponseQueue
:members:
2 changes: 1 addition & 1 deletion examples/learning_feedback_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)

def get_feedback():
from chatterbot.utils.read_input import input_function
from chatterbot.utils import input_function

text = input_function()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'chatterbot.ext.django_chatterbot.migrations',
'chatterbot.ext.django_chatterbot.management',
'chatterbot.ext.django_chatterbot.management.commands',
'chatterbot.utils'
'chatterbot.tools'
],
package_dir={'chatterbot': 'chatterbot'},
include_package_data=True,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parsing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from unittest import TestCase
from datetime import timedelta, date, datetime
from chatterbot.utils.parsing import datetime_parsing, this_week_day, previous_week_day, next_week_day, dateFromDuration
from chatterbot.tools.parsing import datetime_parsing, this_week_day, previous_week_day, next_week_day, dateFromDuration

"""
Output of the parser is an array of tuples
Expand Down
2 changes: 1 addition & 1 deletion tests/test_queues.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unittest import TestCase
from chatterbot.utils.queues import ResponseQueue
from chatterbot.tools.queues import ResponseQueue


class ResponseQueueTests(TestCase):
Expand Down
53 changes: 53 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from unittest import TestCase


class TokenizerTestCase(TestCase):

def setUp(self):
super(TokenizerTestCase, self).setUp()
from chatterbot.tools.tokenizer import Tokenizer

self.tokenizer = Tokenizer()

def test_get_tokens(self):
tokens = self.tokenizer.get_tokens('what time is it', exclude_stop_words=False)
self.assertEqual(tokens, ['what', 'time', 'is', 'it'])

def test_get_tokens_exclude_stop_words(self):
tokens = self.tokenizer.get_tokens('what time is it', exclude_stop_words=True)
self.assertEqual(tokens, {'time'})


class StopWordsTestCase(TestCase):

def setUp(self):
super(StopWordsTestCase, self).setUp()
from chatterbot.tools.stop_words import StopWordsManager

self.stopwords_manager = StopWordsManager()

def test_remove_stop_words(self):
tokens = ['this', 'is', 'a', 'test', 'string']
words = self.stopwords_manager.remove_stopwords('english', tokens)

# This example list of words should end up with only two elements
self.assertEqual(len(words), 2)
self.assertIn('test', list(words))
self.assertIn('string', list(words))


class WordnetTestCase(TestCase):

def setUp(self):
super(WordnetTestCase, self).setUp()
from chatterbot.tools.wordnet import Wordnet

self.wordnet = Wordnet()

def test_wordnet(self):
synsets = self.wordnet.synsets('test')

self.assertEqual(
0.06666666666666667,
synsets[0].path_similarity(synsets[1])
)

0 comments on commit 44a3d1c

Please sign in to comment.