interpreter module¶
-
interpreter.interpreter.
binary_search_actions
(target, pool)[source]¶ Binary search to find a
target
word in apool
of possibilities.This binary search is used to find a target word with fast performance even with a large search pool. It is implemented recursively to provide a simpler implementation. It assumes that the known actions list is sorted in descending (A to Z) order.
Parameters: - target (str) – The word to search for
- pool (list) – A list of tuples
(str, int)
, each containing the string of a known action and its action set index
Returns: The action set index of the
target
word if it is found in thepool
- Returns -1 if no match is found
Return type: int
-
interpreter.interpreter.
build_action_structures
(filename)[source]¶ MUST UPDATE FOR CREATING THE LIST OF FIRST ACTIONS Given a filename that contains a list of known actions, generates the data structures needed to interpret commands.
Opens a file of known actions, where each action set is represented on one line (see this part of the interpreter demo for details on the input file). For each word in this file, a tuple (str, int) is generated that contains that word along with its action set index. This tuple is then appended to the list of known actions. This list is sorted by A-Z order before it is returned to prepare it for binary search operations.
A list of object extractor functions is created to correspond to the action set indices be called later on. For each line, another function is added to the extractor function list. To determine the name of the extractor function to be added next, the first word in the action set is appended to the end of the string
"object_dict\_"
. Once the function name string is built, the function is retreived from the extractor module and is appended to the extractor function list.Parameters: filename (str) – The name of the file that contains the list of known actions Returns: A tuple (known_actions, object_extractor_functions)
containing:known_actions
- The list of tuples each containing a known action and its set indexobject_extractor_functions
- The list of object extractor functions whose list indices correspond with the appropriate action set indices
Return type: (list, list) Note
This function only needs to run when the input file is updated. It should not be run every time a new command needs to be interpreted.
-
interpreter.interpreter.
extract_action
(sent, known_actions)[source]¶ Finds the action that LILI can respond to, given a tokenized command sentence and a list of known actions.
The action is most likely one of the first couple of words of the command, so the sentence is processed from first word to last word. Each word in the sentence is searched for in the given list of known actions. The first word that is found in the known actions array is determined to be the action, and processing ends. A binary search algorithm is used to search for the word to keep processing time short even with large lists of known actions.
A tuple that contains two values is returned:
- The action’s action set index value
- The position of the found word in the sentence represented as an index value
Parameters: - sent (list) – A list containing the tokenized sentence
- known_actions (list) – A list of tuples
(str, int)
, each containing the string of a known action and its action set index
Returns: A tuple that contains the action’s set index and position in the command sentence
- Returns (-1, 0) if no action is found
Return type: (int, int)
-
interpreter.interpreter.
generate_json
(action, object_dict)[source]¶ Returns a JSON string representation of an object dictionary with an entry for the action’s set index
First adds the action and its set index to the object dictionary, then uses the
json
module to dump the final dictionary into a JSON string.Parameters: - action (int) – The set index of the action
- object_dict (dict) – The object dictionary to be converted to a JSON string
Returns: A JSON string representation of the object dictionary and the action set index
Return type: str
-
interpreter.interpreter.
generate_object_dict
(sent, action_tuple, object_extractor_functions)[source]¶ Creates an object dictionary according to the provided action.
Begins by part of speech tagging the sentence, then trimming the action out of the sentence so that it is not re-processed (depending on implementation details, the presence of the main action in the sentence may throw off results). Then calls the appropriate object extractor function to create the object dictionary. The called object extractor is determined by the action’s set index value.
Parameters: - sent (list) – A list containing tokens of the command sentence
- action_tuple (int, int) –
A tuple
(action_set_index, position_in_sent)
:action_set_index
- The action’s action set indexposition_in_sent
- An index representing the action’s position in the command sentence
- This tuple is in the same format as the return value of
extract_action()
Returns: An object dictionary for the command
Return type: dict
-
interpreter.interpreter.
interpret_sent
(sent_text)[source]¶ Implements the order of execution (pipeline) of interpreting a sentence - utilized for checking test cases
-
interpreter.interpreter.
preprocess_text
(text)[source]¶ Preprocesses a raw text sentence. Currently only tokenizes the sentence.
Used to do any preprocessing on a sentence in raw text. Currently, the only preprocessing operation is tokenizing the sentence into individual words and punctuation marks.
Parameters: sent (str) – A string containing a command sentence Returns: A list of preprocessed tokens from the sentence Return type: list Note
Do not do any part of speech tagging in this function. It is a compute-intensive task and may not be needed for all commands (i.e. erroneous situations).