simbots package

Submodules

simbots.Bot module

class simbots.Bot.Bot(intentExamples, entityExamples, templates, confidenceLimit=0, testCases=None, subConversations=None)

Bases: object

Whenever a new bot is created it will inherit from the Bot class.

addConversationAsTestCase(caseName, caseDescription, dialogStart, dialogEnd)

This function can be used to add a sample conversation in the dev mode as a test case

Parameters
  • caseName – The case name

  • caseDescription – Case Description (string)

  • dialogStart – The test case will start from this dialog number

  • dialogEnd – The test case will end at this dialog number (the current dialog is included)

evaluateAllTestCases(theme)
evaluateTestCase(caseName, outputTheme)

evaluate a particular testCase :param caseName: :param outputTheme: The bot template theme to use for getting the output message :return: .. code:: json

{ “caseName”“test case name as string” ,

“failedCases” : “failedCases as int”, “result” : “testResults”

}

executeCurrentSubConversation(outputs=None)

Find which conversation has isHappenning set to true and executes first found in subconversations :param outputs: outputs that were previously computed :return: outputs modified (contains outputs of the executed subConversation (as list))

findCurrentlyHappeningSubconversation()

First checks if any subConversation is happening or not : returns first found , then checks if the enterOn condition of any subConversation is completed or not , returns first found :return: [index of the subconversation,subconversationId of the subconversation which matches the above criteria

getBotConfidence()

Returns the bot confidence as list of tuples :return: [(intentName,intentConfidence) ..]

getBotContext()

Returns bot context :return: context as json

getBotOutput(message, outputTheme)

Get’s bot output for a given message and output theme

Parameters
  • message – input message as string

  • outputTheme – theme from botTemplates

Returns

message as string

listAllTestCases()

print all test cases

listTestCase(caseName)

print test case with the given case name :param caseName: Case Name as string

loadBot(loadPath)

Loads trained bot from the path specified, note that context stack (conversation) is not loaded :param loadPath: path to a pickle file for loading the bot

loadConversation(loadPath)

Will load existing conversation from json file :param loadPath: Path to json file

reason()

Bot reasoning is done here , use bot intents and entities to create message logic here

Returns

Should return a list of { “tag”: Used to identify output message from the template “data”: To be used to fill the missing attributes in message to generate the complete message }

run(theme='basic', mode='dev', loadPath='', conversationId='', message='', conversationSavePath='')

Bot can run in two modes

dev : cli to debug, train intents, evaluate test cases and more singleMessageResponse : to be used when deploying in production , gives output for a single message only loads in conversations from conversationSavePath and trained bot from loadpath

Parameters
  • theme – The theme name from bot responses

  • mode – dev and singleMessageResponse

  • loadPath – Loads trained intents ,entities and relations from .p file ie. file exported from saveBotMethod (only used in singleMessageResponse mode)

  • conversationId – conversationId of the conversation to continue (only used in singleMessageResponse mode)

  • message – (only used in singleMessageResponse mode)

  • conversationSavePath – looks for context at this path (only used in singleMessageResponse mode)

Returns

conversationId,outputMessage (only used in singleMessageResponse mode)

no returns in dev mode

saveBot(savePath)

Saves the trained bot to a pickle file , note that the context stack is not saved :param savePath: path to a pickle file for saving the bot contents

saveConversation(savePath=None, uid=None)

Export the current conversation (context stack ) to a json file which can be loaded in later to continue the conversation :param savePath: Path to save the conversation :param uid: Unique Conversation Id (str) , if not supplied will be randomly generated

saveSubConversationStatus()

Save SubConversationStatus (isHappenning True or False) as a dict in the context manager

say(output, outputTheme)

Generate bot output message by adding message to the output components :param output: :param outputTheme: :return: message as string

updateConversationInputs(message)

Update the conversation by updating the context with the input message :param message: :return:

updateConversationOutputs(message)

Update the conversation by updating the context with the output message :param message: :return:

updateIntentsAndEntitiesInContextManager()

Use this function when you need to retrain the bot with newly added intent and entity examples

updateSubConversationStatus()
class simbots.Bot.SubConversation(name='518fa8c6-6990-43ef-89ae-59da481d4ffc', subConversationId='0ab566de-1f8f-4c23-a1a4-ff79d644b56a', enter=None, exit=None, onEnterLogic=None, onExitLogic=None)

Bases: object

Subconversations can be used to divide bot logic into multiple small parts

enterOn(contextManager)

Enter this SubConversation when condition is fulfilled :param contextManager: contextManager object , use this to construct a bool :return: bool that indicates if to enter this SubConversation or not

execute(contextManager, outputs)

Checks the enterOn and exitOn condition and executes the :param contextManager: :param outputs: creates outputs of the SubConversation using contextManager :return: modified contextManager , outputs

exitOn(contextManager)

Make a hard exit from this Subconversation when given condition is satisfied

Parameters

contextManager – contextManager object , use this to construct a bool

Returns

bool that indicates if to make a hard exit or not

simbots.Context module

class simbots.Context.ContextManager(entitiesExtractorJson, allIntentExamples)

Bases: object

Context handler class, This maintains context as a dict and also as a Object Path tree, to facilitate searching within the context also provides functions to search within the context object. Context manager also manages and handles subconversations .

clearContext()

Clears context

findCurrentEntities()

Find the entities detected in the current message :return: currentEntities : the entities detected in the current message as list

findCurrentTopIntent()

Find the the top intent in the current message :return: topIntent: the Top intent ie intent with rank 1

static findObjectInStuff(stuff, _filter)

Generalised method to search within any given dictionary using objectPath library :param stuff: the dictionary where the object is to be searched :param filter: the filter :return: list of matched objects

findStuff(_filter=None, stuff='intents')

Basic method to search a particular part of the context ie Intent, Entities, Message :param filter: dict of terms to search :param stuff: part of context that needs to be searched ie Intent, Entities, Message :return: matched values as list

setNewContext(newContext)

Deletes the old context and sets a new one . :param newContext: the new context object as json

updateContextTree()

Updates the contextTree with the current context so that everything in the context is searchable, this becomes useful when saving custom variables in context .

updateDialog(message, type='input')

Updates the context by 1) Computing all entities present in the message 2) Extracting all intents in the message 3) Updating the current dialog number 4) Updating messages in the context

