text_game_maker.chatbot_utils package¶
Submodules¶
-
class
text_game_maker.chatbot_utils.redict.
ReDict
(*args, **kwargs)¶ Bases:
dict
Special dictionary which expects values to be set with regular expressions (REs) as keys, and expects values to be retreived using input text for an RE as keys. The value corresponding to the regular expression which matches the input text will be returned. In the case where the input text matches multiple REs, one of the matching values will be returned, but precisely which one is undefined.
Example usage:
>>> d = ReDict() >>> d['hello( world(!)*)?'] = 1 >>> d['regex|dict key'] = 2 >>> d['hello'] 1 >>> d['hello world!!!!'] 1 >>> d['regex'] 2 >>> d['dict key'] 2
-
__init__
(*args, **kwargs)¶ x.__init__(…) initializes x; see help(type(x)) for signature
-
clear
()¶ Clear all key/value pairs stored in this dict
-
compile
()¶ Compile all regular expressions in the dictionary
-
copy
()¶ Create a new ReDict instance and copy all items in this dict into the new instance
Returns: new ReDict instance containing copied data Return type: ReDict
-
dump_to_dict
()¶ Dump all pattern/value pairs to a regular dict, where the regular expressions are the keys
Returns: dict of pattern/value pairs Return type: dict
-
groups
()¶ Return tuple of all subgroups from the last regex match performed when fetching an item, as returned by re.MatchObject.groups()
Returns: tuple of subgroups from last match Return type: tuple
-
items
()¶ Return all values stored in this dict
Returns: list of values Return type: list
-
iteritems
()¶ Returns a generator to get all key/value pairs stored in this dict
Returns: generator to get pattern/value pairs
-
keys
()¶ Return all keys stored in this dict
Returns: list of keys Return type: list
-
load_from_dict
(data)¶ Load pattern/value pairs from a regular dict. This overwrites any existing pattern/value pairs
Parameters: data (dict) – pattern/value pairs to load
-
pop
(text)¶ Return and delete the first value associated with a pattern matching ‘text’
Parameters: text (str) – text to match against Returns: value associated with pattern matching ‘text’ (if any)
-
update
(other)¶ Add items from ‘other’ into this dict
Parameters: other (ReDict) – dict containing items to copy
-
values
()¶ Return all values stored in this dict
Returns: list of values Return type: list
-
-
class
text_game_maker.chatbot_utils.responder.
Context
(lists=None)¶ Bases:
object
Class representing a “discussion” context, allowing for a Responder that responds with contextual awareness
-
__init__
(lists=None)¶ x.__init__(…) initializes x; see help(type(x)) for signature
-
add_chained_phrases
(*pattern_response_pairs)¶ Add multiple chained pattern/response pairs. A chain defines a sequence of pattern/response pairs that are expected in the order that they occur in the passed arguments to this method. Whenever a Responder is inside a context and input matching the first pattern/response pair in a chain is seen, the Responder will continually expect the next pattern in the current chain until another chain or another context is entered. When the last pattern in the chain is reached, Responders will continue expecting this pattern until another chain or context is entered.
Parameters: pattern_response_pairs – one or more pattern/response pairs, where a pattern/response pair is a tuple of the form (regexs, value)
, whereregexs
is a regular expression or list of regular expressions andvalue
is an arbitrary object
-
add_context
(context)¶ Add context that can only be entered when already in this context
Parameters: context (text_game_maker.chatbot_utils.responder.Context) – context instance to add
-
add_contexts
(*contexts)¶ Add one or more context instances to this context
Parameters: contexts (text_game_maker.chatbot_utils.responder.Context) – context instances to add
-
add_entry_phrase
(patterns, response)¶ Add a pattern/response pair to be used as an entry point for this context. If input matching matching one of the patterns passed here is seen, Responders will return the corresponding response object and enter the context.
Parameters: - patterns – regular expression or list of regular expressions. If the input passed to
get_response
matches one of these patterns, then the object passed here asresponse
will be returned. - response (object) – object to return from
get_response
if the passed input matches one of the regular expressions passed here asresponse
.
- patterns – regular expression or list of regular expressions. If the input passed to
-
add_entry_phrases
(*pattern_response_pairs)¶ Add one or more pattern/response pairs to be used as entry points for this context. If input matching matching one of the patterns passed here is seen, Responders will return the corresponding response object and enter the context.
Parameters: pattern_response_pairs – one or more pattern/response pairs, where a pattern/response pair is a tuple of the form (regexs, value)
, whereregexs
is a regular expression or list of regular expressions andvalue
is an arbitrary object
-
add_response
(patterns, response)¶ Add a pattern/response pair that will be only be recognized when a Responder is in this context
Parameters: - patterns – regular expression or list of regular expressions. If the input passed to
get_response
matches one of these patterns, then the object passed here asresponse
will be returned. - response (object) – object to return from
get_response
if the passed input matches one of the regular expressions passed here asresponse
.
- patterns – regular expression or list of regular expressions. If the input passed to
-
add_responses
(*pattern_response_pairs)¶ Add one more more pattern/response pairs that will be only be recognized when a Responder is in this context
Parameters: pattern_response_pairs – one or more pattern/response pairs, where a pattern/response pair is a tuple of the form (regexs, value)
, whereregexs
is a regular expression or list of regular expressions andvalue
is an arbitrary object
-
compile
()¶ Compile all regular expressions contained in this context so they are ready for immediate matching
-
get_response
(text)¶ Find a response object associated with a pattern in this context that matches ‘text’, and return it (if any). If no matching patterns can be found, ‘text’ itself will be returned.
Parameters: text (str) – input text to check for matching patterns against Returns: tuple of the form (response, groups)
.response
is the response object associated with the matching regular expression, if any, otherwise ‘text’.groups
is a tuple of subgroups from the regular expression match (as returned by re.MatchObject.groups), if any, otherwise None.
-
-
class
text_game_maker.chatbot_utils.responder.
NoResponse
¶ Bases:
object
-
class
text_game_maker.chatbot_utils.responder.
Responder
¶ Bases:
object
Represents a high-level responder object which can be used to register pattern/response pairs, and can accept input text to retrieve matching response objects
-
__init__
()¶ x.__init__(…) initializes x; see help(type(x)) for signature
-
add_context
(context)¶ Add context instance to this responder
Parameters: context (text_game_maker.chatbot_utils.responder.Context) – context instance to add
-
add_contexts
(*contexts)¶ Add one or more context instances to this responder
Parameters: contexts (text_game_maker.chatbot_utils.responder.Context) – context instances to add
-
add_default_response
(response)¶ Set response to return when no other matching responses can be found
Parameters: response – object to return as default response
-
add_response
(patterns, response)¶ Add a pattern/response pair that will always be recognized by a Responder, regardless of context
Parameters: - patterns (list) – list of regular expressions. If the input passed to
get_response
matches one of these patterns, then the object passed here asresponse
will be returned. - response (object) – object to return from
get_response
if the passed input matches one of the regular expressions passed here asresponse
.
- patterns (list) – list of regular expressions. If the input passed to
-
add_responses
(*pattern_response_pairs)¶ Add one or moe pattern/response pairs that will always be recognized by a Responder, regardless of context
Parameters: pattern_response_pairs – one or more pattern/response pairs, where a pattern/response pair is a tuple of the form (regexs, value)
, whereregexs
is a regular expression or list of regular expressions andvalue
is an arbitrary object
-
compile
()¶ Compile all regular expressions contained in this responder (including contexts), so they are ready for matching immediately
-
get_response
(text)¶ Find a response object associated with a pattern that matches ‘text’, and return it (if any). If no matching patterns can be found, ‘text’ itself will be returned.
Parameters: text (str) – input text to check for matching patterns against Returns: tuple of the form (response, groups)
.response
is the response object associated with the matching regular expression, if any, otherwise ‘text’.groups
is a tuple of subgroups from the regular expression match (as returned by re.MatchObject.groups), if any, otherwise None.
-