Skip to content

Commit

Permalink
Move system imports to top level of files
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Feb 17, 2025
1 parent 823430e commit 99ddfb9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 30 deletions.
7 changes: 4 additions & 3 deletions chatterbot/languages.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import sys
import inspect


class AAR:
ISO_639_1 = ''
ISO_639 = 'aar'
Expand Down Expand Up @@ -2411,7 +2415,4 @@ class ZZA:


def get_language_classes():
import sys
import inspect

return inspect.getmembers(sys.modules[__name__], inspect.isclass)
4 changes: 2 additions & 2 deletions chatterbot/logic/logic_adapter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from random import choice

from chatterbot.adapters import Adapter
from chatterbot.storage import StorageAdapter
from chatterbot.search import IndexedTextSearch
Expand Down Expand Up @@ -105,8 +107,6 @@ def get_default_response(self, input_statement):
This method is called when a logic adapter is unable to generate any
other meaningful response.
"""
from random import choice

if self.default_responses:
response = choice(self.default_responses)
else:
Expand Down
21 changes: 10 additions & 11 deletions chatterbot/preprocessors.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
"""
Statement pre-processors.
"""
from unicodedata import normalize
from re import sub as re_sub
from html import unescape


def clean_whitespace(statement):
"""
Remove any consecutive whitespace characters from the statement text.
"""
import re

# Replace linebreaks and tabs with spaces
statement.text = statement.text.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ')
# Uses splitlines() which includes a superset of universal newlines:
# https://docs.python.org/3/library/stdtypes.html#str.splitlines
statement.text = ' '.join(statement.text.splitlines()).replace('\t', ' ')

# Remove any leeding or trailing whitespace
# Remove any leading or trailing whitespace
statement.text = statement.text.strip()

# Remove consecutive spaces
statement.text = re.sub(' +', ' ', statement.text)
statement.text = re_sub(' +', ' ', statement.text)

return statement

Expand All @@ -26,9 +29,7 @@ def unescape_html(statement):
Convert escaped html characters into unescaped html characters.
For example: "&lt;b&gt;" becomes "<b>".
"""
import html

statement.text = html.unescape(statement.text)
statement.text = unescape(statement.text)

return statement

Expand All @@ -38,9 +39,7 @@ def convert_to_ascii(statement):
Converts unicode characters to ASCII character equivalents.
For example: "på fédéral" becomes "pa federal".
"""
import unicodedata

text = unicodedata.normalize('NFKD', statement.text)
text = normalize('NFKD', statement.text)
text = text.encode('ascii', 'ignore').decode('utf-8')

statement.text = str(text)
Expand Down
3 changes: 1 addition & 2 deletions chatterbot/storage/mongodb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from random import randint
from chatterbot.storage import StorageAdapter


Expand Down Expand Up @@ -244,8 +245,6 @@ def get_random(self):
"""
Returns a random statement from the database
"""
from random import randint

count = self.count()

if count < 1:
Expand Down
3 changes: 1 addition & 2 deletions chatterbot/storage/sql_storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
from chatterbot.storage import StorageAdapter


Expand Down Expand Up @@ -351,8 +352,6 @@ def get_random(self):
"""
Returns a random statement from the database.
"""
import random

Statement = self.get_model('statement')

session = self.Session()
Expand Down
5 changes: 2 additions & 3 deletions chatterbot/trainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import csv
import time
import glob
import json
import tarfile
from tqdm import tqdm
from dateutil import parser as date_parser
from chatterbot.conversation import Statement
Expand Down Expand Up @@ -69,7 +71,6 @@ def export_for_training(self, file_path='./export.json'):
Create a file from the database that can be used to
train other chat bots.
"""
import json
export = {'conversations': self._generate_export_data()}
with open(file_path, 'w+', encoding='utf8') as jsonfile:
json.dump(export, jsonfile, ensure_ascii=False)
Expand Down Expand Up @@ -265,8 +266,6 @@ def extract(self, file_path):
"""
Extract a tar file at the specified file path.
"""
import tarfile

print('Extracting {}'.format(file_path))

if not os.path.exists(self.extracted_data_directory):
Expand Down
9 changes: 3 additions & 6 deletions chatterbot/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""
ChatterBot utility functions
"""
import importlib
import time
import sys


def import_module(dotted_path):
"""
Imports the specified module based on the
dot notated import path for the module.
"""
import importlib

module_parts = dotted_path.split('.')
module_path = '.'.join(module_parts[:-1])
module = importlib.import_module(module_path)
Expand Down Expand Up @@ -81,8 +82,6 @@ def get_response_time(chatbot, statement='Hello'):
:returns: The response time in seconds.
:rtype: float
"""
import time

start_time = time.time()

chatbot.get_response(statement)
Expand Down Expand Up @@ -110,8 +109,6 @@ def print_progress_bar(description, iteration_counter, total_items, progress_bar
DEPRECTTED: use `tqdm` instead
"""
import sys

percent = float(iteration_counter) / total_items
hashes = '#' * int(round(percent * progress_bar_length))
spaces = ' ' * (progress_bar_length - len(hashes))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CleanWhitespacePreprocessorTestCase(ChatBotTestCase):
def test_clean_whitespace(self):
statement = Statement(text='\tThe quick \nbrown fox \rjumps over \vthe \alazy \fdog\\.')
cleaned = preprocessors.clean_whitespace(statement)
normal_text = 'The quick brown fox jumps over \vthe \alazy \fdog\\.'
normal_text = 'The quick brown fox jumps over the \alazy dog\\.'

self.assertEqual(cleaned.text, normal_text)

Expand Down

0 comments on commit 99ddfb9

Please sign in to comment.