Parameters
  • message – Message text

  • type – message type : “input” or “output”

simbots.Entity module

class simbots.Entity.EntitiesHandler(entitiesExtractorJson=None)

Bases: object

This class is responsible for handling entities within the bot The init method for the EntitiesHandler Class

param entitiesExtractorJson

this is a json which specifies the type of entities and their definitions . Note currently no schema checks have been implemented .

Sample Json accepted

{
        "GreetingsHelper":{
            "wsup" : [{
                        "tag":"case-insensitive",
                        "pattern":"\s[w]*[a]*[s]+[u]+[p]+\s",
                        "type":"regex"
            }
                    ],
            "hi":[
                {
                    "tag":"case-insensitive",
                    "pattern":"\s[h]+[i]+\s",
                    "type":"regex"


                }

            ],
            "hello":[
                {
                    "tag":"case-insensitive",
                    "pattern":"\s[h]+[e]+[l]+[o]+\s",
                    "type":"regex"


                },
                {
                   "tag":"case-insensitive",
                   "extractor":helloExtractorFunc ->
                   "type":"function"

                }

            ]


        },

        "LaughterHelper":{

            "haha" : [{
                        "tag":"case-insensitive",
                        "pattern":"\s(h+(a|e)+)+(h+)?\s",
                        "type":"regex"


            }
                    ],
            "happysmily":[
                {
                    "tag":"case-insensitive",
                    "pattern":"\s\:\)\s",
                    "type":"regex"


                }

            ],

        },
        "CoolHelper":{
            "cool":[
                {
                    "tag":"case-insensitive",
                    "pattern":"\sc+oo+l+\s",
                    "type":"regex"

                }


            ]


        },
        "ByeHelper":{
            "bye":[
                {
                    "tag":"case-insensitive",
                    "pattern":"\s(goo+d)?b+y+e+\s",
                    "type":"regex"

                }
            ]

        }

    }

kind : Here GreetingsHelper,LaughterHelper,CoolHelper,ByeHelper are entity kinds. also “wsup”,”hi”,”hello” are entity values assosciated with the entity kind GreetingsHelper

type: Only two types are supported -> regex and function.

pattern : the pattern of the regex, only used if type is regex,

extractor: A function that extracts the given entity from the message text should be present if type is function,

Note that any function used should take in two arguments messageText and dialogNumber and should return A list of dicts of the format

[{
            "value":the value of the entity , for more details look at the sample json.,
            "exactValue":the exact value that occurs within the message text.,
            "kind":the kind of the entity eg  food in case of pizza.,
            "dialogNumber":the dialog number of the current message -> same as the input param.,
            "foundAt":[start location , end location],

}]

substituter: A custom substitution function to substitute entity by another value, This function takes in the message text and should return the substituted message text this should always be present if type is “function”. should return substituted message text.

extractAllEntities(message, dialogNumber=0)
This function extracts all entities from the message .
param message

The message text

param dialogNumber

the current dialog number

return

