Override this method and implement your logic for selecting a response to an input statement.

A confidence value and the selected response statement should be returned.
The confidence value represents a rating of how accurate the logic adapter
expects the selected response to be. Confidence scores are used to select
the best response from multiple logic adapters.

The confidence value should be a number between 0 and 1 where 0 is the
lowest confidence level and 1 is the highest.

Parameters:

statement (Statement) – An input statement to be processed by the logic adapter.

fromchatterbot.logicimportLogicAdapterclassMyLogicAdapter(LogicAdapter):def__init__(self,**kwargs):super(MyLogicAdapter,self).__init__(**kwargs)defcan_process(self,statement):returnTruedefprocess(self,statement):importrandom# Randomly select a confidence between 0 and 1confidence=random.uniform(0,1)# For this example, we will just return the input as outputselected_statement=statementselected_statement.confidence=confidencereturnselected_statement

If you want a particular logic adapter to only respond to a unique type of
input, the best way to do this is by overriding the can_process
method on your own logic adapter.

Here is a simple example. Let’s say that we only want this logic adapter to
generate a response when the input statement starts with “Hey Mike”. This
way, statements such as “Hey Mike, what time is it?” will be processed,
but statements such as “Do you know what time it is?” will not be processed.

In some cases, it is useful to have a logic adapter that can interact with an external service or
api in order to complete its task. Here is an example that demonstrates how this could be done.
For this example we will use a fictitious API endpoint that returns the current temperature.

defcan_process(self,statement):""" Return true if the input statement contains 'what' and 'is' and 'temperature'. """words=['what','is','temperature']ifall(xinstatement.text.split()forxinwords)returnTrueelse:returnFalsedefprocess(self,statement):fromchatterbot.conversationimportStatementimportrequests# Make a request to the temperature APIresponse=requests.get('https://api.temperature.com/current?units=celsius')data=response.json()# Let's base the confidence value on if the request was successfulifresponse.status_code==200:confidence=1else:confidence=0temperature=data.get('temperature','unavailable')response_statement=Statement('The current temperature is {}'.format(temperature))returnconfidence,response_statement

All key word arguments that have been set in your ChatBot class’s constructor
will also be passed to the __init__ method of each logic adapter.
This allows you to access these variables if you need to use them in your logic adapter.
(An API key might be an example of a parameter you would want to access here.)

You can override the __init__ method on your logic adapter to store additional
information passed to it by the ChatBot class.