Exemple #1
0
def gen_split_spec(query_str):
    object_rel_split = query_str.split(';')
    objects = object_rel_split[0]
    relationships = object_rel_split[1]

    obj_nodes = []
    for obj in objects.split(','):
        obj_mat_struct = siom.mat_struct()
        obj_mat_struct.__setattr__('names', obj.strip())
        obj_nodes.append(obj_mat_struct)

    ob_list = np.array(obj_nodes)

    rel_nodes = []
    for three_part_rel in relationships.split(','):
        rel_components = three_part_rel.strip().split(' ')
        rel_struct = siom.mat_struct()
        rel_struct.__setattr__('subject', int(rel_components[0].strip()))
        rel_struct.__setattr__('predicate', rel_components[1].strip())
        rel_struct.__setattr__('object', int(rel_components[2].strip()))
        rel_nodes.append(rel_struct)

    rel_list = np.array(rel_nodes)

    query_struct = siom.mat_struct()
    query_struct.__setattr__('objects', ob_list)
    query_struct.__setattr__('unary_triples', np.array([]))
    query_struct.__setattr__('binary_triples', rel_list)

    return query_struct
Exemple #2
0
def gen_sro(sub, pred, obj, use_attrs=False):
    sub_struct, obj_struct = siom.mat_struct(), siom.mat_struct()
    sub_struct.__setattr__('names', sub)
    obj_struct.__setattr__('names', obj)

    rel_struct = siom.mat_struct()
    rel_struct.__setattr__('subject', 0)
    rel_struct.__setattr__('object', 1)
    rel_struct.__setattr__('predicate', pred)

    query_struct = siom.mat_struct()
    query_struct.__setattr__(
        'objects', np.array([sub_struct, obj_struct]))
    query_struct.__setattr__('unary_triples', np.array([]))
    query_struct.__setattr__('binary_triples', rel_struct)
    return query_struct
Exemple #3
0
def gen_asrao(query_str):
    words = query_str.split()
    if len(words) != 5: return None

    obj_attr_struct = siom.mat_struct()
    obj_attr_struct.__setattr__('subject', 1)
    obj_attr_struct.__setattr__('predicate', 'is')
    obj_attr_struct.__setattr__('object', words[3])

    query_struct = gen_asro(' '.join([words[0], words[1], words[2], words[4]]))
    query_struct.__setattr__('unary_triples', obj_attr_struct)

    return query_struct
Exemple #4
0
def gen_asro(query_str):
    words = query_str.split()
    if len(words) != 4: return None

    sub_attr_struct = siom.mat_struct()
    sub_attr_struct.__setattr__('subject', 0)
    sub_attr_struct.__setattr__('predicate', 'is')
    sub_attr_struct.__setattr__('object', words[0])

    query_struct = gen_sro(' '.join([words[1], words[2], words[3]]))
    query_struct.__setattr__('unary_triples', sub_attr_struct)

    return query_struct
Exemple #5
0
def gen_sro(query_str):
    words = query_str.split()
    if len(words) != 3: return None

    sub_struct = siom.mat_struct()
    sub_struct.__setattr__('names', words[0])

    obj_struct = siom.mat_struct()
    obj_struct.__setattr__('names', words[2])

    rel_struct = siom.mat_struct()
    rel_struct.__setattr__('subject', 0)
    rel_struct.__setattr__('predicate', words[1])
    rel_struct.__setattr__('object', 1)

    det_list = np.array([sub_struct, obj_struct], dtype=np.object)
    query_struct = siom.mat_struct()
    query_struct.__setattr__('objects', det_list)
    query_struct.__setattr__('unary_triples', np.array([]))
    query_struct.__setattr__('binary_triples', rel_struct)

    return query_struct
Exemple #6
0
def convert_mat_7_3_to_struct(mat_file,
                              var_name,
                              verbose=False,
                              squeeze_me=True):

    result = mat_struct()

    with h5py.File(mat_file, 'r') as f:

        iter_struct(f[var_name],
                    result,
                    verbose=verbose,
                    squeeze_me=squeeze_me)

    return result
Exemple #7
0
def gen_srao(query_str):
    import scipy.io.matlab.mio5_params as siom

    words = query_str.split()
    if len(words) != 4: return None

    obj_attr_struct = siom.mat_struct()
    obj_attr_struct.__setattr__('subject', 1)
    obj_attr_struct.__setattr__('predicate', 'is')
    obj_attr_struct.__setattr__('object', words[2])

    query_struct = gen_sro(' '.join([words[0], words[1], words[3]]))
    query_struct.__setattr__('unary_triples', words[2])

    return query_struct