A list of dicts of the format

[{
            "value":the value of the entity , for more details look at the sample json.,
            "exactValue":the exact value that occurs within the message text.,
            "kind":the kind of the entity eg : food in case of pizza.,
            "dialogNumber":the dialog number of the current message -> same as the input param.,
            "foundAt":[start location , end location],

}]
extractEntitySynonym(message, pattern, value, kind, tag, dialogNumber)
This function extracts the entities and synonyms from the text message and returns a json. Currently it does not extract any synonyms
param message

The text message str eg “I want to buy a pizza”

param pattern

param value

param kind

param tag

can be “case-insensitive” if we want the extractor to ignore the casing (uppercase or lowercase) of the entity text

param dialogNumber

the dialog number of the message string.

return

A list of dicts of the format

[{
            "value":the value of the entity , for more details look at the sample json.,
            "exactValue":the exact value that occurs within the message text.,
            "kind":the kind of the entity eg : food in case of pizza.,
            "dialogNumber":the dialog number of the current message -> same as the input param.,
            "foundAt":[start location , end location],

}]
substituteAllEntities(message)
This function substitutes all entity values with entity kinds

:param message:The message text :return: substituted message text

substituteEntityKind(message, pattern, by, tag)

This function substitutes an entity value for its kind in the message text and returns the substituted message text . Note this function is currently unstable and a better version needs to be written to substitute entities effectively .

param message

The message text

param pattern

the entity pattern

param by

the value to be substituted by eg if we need to substitute the word “equivalent” by “alike” then by =”alike”

or if pizza then it will be kind :param tag:”case-insensitive” or not :return: the substituted message text

simbots.Intent module

class simbots.Intent.Intent(intentName='Irrelevant', allIntentExamples=None, entityHandler=None, testSize=0.25, vocab=None)

Bases: abc.ABC

The abstract method for any intent.

abstract createIntent(clfName=None)

Method to create intent will be overridden in the derived class

Parameters

clfName – Classifier Name

Returns

the inherited class should return a classifier

abstract static train(X_train, y_train, params=None)
Parameters
  • X_train – Training examples

  • y_train – Training labels

  • params – Any additional parameters that may be needed can be defined here

Returns

class simbots.Intent.IntentsHandler(allIntentExamples=None, entityHandler=None)

Bases: object

Class for creating and handling multiple intents

createAllTrainedIntents()

Creates trained intents and sets self.trained

getMessageIntents(message, dialogNumber=0)

Get message intents for a given dialog number :param message: Message Text :param dialogNumber: The current dialog number :return: list of [{ “name”: classifier name “confidence”: Classifier confidence “dialogNumber”: Current dialog number “rank”: Classifier rank }]

intentSamplesAugment()

Function to augment intent samples ,making sure each intent is at least 100 samples long

substituteAllEntitiesInAllTrainingExamples()

For certain types of classifiers it is helpful to have entities substituted Whenever a new intent is trained , all the entitiy values in the message texts get replaced by the entity kind values. This is to help training the intents . Note that you can define certain entities to help increase the accuracy of intent

class simbots.Intent.MultinomialNBIntent(intentName='Irrelevant', allIntentExamples=None, entityHandler=None, vocab=None)

Bases: simbots.Intent.Intent

Uses Multinomial Naive bayes classifier for intent training.

createDataSetForIntent()

Creates the data set for the intent model :return: X_train: training sentences asn list X_test: testing sentences as list X : all sentences as list y_train: train set labels y_test: testset labels y : the complete sample labels (including the training and the testing sets)

createIntent(clfName=None)
Parameters

clfName – Classifier Name , if not provided , will default to intentName+’_multinomialNbClassifier’

Returns

classifier

classifier = {

“name”: Classifier name, “classifier”: Classifier Object, “expectedAccuracy”: Training accuracy achieved by the intent, “data”: {

“countVect”: count vectoriser , “trainedOnX”: Training samples, “trainedOnY”: Training labels

}

createVocab()

This function creates the vocab for intent and updates the intent vocabulary :return: count_vect : This returns a fitted Countvectorizer object

getIntentProbability(userMessage)

Get the intent probability :param userMessage: Text message from the user :return: { “name”: classifier name “confidence”: classifier confidence }

static predictClassifierOutput(clf, X, count_vect)

Make predictions on the trained intent :param clf: trained classifier :param X: The sample for prediction :param count_vect: count vectoriser for the classifier :return:

predictions: predicted class probabilities: probablities for the predicted class

static train(X_train, y_train, params=None)

Train the intent model

Parameters
  • X_train – Training sentences

  • y_train – Training labels

  • params – None

Returns

trained classifier

Module contents