Пример #1
0
def parse_output_file(filename, rel_number, states, path, class_name,
                      min_ver_support, class_1_tirp_file_name,
                      to_add_entities):
    """
    This function create TIRP list from KarmaLego output file.
    Output file structure: [0]TIRP_size [1]symbolNumber-symbolNumber-sym...- [2]rel.rel.rel... [3]mean_duration
    [4]mean_offset_from_start [5]mean_offset_from_end  [6]vertical_support
    [7]mean_horizontal_support [8]entity_id [9][start_time-end_time][10] duration [11]offset_from_start [12]offset_from_end
    :param filename:
    :param rel_number:
    :return: TIRPs list
    """
    if not input_validation(filename):
        return

    if rel_allen_seven is rel_number:
        relations_dict = {
            "<": "before",
            "m": "meets",
            "o": "overlaps",
            "f": "finished by",
            "c": "contains",
            "=": "equals",
            "s": "starts",
            "-": 7
        }
    else:
        print("Wrong number of relations")
        return

    TIRP_list = []
    lines = [line.rstrip('\n') for line in open(filename)]
    for i in range(0, len(lines) - 1):
        if i % 2 == 1:
            continue
        line_vector = lines[i].split()
        next_line = lines[i + 1]
        instances = []
        entities = list()
        TIRP_size = int(line_vector[0])
        symbols = list(filter(None, line_vector[1].split('-')))
        if states:
            for i in range(0, len(symbols)):
                symbol = states[symbols[i]]
                symbols[i] = symbol
        if TIRP_size > 1:
            index = 0
            relations = list(filter(None, line_vector[index + 2].split('.')))
            for r in range(0, len(relations)):
                relations[r] = relations_dict[relations[r]]
        else:
            relations = list()
            index = -1
        mean_duration = float(line_vector[index + 3])
        mean_offset_from_start = float(line_vector[index + 4])
        mean_offset_from_end = float(line_vector[index + 5])
        vertical_support = int(line_vector[index + 6])
        mean_horizontal_support = float(line_vector[index + 7])
        get_supporting_instances(entities,
                                 instances,
                                 line_vector,
                                 symbols,
                                 index=index,
                                 next_line=next_line)
        TIRP_obj = TIRP(tirp_size=TIRP_size,
                        symbols=symbols,
                        relation=relations,
                        supporting_instances=instances,
                        supporting_entities=entities,
                        vertical_support=vertical_support,
                        mean_horizontal_support=mean_horizontal_support,
                        mean_duration=mean_duration,
                        mean_offset_from_start=mean_offset_from_start,
                        mean_offset_from_end=mean_offset_from_end,
                        path=path,
                        min_vertical_support=min_ver_support)
        if class_name == 'class_0':
            class_1_tirp = find_tirp_in_class_1(path, TIRP_obj,
                                                class_1_tirp_file_name,
                                                to_add_entities)
            TIRP_obj.set_exist_in_class_0()
            if class_1_tirp:
                if not to_add_entities:
                    class_1_tirp = find_tirp_in_class_1(
                        path, TIRP_obj, class_1_tirp_file_name, True)
                TIRP_obj.set_class_1_properties(class_1_tirp)
        if not to_add_entities:
            TIRP_obj.set_supporting_instances(list())
            TIRP_obj.set_supporting_entitie(list())
        TIRP_list.append(TIRP_obj)
    return TIRP_list
Пример #2
0
def parse_TIRP(line1, line2, states, path, class_name, min_ver_support,
               class_1_tirp_file_name, second_class_output_file_name,
               to_add_entities):
    relations_dict = {
        "<": "before",
        "m": "meets",
        "o": "overlaps",
        "f": "finished by",
        "c": "contains",
        "=": "equals",
        "s": "starts",
        "-": 7
    }
    line_vector = line1.split()
    instances = []
    entities = list()
    TIRP_size = int(line_vector[0])
    symbols = list(filter(None, line_vector[1].split('-')))
    if states:
        for i in range(0, len(symbols)):
            symbol = states[symbols[i]]
            symbols[i] = symbol
    if TIRP_size > 1:
        index = 0
        relations = list(filter(None, line_vector[index + 2].split('.')))
        for r in range(0, len(relations)):
            relations[r] = relations_dict[relations[r]]
    else:
        relations = list()
        index = -1
    mean_duration = float(line_vector[index + 3])
    mean_offset_from_start = float(line_vector[index + 4])
    mean_offset_from_end = float(line_vector[index + 5])
    vertical_support = int(line_vector[index + 6])
    mean_horizontal_support = float(line_vector[index + 7])
    get_supporting_instances(entities=entities,
                             instances=instances,
                             line_vector=line_vector,
                             symbols=symbols,
                             index=index,
                             next_line=line2)
    TIRP_obj = TIRP(tirp_size=TIRP_size,
                    symbols=symbols,
                    relation=relations,
                    supporting_instances=instances,
                    supporting_entities=entities,
                    vertical_support=vertical_support,
                    mean_horizontal_support=mean_horizontal_support,
                    mean_duration=mean_duration,
                    mean_offset_from_start=mean_offset_from_start,
                    mean_offset_from_end=mean_offset_from_end,
                    path=path,
                    min_vertical_support=min_ver_support)
    if class_name == 'class_0' and second_class_output_file_name != 'File does not exist':
        TIRP_obj.set_exist_in_class_0()
        class_1_tirp = find_tirp_in_class_1(path, TIRP_obj,
                                            class_1_tirp_file_name,
                                            to_add_entities)
        if class_1_tirp:
            if not to_add_entities:
                class_1_tirp = find_tirp_in_class_1(path, TIRP_obj,
                                                    class_1_tirp_file_name,
                                                    True)
            TIRP_obj.set_class_1_properties(class_1_tirp)
    if not to_add_entities:
        TIRP_obj.set_supporting_instances(list())
        TIRP_obj.set_supporting_entitie(list())
    return TIRP_obj