allennlp.semparse.executors

Executors are classes that deterministically transform programs in domain specific languages into denotations. We have one executor defined for each language-domain pair that we handle.

class allennlp.semparse.executors.wikitables_sempre_executor.WikiTablesSempreExecutor(table_directory: str) → None[source]

Bases: object

This class evaluates lambda-DCS logical forms by calling out to SEMPRE, where the particular lambda-DCS language we use was defined. It’s a huge pain to have to rely on a call to a java subprocess, but it’s way easier than trying to write our own executor for the language.

Because of how the SEMPRE executor works, we need to have access to the original table on disk, and we need to pass in the full lisp “example” string that’s given in the dataset. SEMPRE parses the example string (which includes a path to the table), reads the table, executes the logical form in the context of the table, and compares the answer produced to the answer specified in the example string.

We don’t even get back the denotation of the logical form here, because then we’d have to do a comparison with the correct answer, and that’s a bit messy - better to just let SEMPRE do it. This is why we only provide a evaluate_logical_form() method that returns a bool instead of an execute method returning an answer. You might think that if we got the answer back, we could at least use this for a demo. The sad thing is that even that doesn’t work, because this executor relies on having the table for the example already accessible on disk, which we don’t have in the case of a demo - we have to do extra stuff there to get it to work, including writing the table to disk so that SEMPRE can access it! It’s all a bit of a mess.

evaluate_logical_form(logical_form: str, example_lisp_string: str) → bool[source]
class allennlp.semparse.executors.sql_executor.SqlExecutor(database_file: str) → None[source]

Bases: object

This class evaluates SQL queries by connecting to a SQLite database. Because SQLite is disk-based we just need to provide one file with the location. We execute the predicted SQL query and the labeled queries against the database and check if they execute to the same table.

evaluate_sql_query(predicted_sql_query: str, sql_query_labels: typing.List[str]) → int[source]
static postprocess_query_sqlite(query: str)[source]
class allennlp.semparse.executors.wikitables_variable_free_executor.Date(year: int, month: int, day: int) → None[source]

Bases: object

class allennlp.semparse.executors.wikitables_variable_free_executor.WikiTablesVariableFreeExecutor(table_data: typing.List[typing.Dict[str, str]]) → None[source]

Bases: object

Implements the functions in the variable free language we use, that’s inspired by the one in “Memory Augmented Policy Optimization for Program Synthesis with Generalization” by Liang et al.

Parameters:
table_data : List[Dict[str, str]]

All the rows in the table on which the executor will be used. The class expects each row to be represented as a dict from column names to corresponding cell values.

