def __str__(self): """ Returns a string representation of the double. :return: the string representation """ return StringUtils.get_short_form(self._value)
def fill_slots(self, assignment): """ Fills the slots of the template, and returns the result of the function evaluation. If the function is not a simple arithmetic expression, """ filled = super(ArithmeticTemplate, self).fill_slots(assignment) if '{' in filled: return filled if ArithmeticTemplate.is_arithmetic_expression(filled): try: return StringUtils.get_short_form( MathExpression(filled).evaluate()) # TODO: need to check exception handling except Exception as e: self.log.warning("cannot evaluate " + filled) return filled # handling expressions that manipulate sets # (using + and - to respectively add/remove elements) merge = ValueFactory.none() for str_val in filled.split("+"): negations = str_val.split("-") merge = merge.concatenate(ValueFactory.create(negations[0])) for negation in negations[1:]: values = merge.get_sub_values() old_value = ValueFactory.create(negation) if old_value in values: values.remove(ValueFactory.create(negation)) merge = ValueFactory.create(values) return str(merge)
def _get_text_rendering(self, table): """ Generates the text representation for the categorical table. :param table: the table :return: the text rendering of the table """ text_table = '' base_variable = table.get_variable().replace("'", '') if base_variable == self._system.get_settings().user_input: text_table += '\n[user]\t' elif base_variable == self._system.get_settings().system_output: text_table += '[system]\t' else: text_table += '[' + base_variable + ']\t' for value in table.get_values(): if not isinstance(value, NoneVal): content = str(value) if table.get_prob(value) < 0.98: content += ' (' + StringUtils.get_short_form( table.get_prob(value)) + ')' text_table += content + '\n\t\t' if base_variable == self._system.get_settings().user_input: text_table += '\n' text_table = text_table[0:-3] return text_table
def perform_turn(self, system_action): """ Performs the dialogue turn in the simulator. :param system_action: the last system action. """ with self._lock: turn_performed = False self.simulator_state.set_parameters(self.domain.get_parameters()) system_assign = Assignment(self.system.get_settings().system_output, system_action) self.simulator_state.add_to_state(system_assign) while len(self.simulator_state.get_new_variables()) > 0: to_process = self.simulator_state.get_new_variables() self.simulator_state.reduce() for model in self.domain.get_models(): if model.is_triggered(self.simulator_state, to_process): change = model.trigger(self.simulator_state) if change and model.is_blocking(): break if len(self.simulator_state.get_utility_node_ids()) > 0: reward = self.simulator_state.query_util() comment = 'Reward: ' + StringUtils.get_short_form(reward) self.system.display_comment(comment) self.system.get_state().add_evidence(Assignment('R(' + system_assign.add_primes() + ')', reward)) self.simulator_state.remove_nodes(self.simulator_state.get_utility_node_ids()) if self.add_new_observations(): turn_performed = True self.simulator_state.add_evidence(self.simulator_state.get_sample()) return turn_performed
def __str__(self): """ Returns a string representation of the array. :return: the string representation of the array """ return '[' + ','.join( [StringUtils.get_short_form(d) for d in self._value]) + ']'
def generate_xml(self): element_list = [] for point, prob in self._points.items(): value_node = Element('value') value_node.set('prob', StringUtils.get_short_form(prob)) value_node.text(str(ValueFactory.create(prob))) element_list.append(value_node) return element_list
def __str__(self): """ Return a pretty print for the kernel density :return: the KDE string representation """ result = 'KDE(mean=[' mean = self.get_mean() mean_str = list() for mean_value in mean: mean_str.append(StringUtils.get_short_form(mean_value)) result += ', '.join(mean_str) result += ']), std=' average_std = 0. for std in self.get_variance(): average_std += math.sqrt(std) result += StringUtils.get_short_form(average_std / len(mean)) result += ') with ' + str(len(self._points)) + ' kernels' return result
def generate_xml(self): """ Generates the XML representation for the table, for the document doc. :param doc: the XML document for which to generate the XML. :return: XML reprensetation """ var = Element("variable") var.set("id", self._variable.replace("'", "")) for v in InferenceUtils.get_n_best(self._table, len(self._table)).keys(): if v != ValueFactory.none(): value_node = Element("value") if self._table[v] < 0.99: value_node.set("prob", StringUtils.get_short_form(self._table[v])) value_node.text = str(v) var.append(value_node) return var
def generate_xml(self): distrib_element = Element('distrib') distrib_element.set('type', 'gaussian') mean_element = Element('mean') mean_element.text(str(ValueFactory.create(self._mean)) if len(self._mean) > 1 else str(StringUtils.get_short_form(self._mean[0]))) distrib_element.append(mean_element) variance_element = Element('variance') variance_element.text(str(ValueFactory.create(self._variance)) if len(self._variance) > 1 else str(StringUtils.get_short_form(self._variance[0]))) distrib_element.append(variance_element) return [distrib_element]