def instantiate_periods()

in dvd_generation/simulators/question_generator.py [0:0]


def instantiate_periods(scene_struct, period_idx, synonyms, template, last_unique_obj, earlier_unique_obj, excluding): 
    cutoff = template['cutoff']
    e1 = scene_struct[intervals_to_periods[template['interval_type']]][period_idx][0]
    e2 = scene_struct[intervals_to_periods[template['interval_type']]][period_idx][1]
    assert template['used_periods'][-1] == (e1,e2)
    
    if e1 != None: 
        obj_identifier, obj_id = template['temporal_obj_attr'], template['temporal_obj_id'] 
        o1_program, o1 = instantiate_temporal_object(scene_struct, template, obj_identifier, obj_id,
                                                     synonyms, last_unique_obj, earlier_unique_obj)
        a1, a1_period = instantiate_action(synonyms, e1, scene_struct, cutoff)
        obj_text = [o1] + ["'s"] if o1!='its' else [o1]
        if 'start' in e1[1]:
            oa1 = ['from'] + obj_text + [a1]
        else:
            oa1 = ['after'] + obj_text + [a1]
    
    if e2 != cutoff:
        obj_identifier, obj_id = template['temporal_obj_attr'], template['temporal_obj_id']
        o2_program, o2 = instantiate_temporal_object(scene_struct, template, obj_identifier, obj_id,
                                                     synonyms, last_unique_obj, earlier_unique_obj)
        a2, a2_period = instantiate_action(synonyms, e2, scene_struct, cutoff)
        obj_text = [o2] + ["'s"] if o2!='its' else [o2]
        if 'end' in e2[1]:
            oa2 = ['to'] + obj_text + [a2]
        else:
            oa2 = ['before'] + obj_text + [a2] 
    
    if e1 is None and e2 == cutoff:  
        return [], random.choice(synonyms['whole video']) #, None, -1
    if e1 is None:
        side_inputs = [oa2[-1]]
        if 'start' in e2[1]: 
            answer_find_relate_period(a2_period, o2_program, 'before', side_inputs, last_idx=len(o2_program)-1)
        else:
            oa2[0] = 'until the end of'
            answer_find_relate_period(a2_period, o2_program, 'until', side_inputs, last_idx=len(o2_program)-1)
        out = oa2
        program = o2_program
    elif e2 == cutoff: # and cutoff is None: 
        side_inputs = [oa1[-1]]
        if 'end' in e1[1]: 
            answer_find_relate_period(a1_period, o1_program, 'after', side_inputs, last_idx=len(o1_program)-1)
        else:
            oa1[0] = 'since the start of'
            answer_find_relate_period(a1_period, o1_program, 'since', side_inputs, last_idx=len(o1_program)-1)
        out = oa1
        program = o1_program
    elif is_sample_action(e1, e2):
        out = oa1
        out[0] = 'during'
        answer_find_relate_period(a1_period, o1_program, 'during', [out[-1]], last_idx=len(o1_program)-1)
        program = o1_program
    else:
        if e2[0] == e1[0]: 
            original_len = len(o1_program)
            if 'start' in e1[1]:
                oa2 = ['to'] + ['its'] + [a2]
                out = ['inclusively ,'] + oa1 + oa2
                answer_find_relate_period(a1_period, o1_program, 'since', [oa1[-1]], last_idx=len(o1_program)-1)
                answer_find_relate_period(a2_period, o1_program, 'until', [oa2[-1]], last_idx=original_len-1)
                
                o1_program.append({'type': 'union_interval', 'inputs': [len(o1_program)-3, len(o1_program)-1], 
                           '_output': (a1_period[0], a2_period[1])})
            else:
                oa2 = ['before'] + ['its'] + [a2] 
                out = oa1 + ['and'] + oa2
                answer_find_relate_period(a1_period, o1_program, 'after', [oa1[-1]], last_idx=len(o1_program)-1)
                answer_find_relate_period(a2_period, o1_program, 'before', [oa2[-1]], last_idx=original_len-1)
                
                o1_program.append({'type': 'union_interval', 'inputs': [len(o1_program)-3, len(o1_program)-1], 
                           '_output': (a1_period[1], a2_period[0])})
            program = o1_program
        else:
            pdb.set_trace()
            if 'start' in e1[1]:
                out = ['inclusively ,'] + oa1 + oa2
            else:
                out = oa1 + ['and'] + oa2
    
    out = ' '.join(out)
    
    if last_unique_obj:
        if template['sampled_ans_object']!=-1:
            sampled_obj_program = []
            ans_obj_attr = template['sampled_ans_object_attr']
            attr_str = []
            sampled_obj_program.append({'type': 'refer_object', 'inputs': [], 
                                    'side_inputs': ['them'],
                                    '_output': template['prior_ans_object_group']})
            for name, val in ans_obj_attr.items():
                if val not in ['', 'thing']:
                    node_type = 'filter_{}'.format(attribute_to_text[name])
                    program_node = {'type': node_type, 'inputs': [len(sampled_obj_program)-1], 'side_inputs': [val]}
                    sampled_obj_program.append(program_node)
                if val in synonyms: 
                    val = random.choice(synonyms[val])
                attr_str.append(val)
            if '<S>' not in ans_obj_attr:
                attr_str.append('thing')
            modifier_text = 'Among them , there is a {} . '.format(' '.join(attr_str))
            
            sampled_obj_program.append({'type': 'unique', 'inputs': [len(sampled_obj_program)-1]})
            sampled_obj_program = answer_scene_obj_program(sampled_obj_program, scene_struct)
            assert sampled_obj_program[-1]['_output'] == template['sampled_ans_object']
            program = merge_sampled_obj_temporal_program(sampled_obj_program, program)
        else:
            modifier_text = '{} {} , '.format(random.choice(synonyms['<given>']), random.choice(synonyms['<it>']))
        out = modifier_text + out 
        
    return program, out