argmax(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows and a column name and returns a list containing a single row (dict from columns to cells) that has the maximum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of _select and _all_rows.

argmin(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows and a column and returns a list containing a single row (dict from columns to cells) that has the minimum numerical value in the given column. We return a list instead of a single dict to be consistent with the return type of _select and _all_rows.

average(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → float[source]

Takes an expression list that evaluates to a list of rows and a column, and returns the mean of the values under that column in those rows.

count(row_expression_list: typing.List[typing.Union[str, typing.List]]) → float[source]

Takes an expression that evaluates to a a list of rows and returns their count (as a float to be consistent with the other functions like max that also return numbers).

static date(year_string: str, month_string: str, day_string: str) → allennlp.semparse.executors.wikitables_variable_free_executor.Date[source]

Takes three numbers as strings, and returns a Date object whose year, month, and day are the three numbers in that order.

diff(first_row_expression_list: typing.List[typing.Union[str, typing.List]], second_row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → float[source]

Takes an expressions that evaluate to two rows, and a column name, and returns the difference between the values under that column in those two rows.

evaluate_logical_form(logical_form: str, target_list: typing.List[str]) → bool[source]

Takes a logical form, and the list of target values as strings from the original lisp string, and returns True iff the logical form executes to the target list.

execute(logical_form: str) → typing.Any[source]
filter_date_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a numerical value and returns all the rows where the value in that column equals the given value.

filter_date_greater(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows as an expression, a column, and a numerical value expression and returns all the rows where the value in that column is greater than the given value.

filter_date_greater_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows as an expression, a column, and a numerical value expression and returns all the rows where the value in that column is greater than or equal to the given value.

filter_date_lesser(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows as an expression, a column, and a numerical value expression and returns all the rows where the value in that column is less than the given value.

filter_date_lesser_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a numerical value and returns all the rows where the value in that column is lesser than or equal to the given value.

filter_date_not_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a numerical value and returns all the rows where the value in that column is not equal to the given value.

filter_in(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a string value and returns all the rows where the value in that column contains the given string.

filter_not_in(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a string value and returns all the rows where the value in that column does not contain the given string.

filter_number_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a numerical value and returns all the rows where the value in that column equals the given value.

filter_number_greater(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows as an expression, a column, and a numerical value expression and returns all the rows where the value in that column is greater than the given value.

filter_number_greater_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows as an expression, a column, and a numerical value expression and returns all the rows where the value in that column is greater than or equal to the given value.

filter_number_lesser(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows as an expression, a column, and a numerical value expression and returns all the rows where the value in that column is less than the given value.

filter_number_lesser_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a numerical value and returns all the rows where the value in that column is lesser than or equal to the given value.

filter_number_not_equals(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str, value_expression: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes a list of rows, a column, and a numerical value and returns all the rows where the value in that column is not equal to the given value.

first(row_expression_list: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes an expression that evaluates to a list of rows, and returns the first one in that list.

last(row_expression_list: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes an expression that evaluates to a list of rows, and returns the last one in that list.

max(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → float[source]

Takes an expression list that evaluates to a list of rows and a column name, and returns the max of the values under that column in those rows.

min(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → float[source]

Takes an expression list that evaluates to a list of rows and a column, and returns the min of the values under that column in those rows.

mode(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → typing.List[str][source]

Takes an expression that evaluates to a list of rows, and a column and returns the most frequent values (one or more) under that column in those rows.

next(row_expression_list: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes an expression that evaluates to a single row, and returns the row (as a list to be consistent with the rest of the API), that occurs after the input row in the original set of rows. If the input row happens to be the last row, we will return an empty list.

previous(row_expression_list: typing.List[typing.Union[str, typing.List]]) → typing.List[typing.Dict[str, str]][source]

Takes an expression that evaluates to a single row, and returns the row (as a list to be consistent with the rest of the API), that occurs before the input row in the original set of rows. If the input row happens to be the top row, we will return an empty list.

same_as(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → typing.List[typing.Dict[str, str]][source]

Takes an expression that evaluates to a row, and a column and returns a list of rows from the full set of rows that contain the same value under the given column as the given row.

select(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name: str) → typing.List[str][source]

Select function takes a list of rows and a column name and returns a list of cell values as strings.

sum(row_expression_list: typing.List[typing.Union[str, typing.List]], column_name) → float[source]

Takes an expression list that evaluates to a list of rows and a column, and returns the sum of the values under that column in those rows.

class allennlp.semparse.executors.nlvr_executor.NlvrExecutor(boxes: typing.Set[allennlp.semparse.worlds.nlvr_box.Box]) → None[source]

Bases: object

Executor for logical forms in the language we wrote for the NLVR domain. The top level function is an assertion function (see below). We just pass the logical form parsed into a list to _execute_assertion and let the method deal with it. This is because the dataset contains sentences (instead of questions), and they evaluate to either true or false.

The language we defined here contains six types of functions, five of which return sets, and one returns booleans.

1) Assertion Function : These occur only at the root node of the logical form trees. They take a set of entities, and compare their attributes to a given value, and return true or false. The entities they take can be boxes or objects. If the assertion function takes objects, it may compare their colors or shapes with the given value; If it takes boxes, the attributes it compares are only the counts. The comparison operator can be any of equals, not equals, greater than, etc. So, the function specifies what kind of entities it takes, the attribute being compared and the comparison operator. For example, “object_count_not_equals” takes a set of objects, compares their count to the given value and returns true iff they are not equal. They have names like “object_*” or “box_*”

2) Object Attribute Functions: They take sets of objects and return sets of attributes. color and shape are the attribute functions.

3) Box Membership Function : This takes a box as an argument and returns the objects in it. This is a special kind of attribute function for two reasons. Firstly, it returns a set of objects instead of attributes, and secondly it occurs only within the second argument of a box filtering function (see below). It provides a way to query boxes based on the attributes of objects contained within it. The function is called object_in_box, and it gets executed within _execute_box_filter.

4) Box Filtering Functions : These are of the form filter(set_of_boxes, attribute_function, target_attribute) The idea is that we take a set of boxes, an attribute function that extracts the relevant attribute from a box, and a target attribute that we compare against. The logic is that we execute the attribute function on each of the given boxes and return only those whose attribute value, in comparison with the target attribute, satisfies the filtering criterion (i.e., equal to the target, less than, greater than etc.). The fitering function defines the comparison operator. All the functions in this class with names filter_* belong to this category.

5) Object Filtering Functions : These are of the form filter(set_of_objects). These are similar to box filtering functions, but they operate on objects instead. Also, note that they take just one argument instead of three. This is because while box filtering functions typically query complex attributes, object filtering functions query the properties of the objects alone. These are simple and finite in number. Thus, we essentially let the filtering function define the attribute function, and the target attribute as well, along with the comparison operator. That is, these are functions like black (which takes a set of objects, and returns those whose “color” (attribute function) “equals” (comparison operator) “black” (target attribute)), or “square” (which returns objects that are squares).

6) Negate Object Filter : Takes an object filter and a set of objects and applies the negation of the object filter on the set.

above(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]

Returns the set of objects in the same boxes that are above the given objects. That is, if the input is a set of two objects, one in each box, we will return a union of the objects above the first object in the first box, and those above the second object in the second box.

below(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]

Returns the set of objects in the same boxes that are below the given objects. That is, if the input is a set of two objects, one in each box, we will return a union of the objects below the first object in the first box, and those below the second object in the second box.

classmethod big(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod black(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod blue(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
bottom(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]

Return the bottom most objects(i.e. maximum y_loc). The comparison is done separately for each box.

classmethod circle(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
execute(logical_form: str) → bool[source]
classmethod medium(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
static negate_filter(filter_function: typing.Callable[typing.Set[allennlp.semparse.worlds.nlvr_object.Object], typing.Set[allennlp.semparse.worlds.nlvr_object.Object]], objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
static object_in_box(box: typing.Set[allennlp.semparse.worlds.nlvr_box.Box]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod same_color(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]

Filters the set of objects, and returns those objects whose color is the most frequent color in the initial set of objects, if the highest frequency is greater than 1, or an empty set otherwise.

This is an unusual name for what the method does, but just as blue filters objects to those that are blue, this filters objects to those that are of the same color.

classmethod same_shape(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]

Filters the set of objects, and returns those objects whose color is the most frequent color in the initial set of objects, if the highest frequency is greater than 1, or an empty set otherwise.

This is an unusual name for what the method does, but just as triangle filters objects to those that are triangles, this filters objects to those that are of the same shape.

classmethod small(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod square(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
top(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]

Return the topmost objects (i.e. minimum y_loc). The comparison is done separately for each box.

classmethod touch_bottom(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod touch_corner(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod touch_left(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
touch_object(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]

Returns all objects that touch the given set of objects.

classmethod touch_right(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod touch_top(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod touch_wall(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod triangle(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]
classmethod yellow(objects: typing.Set[allennlp.semparse.worlds.nlvr_object.Object]) → typing.Set[allennlp.semparse.worlds.nlvr_object.Object][source]