def caption(self, predication, world): if predication.num_agreeing == 0: return None entity = predication.random_agreeing_entity() if self.attribute == 'shape': attribute = Attribute(predtype='shape', value=entity.shape.name) elif self.attribute == 'color': attribute = Attribute(predtype='color', value=entity.color.name) elif self.attribute == 'texture': attribute = Attribute(predtype='texture', value=entity.texture.name) if predication.contradictory(predicate=attribute): assert False elif not self.pragmatical_redundancy and predication.redundant( predicate=attribute): return None self.apply_caption_to_predication(caption=attribute, predication=predication) return attribute
def caption(self, predication, world): if predication.num_agreeing == 0: return None entity = choice( list( set((entity.shape.name, entity.color.name, entity.texture.name) for entity in predication.agreeing))) # predication.random_agreeing_entity() if world.meta[self.label] == 'shape': attribute = Attribute(predtype='shape', value=entity[0]) elif world.meta[self.label] == 'color': attribute = Attribute(predtype='color', value=entity[1]) elif world.meta[self.label] == 'texture': attribute = Attribute(predtype='texture', value=entity[2]) if predication.contradictory(predicate=attribute): raise NotImplementedError elif not self.pragmatical_redundancy and predication.num_entities > 1 and predication.redundant( predicate=attribute): raise NotImplementedError return None if not self.correct(caption=attribute, predication=predication): return None return attribute
def caption(self, predication, world): if predication.num_agreeing == 0: return None values = set() for entity in predication.agreeing: if self.attribute == 'shape' and entity.shape.name in self.shapes: values.add(entity.shape.name) elif self.attribute == 'color' and entity.color.name in self.colors: values.add(entity.color.name) elif self.attribute == 'texture' and entity.texture.name in self.textures: values.add(entity.texture.name) value = choice(list(values)) if self.attribute == 'shape': attribute = Attribute(predtype='shape', value=value) elif self.attribute == 'color': attribute = Attribute(predtype='color', value=value) elif self.attribute == 'texture': attribute = Attribute(predtype='texture', value=value) if predication.contradictory(predicate=attribute): raise NotImplementedError elif not self.pragmatical_redundancy and predication.num_entities > 1 and predication.implies( predicate=attribute): raise NotImplementedError return None if not self.correct(caption=attribute, predication=predication): return None return attribute
def caption(self, predication, world): if predication.num_agreeing == 0: return None entity = predication.random_agreeing_entity() attributes = list() for predtype in self.attributes: if predtype == 'shape': attributes.append( Attribute(predtype='shape', value=entity.shape.name)) elif predtype == 'color': attributes.append( Attribute(predtype='color', value=entity.color.name)) elif predtype == 'texture': attributes.append( Attribute(predtype='texture', value=entity.texture.name)) for n in range(len(attributes) - 1, -1, -1): if predication.contradictory(predicate=attributes[n]): assert False elif not self.pragmatical_redundancy and predication.num_entities > 1 and predication.redundant( predicate=attributes[n]): attributes.pop(n) entity_type = EntityType(attributes=attributes) entity_type.apply_to_predication(predication=predication) return entity_type
def caption(self, predication, world): if predication.num_agreeing == 0: return None entity = predication.random_agreeing_entity() attributes = dict() if 'shape' in self.attributes: attributes['shape'] = Attribute(predtype='shape', value=entity.shape.name) if 'color' in self.attributes: attributes['color'] = Attribute(predtype='color', value=entity.color.name) if 'texture' in self.attributes: attributes['texture'] = Attribute(predtype='texture', value=entity.texture.name) for predtype, attribute in list(attributes.items()): if predication.contradictory(predicate=attribute): assert False elif not self.pragmatical_redundancy and predication.redundant( predicate=attribute): attributes.pop(predtype) entity_type = EntityType(predicates=attributes) self.apply_caption_to_predication(caption=entity_type, predication=predication) return entity_type
def caption(self, predication, world): if predication.num_agreeing == 0: return None entities = dict() for entity in predication.agreeing: entity_attributes = list() for predtype in self.attributes: if predtype == 'shape' and entity.shape.name in self.shapes: entity_attributes.append(entity.shape.name) elif predtype == 'color' and entity.color.name in self.colors: entity_attributes.append(entity.color.name) elif predtype == 'texture' and entity.texture.name in self.textures: entity_attributes.append(entity.texture.name) else: break else: entity = tuple(entity_attributes) if entity in entities: entities[entity] += 1 else: entities[entity] = 1 entities = [entity for entity, count in entities.items() if count == 1] if len(entities) == 0: return None entity = choice(entities) attributes = list() for n, predtype in enumerate(self.attributes): if predtype == 'shape': attributes.append(Attribute(predtype='shape', value=entity[n])) elif predtype == 'color': attributes.append(Attribute(predtype='color', value=entity[n])) elif predtype == 'texture': attributes.append( Attribute(predtype='texture', value=entity[n])) for n in range(len(attributes) - 1, -1, -1): if predication.contradictory(predicate=attributes[n]): raise NotImplementedError elif not self.pragmatical_redundancy and predication.num_entities > 1 and predication.redundant( predicate=attributes[n]): raise NotImplementedError attributes.pop(n) entity_type = Selector(predtype='unique', scope=EntityType(attributes=attributes)) if not self.correct(caption=entity_type, predication=predication): return None return entity_type
def caption(self, predication, world): if predication.num_agreeing == 0: return None values = list() for entity in predication.agreeing: if self.attribute == 'shape': values.append(entity.shape.name) elif self.attribute == 'color': values.append(entity.color.name) elif self.attribute == 'texture': values.append(entity.texture.name) value = choice(values) if self.attribute == 'shape': attribute = Attribute(predtype='shape', value=value) elif self.attribute == 'color': attribute = Attribute(predtype='color', value=value) elif self.attribute == 'texture': attribute = Attribute(predtype='texture', value=value) if predication.contradictory(predicate=attribute): assert False elif not self.pragmatical_redundancy and predication.num_entities > 1 and predication.redundant(predicate=attribute): assert False return None attribute.apply_to_predication(predication=predication) return attribute
def caption(self, predication, world): if predication.num_agreeing == 0: return None entities = list() for entity in predication.agreeing: entity_attributes = list() for predtype in self.attributes: if predtype == 'shape' and entity.shape.name in self.shapes: entity_attributes.append(entity.shape.name) elif predtype == 'color' and entity.color.name in self.colors: entity_attributes.append(entity.color.name) elif predtype == 'texture' and entity.texture.name in self.textures: entity_attributes.append(entity.texture.name) else: break else: entity = tuple(entity_attributes) entities.append(entity) entity = choice(entities) attributes = list() for n, predtype in enumerate(self.attributes): if predtype == 'shape': attributes.append(Attribute(predtype='shape', value=entity[n])) elif predtype == 'color': attributes.append(Attribute(predtype='color', value=entity[n])) elif predtype == 'texture': attributes.append(Attribute(predtype='texture', value=entity[n])) for n in range(len(attributes) - 1, -1, -1): if predication.contradictory(predicate=attributes[n]): assert False elif not self.pragmatical_redundancy and predication.num_entities > 1 and predication.redundant(predicate=attributes[n]): assert False attributes.pop(n) entity_type = EntityType(attributes=attributes) entity_type.apply_to_predication(predication=predication) return entity_type
def incorrect(self, caption, predication, world): if self.incorrect_mode == 1: # random (existing) shape if self.existing_attribute: shapes = util.unique_list(entity.shape.name for entity in world.entities if entity.shape.name in self.shapes) else: shapes = self.shapes caption.value['shape'] = Attribute(predtype='shape', value=choice(shapes)) elif self.incorrect_mode == 2: # random (existing) color if self.existing_attribute: colors = util.unique_list(entity.color.name for entity in world.entities if entity.color.name in self.colors) else: colors = self.colors caption.value['color'] = Attribute(predtype='color', value=choice(colors)) elif self.incorrect_mode == 3: # random (existing) texture if self.existing_attribute: textures = util.unique_list( entity.texture.name for entity in world.entities if entity.texture.name in self.textures) else: textures = self.textures caption.value['texture'] = Attribute(predtype='texture', value=choice(textures)) elif self.incorrect_mode == 4: # random (existing) attributes if self.existing_attribute: shapes = util.unique_list(entity.shape.name for entity in world.entities if entity.shape.name in self.shapes) colors = util.unique_list(entity.color.name for entity in world.entities if entity.color.name in self.colors) textures = util.unique_list( entity.texture.name for entity in world.entities if entity.texture.name in self.textures) else: shapes = self.shapes colors = self.colors textures = self.textures if 'shape' in self.attributes: caption.value['shape'] = Attribute(predtype='shape', value=choice(shapes)) if 'color' in self.attributes: caption.value['color'] = Attribute(predtype='color', value=choice(colors)) if 'texture' in self.attributes: caption.value['texture'] = Attribute(predtype='texture', value=choice(textures)) self.apply_caption_to_predication(caption=caption, predication=predication) return True
def incorrect(self, caption, predication, world): if self.existing_attribute: assert len(caption.value) == 1 and caption.value[0].predtype == self.attribute if self.attribute == 'shape': attributes = list(set(entity.shape.name for entity in world.entities if entity.shape.name in self.attributes and entity.shape.name != caption.value[0].value)) elif self.attribute == 'color': attributes = list(set(entity.color.name for entity in world.entities if entity.color.name in self.attributes and entity.color.name != caption.value[0].value)) elif self.attribute == 'texture': attributes = list(set(entity.texture.name for entity in world.entities if entity.texture.name in self.attributes and entity.texture.name != caption.value[0].value)) if not self.existing_attribute or len(attributes) == 0: if self.attribute == 'shape': attributes = self.shapes elif self.attribute == 'color': attributes = self.colors elif self.attribute == 'texture': attributes = self.textures caption.value[0] = Attribute(predtype=self.attribute, value=choice(attributes)) return self.correct(caption=caption, predication=predication)
def caption(self, predication, world): scope = self.scope_captioner.caption(predication=predication, world=world) if scope is None: return None max_attribute = Attribute(predtype=self.predtype, value=self.value) scope_predication_copy = predication.copy(reset=True) self.scope_captioner.apply_caption_to_predication( caption=scope, predication=scope_predication_copy) if predication.contradictory(predicate=max_attribute, predication=scope_predication_copy): return None elif not self.pragmatical_redundancy and predication.redundant( predicate=max_attribute, predication=scope_predication_copy): return None predication.apply(predicate=max_attribute, predication=scope_predication_copy) scope.value[self.predtype] = max_attribute return scope
def incorrect(self, caption, predication, world): if self.incorrect_mode == 0: # random (existing) shape if self.existing_attribute: caption_shape = None for predicate in caption.value: if predicate.predtype == 'shape': caption_shape = predicate.value shapes = list( set(entity.shape.name for entity in world.entities if entity.shape.name in self.shapes and entity.shape.name != caption_shape)) if not self.existing_attribute or len(shapes) == 0: shapes = self.shapes if len(caption.value) == 0: caption.value.append( Attribute(predtype='shape', value=choice(shapes))) else: for n, predicate in enumerate(caption.value): if predicate.predtype == 'shape': caption.value[n] = Attribute(predtype='shape', value=choice(shapes)) elif self.incorrect_mode == 1: # random (existing) color if self.existing_attribute: caption_color = None for predicate in caption.value: if predicate.predtype == 'color': caption_color = predicate.value colors = list( set(entity.color.name for entity in world.entities if entity.color.name in self.colors and entity.color.name != caption_color)) if not self.existing_attribute or len(colors) == 0: colors = self.colors if len(caption.value) == 0: caption.value.append( Attribute(predtype='color', value=choice(colors))) else: for n, predicate in enumerate(caption.value): if predicate.predtype == 'color': caption.value[n] = Attribute(predtype='color', value=choice(colors)) elif self.incorrect_mode == 2: # random (existing) texture if self.existing_attribute: caption_texture = None for predicate in caption.value: if predicate.predtype == 'texture': caption_texture = predicate.value textures = list( set(entity.texture.name for entity in world.entities if entity.texture.name in self.textures and entity.texture.name != caption_texture)) if not self.existing_attribute or len(textures) == 0: textures = self.textures if len(caption.value) == 0: caption.value.append( Attribute(predtype='texture', value=choice(textures))) else: for n, predicate in enumerate(caption.value): if predicate.predtype == 'texture': caption.value[n] = Attribute(predtype='texture', value=choice(textures)) elif self.incorrect_mode == 3: # random (existing) attributes if self.existing_attribute: caption_shape = caption_color = caption_texture = None for predicate in caption.value: if predicate.predtype == 'shape': caption_shape = predicate.value elif predicate.predtype == 'color': caption_color = predicate.value elif predicate.predtype == 'texture': caption_texture = predicate.value shapes = list( set(entity.shape.name for entity in world.entities if entity.shape.name in self.shapes and entity.shape.name != caption_shape)) colors = list( set(entity.color.name for entity in world.entities if entity.color.name in self.colors and entity.color.name != caption_color)) textures = list( set(entity.texture.name for entity in world.entities if entity.texture.name in self.textures and entity.texture.name != caption_texture)) if not self.existing_attribute or len(shapes) == 0: shapes = self.shapes if not self.existing_attribute or len(colors) == 0: colors = self.colors if not self.existing_attribute or len(textures) == 0: textures = self.textures if len(caption.value) == 0: attribute = choice(self.valid_attributes) if attribute == 'shape': caption.value.append( Attribute(predtype='shape', value=choice(shapes))) elif attribute == 'color': caption.value.append( Attribute(predtype='color', value=choice(colors))) elif attribute == 'texture': caption.value.append( Attribute(predtype='texture', value=choice(textures))) else: for n, predicate in enumerate(caption.value): if predicate.predtype == 'shape': caption.value[n] = Attribute(predtype='shape', value=choice(shapes)) elif predicate.predtype == 'color': caption.value[n] = Attribute(predtype='color', value=choice(colors)) elif predicate.predtype == 'texture': caption.value[n] = Attribute(predtype='texture', value=choice(textures)) caption.apply_to_predication(predication=predication) return True