def encode_bayesian_property_addition(chosen_properties, subj, action, obj, context): generating_fillers = {} generating_fillers['subject'] = subj generating_fillers['action'] = action generating_fillers['object'] = obj subject_vector = subj['identity'] action_vector = action['identity'] object_vector = obj['identity'] subject_props = {} object_props = {} chosen_props = {} # arrays of property strings chosen_props['subject'] = [] chosen_props['object'] = [] # probabilistically choose properties of subject, limited to properties that the subject actually possesses for prop in subj['properties'].keys(): subject_props[prop] = context.distribution_params[action_from_vector( context, action)]['subject'][prop][0] / ( context.distribution_params[action_from_vector( context, action)]['subject'][prop][0] + context.distribution_params[action_from_vector( context, action)]['subject'][prop][1]) total_subject = sum(subject_props.values()) for prop in subject_props.keys(): if random.random() < min(subject_props[prop], subject_props[prop] / (total_subject / 1.)): subject_vector += context.properties[prop][int( subj['properties'][prop])] chosen_props['subject'].append(prop) # probabilistically choose properties of object, limited to properties that the object actually possesses for prop in obj['properties'].keys(): object_props[prop] = context.distribution_params[action_from_vector( context, action)]['object'][prop][0] / ( context.distribution_params[action_from_vector( context, action)]['object'][prop][0] + context.distribution_params[action_from_vector( context, action)]['object'][prop][1]) total_object = sum(object_props.values()) for prop in object_props.keys(): if random.random() < min(object_props[prop], object_props[prop] / (total_object / 1.)): object_vector += context.properties[prop][int( obj['properties'][prop])] chosen_props['object'].append(prop) chosen_properties.append(chosen_props) vector = normalize( encode(context.roles['subject'], subject_vector) + encode(context.roles['action'], action_vector) + encode(context.roles['object'], object_vector)) return vector, generating_fillers
def encode_baseline(subj, action, obj, context): generating_fillers = {} generating_fillers['subject'] = subj generating_fillers['action'] = action generating_fillers['object'] = obj subject_vector = subj['identity'] action_vector = action['identity'] object_vector = obj['identity'] vector = normalize( encode(context.roles['subject'], subject_vector) + encode(context.roles['action'], action_vector) + encode(context.roles['object'], object_vector)) return vector, generating_fillers
def encode_all_property_addition(subj, action, obj, context): generating_fillers = {} generating_fillers['subject'] = subj generating_fillers['action'] = action generating_fillers['object'] = obj subject_vector = subj['identity'] action_vector = action['identity'] object_vector = obj['identity'] for p in subj['properties'].keys(): subject_vector += context.properties[p][int(subj['properties'][p])] for p in action['properties'].keys(): action_vector += context.properties[p][int(action['properties'][p])] for p in obj['properties'].keys(): object_vector += context.properties[p][int(obj['properties'][p])] vector = normalize( encode(context.roles['subject'], subject_vector) + encode(context.roles['action'], action_vector) + encode(context.roles['object'], object_vector)) return vector, generating_fillers
def encode_selective_property_addition(subj, action, obj, context, subject_property=None, action_property=None, object_property=None): generating_fillers = {} generating_fillers['subject'] = subj generating_fillers['action'] = action generating_fillers['object'] = obj subject_vector = subj['identity'] action_vector = action['identity'] object_vector = obj['identity'] if subject_property != None and subject_property in subj[ 'properties'].keys(): subject_vector += context.properties[subject_property][int( subj['properties'][subject_property])] generating_fillers['subject_property'] = subject_property else: generating_fillers['subject_property'] = None if action_property != None and action_property in action[ 'properties'].keys(): action_vector += context.properties[action_property][int( action['properties'][action_property])] generating_fillers['action_property'] = action_property else: generating_fillers['action_property'] = None if object_property != None and object_property in obj['properties'].keys(): object_vector += context.properties[object_property][int( obj['properties'][object_property])] generating_fillers['object_property'] = object_property else: generating_fillers['object_property'] = None vector = normalize( encode(context.roles['subject'], subject_vector) + encode(context.roles['action'], action_vector) + encode(context.roles['object'], object_vector)) return vector, generating_fillers
def fillers_to_baseline(fillers, context): vector = normalize( encode(context.roles['subject'], fillers['subject']['identity']) + encode(context.roles['action'], fillers['action']['identity']) + encode(context.roles['object'], fillers['object']['identity'])) return vector