Exemple #8
0
def generate_queries_from_file(path, use_attrs=False):
    """Read queries from a specially-formatted file."""
    queries = []
    gen_dict = {'(sro)': gen_sro,
                '(srao)': gen_srao,
                '(asro)': gen_asro,
                '(asrao)': gen_asrao}
    with open(path) as f:
        for line in f.read().splitlines():
            query_struct = siom.mat_struct()
            parts = [part.replace('_', ' ') for part in line.split()]
            text_parts, gen_func = parts[:-1], gen_dict[parts[-1]]
            query_struct.annotations = gen_func(
                *text_parts, use_attrs=use_attrs)
            queries.append(query_struct)
    return queries
Exemple #9
0
def gen_three_obj_loop(query_str):
    words = query_str.split()

    if len(words) != 7: return None
    if words[0] != words[6]: return None

    # generate the object list
    ob1_struct = siom.mat_struct()
    ob1_struct.__setattr__('names', words[0])

    ob2_struct = siom.mat_struct()
    ob2_struct.__setattr__('names', words[2])

    ob3_struct = siom.mat_struct()
    ob3_struct.__setattr__('names', words[4])

    ob_list = np.array([ob1_struct, ob2_struct, ob3_struct])

    # generate the first relation
    rel1_struct = siom.mat_struct()
    rel1_struct.__setattr__('subject', 0)
    rel1_struct.__setattr__('predicate', words[1])
    rel1_struct.__setattr__('object', 1)

    # generate the second relation
    rel2_struct = siom.mat_struct()
    rel2_struct.__setattr__('subject', 1)
    rel2_struct.__setattr__('predicate', words[3])
    rel2_struct.__setattr__('object', 2)

    # generate the third relation
    rel3_struct = siom.mat_struct()
    rel3_struct.__setattr__('subject', 2)
    rel3_struct.__setattr__('predicate', words[5])
    rel3_struct.__setattr__('object', 0)

    # store the objects and relations
    rel_list = np.array([rel1_struct, rel2_struct, rel3_struct])

    query_struct = siom.mat_struct()
    query_struct.__setattr__('objects', ob_list)
    query_struct.__setattr__('unary_triples', np.array([]))
    query_struct.__setattr__('binary_triples', rel_list)

    return query_struct
Exemple #10
0
def iter_struct(parent, output, depth=0, verbose=False, squeeze_me=True):
    # iteratively walk through a v7.3 mat file and create a scipy mat_struct

    keys = list(parent)

    if verbose:
        print(depth, keys)

    for k in keys:

        if not k.startswith('#') and not k.startswith('_'):

            child = parent[k]

            if type(child) == h5py.Group:

                if verbose:
                    print("  group", depth, k, child)

                setattr(output, k, mat_struct())
                iter_struct(child,
                            getattr(output, k),
                            depth=depth + 1,
                            verbose=verbose)

            elif type(child) == h5py.Dataset:

                if verbose:
                    print("  data set", depth, k)

                if child.dtype != np.object:
                    if squeeze_me:
                        value = np.squeeze(np.asarray(child[()]))
                    else:
                        value = np.asarray(child[()])
                    setattr(output, k, value)
                else:
                    print("skipping mat file object: {}".format(k))
Exemple #11
0
def gen_two_rel_chain(query_str):
    words = query_str.split()
    if len(words) != 5: return None

    # generate the object list
    ob1_struct = siom.mat_struct()
    ob1_struct.__setattr__('names', words[0])

    ob2_struct = siom.mat_struct()
    ob2_struct.__setattr__('names', words[2])

    ob3_struct = siom.mat_struct()
    ob3_struct.__setattr__('names', words[4])

    ob_list = np.array([ob1_struct, ob2_struct, ob3_struct])

    # generate the first relation
    rel1_struct = siom.mat_struct()
    rel1_struct.__setattr__('subject', 0)
    rel1_struct.__setattr__('predicate', words[1])
    rel1_struct.__setattr__('object', 1)

    #rel1_objs = np.array([ob1_struct, ob2_struct], dtype=np.object)

    # generate the second relation
    rel2_struct = siom.mat_struct()
    rel2_struct.__setattr__('subject', 1)
    rel2_struct.__setattr__('predicate', words[3])
    rel2_struct.__setattr__('object', 2)

    #rel2_objs = np.array([ob2_struct, ob3_struct], dtype=np.object)

    # store the objects and relations
    rel_list = np.array([rel1_struct, rel2_struct])

    query_struct = siom.mat_struct()
    query_struct.__setattr__('objects', ob_list)
    query_struct.__setattr__('unary_triples', np.array([]))
    query_struct.__setattr__('binary_triples', rel_list)

    return query_struct
Exemple #12
0
def _get_unary_triple(attr, is_sub):
    attr_struct = siom.mat_struct()
    attr_struct.__setattr__('subject', 0 if is_sub else 1)
    attr_struct.__setattr__('predicate', 'is')
    attr_struct.__setattr__('object', attr)
    return attr_struct