def get_descendents_slot_content_mappings(self) -> Set[SlotAssignment]: """ Gets all mappings from every slot from the template to all possible values, by following children until the leaves """ result = set() if len(self._children) == 0: return result for child in self._children: # Extract the slots to Tuple[TemplateElement] for this child assignment = self._template.extract_content(child._template) # Convert this to mapping of slot names to this child's List[TemplateElement] of the Template mapping: SlotAssignment = self._template.create_slot_mapping(assignment) # Get all of the descendent content mappings if child._template.has_slots(): child_descendents_mapping: Set[ SlotAssignment ] = child.get_descendents_slot_content_mappings() for child_map in child_descendents_mapping: new_mapping = SlotAssignment(mapping) new_mapping.update(child_map) result.add(new_mapping) else: result.add(mapping) return result
def test_get_all_slot_assignments(self): slot_values = SlotValues({ self.a: {Template([self.c]), self.e1}, self.c: self.e12 }) self.assertEqual( { SlotAssignment({self.c: self.e1}), SlotAssignment({self.c: self.e2}) }, set(slot_values.get_all_possible_assignments([self.c])), )
def get_descendent_leaves_slot_content_mappings(self) -> Set[SlotAssignment]: result = set() if len(self._children) == 0: return result for child in self._children: # Extract the slots to Tuple[TemplateElement] for this child assignment = self._template.extract_content(child._template) # Convert this to mapping of slot names to this child's List[TemplateElement] of the Template mapping: SlotAssignment = self._template.create_slot_mapping(assignment) # Get all of the descendent content mappings if child._template.has_slots(): child_descendents_mapping: Set[ SlotAssignment ] = child.get_descendent_leaves_slot_content_mappings() for child_map in child_descendents_mapping: new_mapping = SlotAssignment() for key in mapping.keys(): new_mapping[key] = mapping[key].fill(child_map) result.add(new_mapping) else: result.add(mapping) return result
def test_get_slot_content_mappings(self): self.assertEqual(set(), self.s1.get_slot_content_mappings()) slot1 = NamedTemplateSlot("x") slot2 = NamedTemplateSlot("y") a = TemplateString("a") b = TemplateString("b") c = TemplateString("c") # Simple tree simple_tree = TemplateTree( Template([a, slot1]), [TemplateTree(Template([a, b]), [])] ) simple_slot_contents = simple_tree.get_slot_content_mappings() self.assertEqual(1, len(simple_slot_contents)) simple_slot_content = list(simple_slot_contents)[0] self.assertTrue(slot1 in simple_slot_content) self.assertTrue(slot1 in simple_slot_content.keys()) self.assertEqual(Template([b]), simple_slot_content[slot1]) self.assertEqual({SlotAssignment({slot1: Template([b])})}, simple_slot_contents) # Two slot tree two_slot_tree = TemplateTree( Template([slot1, b, slot2]), [TemplateTree(Template([a, b, c]), [])] ) two_slot_tree_contents = two_slot_tree.get_slot_content_mappings() self.assertEqual( {SlotAssignment({slot1: Template([a]), slot2: Template([c])})}, two_slot_tree_contents, ) # Test tree u1_slot = self.u1.get_template().get_slots()[0] self.assertEqual( { SlotAssignment({u1_slot: Template([TemplateString("c")])}), SlotAssignment({u1_slot: Template([TemplateString("e")])}), }, self.u1.get_slot_content_mappings(), )
def fill_with_strings(self, strings: List[str]) -> "Template": slot_assignments = SlotAssignment() slots = self.get_slots() if len(slots) != len(strings): raise Exception("Can not fill in " + str(len(slots)) + " slots using " + str(len(strings)) + "strings: " + str(strings)) for i in range(len(slots)): slot_assignments[slots[i]] = Template.from_string(strings[i]) return self.fill(slot_assignments)
def create_slot_mapping_all(self, other: "Template") -> Set[SlotAssignment]: template_slots = self.get_slots() all_mappings = set() for tup in self.extract_content_all(other): mapping = SlotAssignment() for i in range(len(tup)): mapping[template_slots[i]] = tup[i] all_mappings.add(mapping) return all_mappings
def create_slot_mapping(self, tup: Tuple["Template"], template_slots=None) -> SlotAssignment: if template_slots is None: template_slots = self.get_slots() # The template slots should not have the same values assert len(template_slots) == len( set(template_slots) ), "Template slot has multiple slots with the same name. Use create_slot_values_mapping instead." assert len(template_slots) == len(tup) mapping = SlotAssignment() for i in range(len(tup)): mapping[template_slots[i]] = tup[i] return mapping
def get_all_possible_assignments( self, slots: Collection[TemplateSlot] = None ) -> Collection[SlotAssignment]: if slots is None: slots = list(self.keys()) tuples = self.get_all_possible_tuples(slots) result = [] for tup in tuples: slot_assignment = dict() for j in range(len(slots)): slot = slots[j] assert slot not in slot_assignment, ( "Multiple slots are occuring, please use get_all_possible_tuples instead:" + str(slot) + " in " + str(slots)) slot_assignment[slot] = tup[j] result.append(SlotAssignment(slot_assignment)) return result
def test_fill(self): a = self.a b = self.b c = self.c slot1 = self.slot1 slot2 = self.slot2 self.assertEqual( Template.from_string("b"), Template([slot1]).fill(SlotAssignment({slot1: Template([b])})), ) self.assertEqual( Template.from_string("a b"), Template([a, slot1]).fill(SlotAssignment({slot1: Template([b])})), ) self.assertEqual( Template.from_string("a b c"), Template([a, slot1, c]).fill(SlotAssignment({slot1: Template([b])})), ) self.assertEqual( Template.from_string("a b c a"), Template([a, slot1, c, slot2]).fill( SlotAssignment({ slot1: Template([b]), slot2: Template([a]) })), ) self.assertEqual( Template.from_string("a b a c"), Template([a, slot1, slot2, c]).fill( SlotAssignment({ slot1: Template([b]), slot2: Template([a]) })), ) self.assertEqual( Template.from_string("a b a c"), Template([a, slot1, slot2, c]).fill( SlotAssignment({ slot2: Template([a]), slot1: Template([b]) })), ) self.assertEqual( Template.from_string("a b a c"), Template([a, slot1, slot2, c]).fill_with_strings(["b", "a"]), )
def get_all_possible_tuples( self, slots: Collection[TemplateSlot] = None ) -> Collection[Tuple[Template, ...]]: if slots is None: slots = list(self.keys()) if len(slots) == 0: return SlotAssignment() lists = [list(self[s]) for s in slots] sizes = [len(l) for l in lists] total_possibilities = reduce(lambda x, y: x * y, sizes) slot_assignments = [] for i in range(total_possibilities): indices = get_indices(i, sizes) slot_assignment = [] for j in range(len(slots)): value = lists[j][indices[j]] slot_assignment.append(value) slot_assignments.append(tuple(slot_assignment)) return slot_assignments