Skip to content

Commit 739220f

Browse files
committed
Add annotations for return types
1 parent bb61312 commit 739220f

21 files changed

+33
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ An example of typical input would be something like this:
2323
2424
## How it works
2525

26-
An untrained instance of ChatterBot starts off with no knowledge of how to communicate. Each time a user enters a statement, the library saves the text that they entered and the text that the statement was in response to. As ChatterBot receives more input the number of responses that it can reply and the accuracy of each response in relation to the input statement increase. The program selects the closest matching response by searching for the closest matching known statement that matches the input, it then returns the most likely response to that statement based on how frequently each response is issued by the people the bot communicates with.
26+
An untrained instance of ChatterBot starts off with no knowledge of how to communicate. Each time a user enters a statement, the library saves the text that they entered and the text that the statement was in response to. As ChatterBot receives more input the number of responses that it can reply to, and the accuracy of each response in relation to the input statement increases. The program selects the closest matching response by searching for the closest matching known statement that matches the input, it then returns the most likely response to that statement based on how frequently each response is issued by the people the bot communicates with.
2727

2828
# [Documentation](https://docs.chatterbot.us)
2929

chatterbot/chatterbot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Union
23
from chatterbot.storage import StorageAdapter
34
from chatterbot.logic import LogicAdapter
45
from chatterbot.search import TextSearch, IndexedTextSearch
@@ -130,7 +131,7 @@ def __init__(self, name, stream=False, **kwargs):
130131
# Allow the bot to save input it receives so that it can learn
131132
self.read_only = kwargs.get('read_only', False)
132133

133-
def get_response(self, statement=None, **kwargs) -> Statement:
134+
def get_response(self, statement: Union[Statement, str, dict] = None, **kwargs) -> Statement:
134135
"""
135136
Return the bot's response based on the input.
136137

chatterbot/conversation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class StatementMixin(object):
2222

2323
extra_statement_field_names = []
2424

25-
def get_statement_field_names(self):
25+
def get_statement_field_names(self) -> list[str]:
2626
"""
2727
Return the list of field names for the statement.
2828
"""
2929
return self.statement_field_names + self.extra_statement_field_names
3030

31-
def get_tags(self):
31+
def get_tags(self) -> list[str]:
3232
"""
3333
Return the list of tags for this statement.
3434
"""
@@ -79,7 +79,7 @@ class Statement(StatementMixin):
7979
'storage',
8080
)
8181

82-
def __init__(self, text, in_response_to=None, **kwargs):
82+
def __init__(self, text: str, in_response_to=None, **kwargs):
8383

8484
self.id = kwargs.get('id')
8585
self.text = str(text)

chatterbot/corpus.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
CORPUS_EXTENSION = 'yml'
1919

2020

21-
def get_file_path(dotted_path, extension='json'):
21+
def get_file_path(dotted_path, extension='json') -> str:
2222
"""
2323
Reads a dotted file path and returns the file path.
2424
"""
@@ -41,7 +41,7 @@ def get_file_path(dotted_path, extension='json'):
4141
return corpus_path
4242

4343

44-
def read_corpus(file_name):
44+
def read_corpus(file_name) -> dict:
4545
"""
4646
Read and return the data from a corpus json file.
4747
"""
@@ -59,7 +59,7 @@ def read_corpus(file_name):
5959
return yaml.safe_load(data_file)
6060

6161

62-
def list_corpus_files(dotted_path):
62+
def list_corpus_files(dotted_path) -> list[str]:
6363
"""
6464
Return a list of file paths to each data file in the specified corpus.
6565
"""

chatterbot/ext/django_chatterbot/abstract_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __str__(self):
117117
return self.text
118118
return '<empty>'
119119

120-
def get_tags(self):
120+
def get_tags(self) -> list[str]:
121121
"""
122122
Return the list of tags for this statement.
123123
(Overrides the method from StatementMixin)

chatterbot/ext/sqlalchemy_app/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ModelBase(object):
1313
"""
1414

1515
@declared_attr
16-
def __tablename__(cls):
16+
def __tablename__(cls) -> str:
1717
"""
1818
Return the lowercase class name as the name of the table.
1919
"""
@@ -99,7 +99,7 @@ class Statement(Base, StatementMixin):
9999
server_default=''
100100
)
101101

102-
def get_tags(self):
102+
def get_tags(self) -> list[str]:
103103
"""
104104
Return a list of tags for this statement.
105105
"""

chatterbot/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def get_recent_repeated_responses(chatbot, conversation, sample=10, threshold=3, quantity=3):
1+
def get_recent_repeated_responses(chatbot, conversation, sample=10, threshold=3, quantity=3) -> list:
22
"""
33
A filter that eliminates possibly repetitive responses to prevent
44
a chat bot from repeating statements that it has recently said.

chatterbot/logic/best_match.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from chatterbot.logic import LogicAdapter
2+
from chatterbot.conversation import Statement
23
from chatterbot import filters
34

45

@@ -22,7 +23,7 @@ def __init__(self, chatbot, **kwargs):
2223

2324
self.excluded_words = kwargs.get('excluded_words')
2425

25-
def process(self, input_statement, additional_response_selection_parameters=None):
26+
def process(self, input_statement: Statement, additional_response_selection_parameters=None) -> Statement:
2627

2728
# Get all statements that have a response text similar to the input statement
2829
search_results = self.search_algorithm.search(input_statement)

chatterbot/logic/logic_adapter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def can_process(self, statement) -> bool:
8686
"""
8787
return True
8888

89-
def process(self, statement, additional_response_selection_parameters=None) -> Statement:
89+
def process(self, statement: Statement, additional_response_selection_parameters: dict = None) -> Statement:
9090
"""
9191
Override this method and implement your logic for selecting a response to an input statement.
9292
@@ -99,11 +99,9 @@ def process(self, statement, additional_response_selection_parameters=None) -> S
9999
lowest confidence level and 1 is the highest.
100100
101101
:param statement: An input statement to be processed by the logic adapter.
102-
:type statement: Statement
103102
104103
:param additional_response_selection_parameters: Parameters to be used when
105104
filtering results to choose a response from.
106-
:type additional_response_selection_parameters: dict
107105
"""
108106
raise self.AdapterMethodNotImplementedError()
109107

chatterbot/logic/mathematical_evaluation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, chatbot, **kwargs):
2525
self.language = kwargs.get('language', languages.ENG)
2626
self.cache = {}
2727

28-
def can_process(self, statement):
28+
def can_process(self, statement) -> bool:
2929
"""
3030
Determines whether it is appropriate for this
3131
adapter to respond to the user input.
@@ -34,7 +34,7 @@ def can_process(self, statement):
3434
self.cache[statement.text] = response
3535
return response.confidence == 1
3636

37-
def process(self, statement, additional_response_selection_parameters=None):
37+
def process(self, statement: Statement, additional_response_selection_parameters: dict = None) -> Statement:
3838
"""
3939
Takes a statement string.
4040
Returns the equation from the statement with the mathematical terms solved.

0 commit comments

Comments
 (0)