def remove_column_from_spec(self, attribute, remove_first: bool = False): """ Removes an attribute from the Vis's clause Parameters ---------- attribute : str attribute to be removed remove_first : bool, optional Boolean flag to determine whether to remove all instances of the attribute or only one (first) instance, by default False """ if (not remove_first): new_inferred = list( filter(lambda x: x.attribute != attribute, self._inferred_intent)) self._inferred_intent = new_inferred self._intent = new_inferred elif (remove_first): new_inferred = [] skip_check = False for i in range(0, len(self._inferred_intent)): if self._inferred_intent[ i].value == "": # clause is type attribute column_spec = [] column_names = self._inferred_intent[i].attribute # if only one variable in a column, columnName results in a string and not a list so # you need to differentiate the cases if isinstance(column_names, list): for column in column_names: if (column != attribute) or skip_check: column_spec.append(column) elif (remove_first): remove_first = True new_inferred.append(Clause(column_spec)) else: if column_names != attribute or skip_check: new_inferred.append(Clause(attribute=column_names)) elif (remove_first): skip_check = True else: new_inferred.append(self._inferred_intent[i]) self._intent = new_inferred self._inferred_intent = new_inferred
def parse(intent: List[Union[Clause, str]]) -> List[Clause]: """ Given the string description from a list of input Clauses (intent), assign the appropriate clause.attribute, clause.filter_op, and clause.value. Parameters ---------- intent : List[Clause] Underspecified list of lux.Clause objects. Returns ------- List[Clause] Parsed list of lux.Clause objects. """ if type(intent) != list: raise TypeError( "Input intent must be a list consisting of string descriptions or lux.Clause objects." "\nSee more at: https://lux-api.readthedocs.io/en/latest/source/guide/intent.html" ) import re # intent = ldf.get_context() new_context = [] #checks for and converts users' string inputs into lux specifications for clause in intent: valid_values = [] if isinstance(clause, list): valid_values = [] for v in clause: if type( v ) is str: # and v in list(ldf.columns): #TODO: Move validation check to Validator valid_values.append(v) temp_spec = Clause(attribute=valid_values) new_context.append(temp_spec) elif isinstance(clause, str): #case where user specifies a filter if "=" in clause: eqInd = clause.index("=") var = clause[0:eqInd] if "|" in clause: values = clause[eqInd + 1:].split("|") for v in values: # if v in ldf.unique_values[var]: #TODO: Move validation check to Validator valid_values.append(v) else: valid_values = clause[eqInd + 1:] # if var in list(ldf.columns): #TODO: Move validation check to Validator temp_spec = Clause(attribute=var, filter_op="=", value=valid_values) new_context.append(temp_spec) #case where user specifies a variable else: if "|" in clause: values = clause.split("|") for v in values: # if v in list(ldf.columns): #TODO: Move validation check to Validator valid_values.append(v) else: valid_values = clause temp_spec = Clause(attribute=valid_values) new_context.append(temp_spec) elif type(clause) is Clause: new_context.append(clause) intent = new_context # ldf._intent = new_context for clause in intent: if (clause.description): #TODO: Move validation check to Validator #if ((clause.description in list(ldf.columns)) or clause.description == "?"):# if clause.description in the list of attributes if any(ext in [">", "<", "=", "!="] for ext in clause.description ): # clause.description contain ">","<". or "=" # then parse it and assign to clause.attribute, clause.filter_op, clause.values clause.filter_op = re.findall(r'/.*/|>|=|<|>=|<=|!=', clause.description)[0] split_description = clause.description.split( clause.filter_op) clause.attribute = split_description[0] clause.value = split_description[1] if re.match(r'^-?\d+(?:\.\d+)?$', clause.value): clause.value = float(clause.value) elif (type(clause.description) == str): clause.attribute = clause.description elif (type(clause.description) == list): clause.attribute = clause.description # else: # then it is probably a value # clause.values = clause.description return intent # ldf._intent = intent