Beispiel #1
0
def resolve_compare_function(n1, n2, d=_dict):
    if (n1['type'], n2['type']) in d:

        v = d[(n1['type'], n2['type'])]
        if type(v) == dict:
            return resolve_compare_function(father(n1), father(n2), d=v)
        elif type(v) == list:
            return d[(n2['type'], n1['type'])]
        else:
            raise TypeError("Only nested dicts or lists are allowed")

    else:
        return []
    def _rarity_node_to_source(node):
        rar_i = []
        M = count['M']
        g = node['graph']

        while node['id'] != 'source':

            try:
                # Subtract __max__ key
                N = len(count['node'][node['type']]) - 1
                rar_i.append((M - N) / M)
            except KeyError:
                return None

            # Feature step
            father_node = father(node)
            linking_feature_type = g[father_node['id']][
                node['id']]['generating_function']

            try:
                N = count['feature'][linking_feature_type]
                rar_i.append((M - N) / M)
            except KeyError:
                return None

            node = father_node

        return rar_i
def trace(n):
    tr = []
    while True:
        tr.insert(0, n['type'])
        father_n = father(n)
        if father_n is not None:
            tr.insert(0, n['graph'][father_n['id']][n['id']]['type'])
            n = father_n
        else:
            break
    return tr
Beispiel #4
0
    def phrase_recursive(n, node_type_level, search_for):
        if search_for in node_type_level:
            return node_type_level[search_for]

        for k in node_type_level.keys():
            k_as_tuple = (k,) if type(k) == str else k
            if n['type'] in k_as_tuple:

                edge_type_level = node_type_level[k]

                if search_for in edge_type_level:
                    return edge_type_level[search_for]

                if father(n) == None:
                    return None
                else:
                    gen_function = n['graph'][father(n)['id']][n['id']]['generating_function']
                    for k in edge_type_level.keys():
                        k_as_tuple = (k,) if type(k) == str else k
                        if gen_function in k_as_tuple:

                            node_type_level = edge_type_level[k]
                            return phrase_recursive(father(n), node_type_level, search_for)
def shortness_score(segue):
    length = 0
    for node in [segue['n1'], segue['n2']]:
        node = segue['n1']
        while True:
            node_father = father(node)
            generating_function = node['graph'][node_father['id']][node['id']]['generating_function']
            if generating_function == 'init':
                length += 1
                break
            func = getattr(globals()[generating_function], generating_function)
            length += 1 if 'entailed' not in func.__annotations__ else 0
            node = node_father
    shortness = 2/length
    return shortness
    def _popularity_node_to_source(node):
        pop_i = []
        while node['id'] != 'source':

            id_node = craft_id_node_graph(node)

            if node['type'] in count['node']:

                try:
                    node_edgeset = count['node'][node['type']][id_node]
                except KeyError:
                    node_edgeset = np.inf

                meadian_edgeset_actual_type = count['node'][node['type']]['__meadian__']
                pop_i.append(min(1, node_edgeset/meadian_edgeset_actual_type))

            else:
                return None

            node = father(node)

        return pop_i
    def _popularity_node_to_source(node):
        pop_i = []
        while node['id'] != 'source':

            id_node = craft_id_node_graph(node)

            if node['type'] in count['node']:

                try:
                    node_edgeset = count['node'][node['type']][id_node]
                except KeyError:
                    node_edgeset = 0

                max_edgeset_actual_type = count['node'][
                    node['type']]['__max__']
                pop_i.append(node_edgeset / max_edgeset_actual_type)

            else:
                return None

            node = father(node)

        return sum(pop_i) / len(pop_i)
Beispiel #8
0
def get_template_relationship(n):
    with open(f"{preprocessed_dataset_path}/textual_templates_artist_relationships.json") as f:
        d = json.load(f)
        d = {k: inner_dict[k] for inner_dict in d.values() for k in inner_dict.keys()}
    template = d[n['graph'][father(n)['id']][n['id']]['type']]['preprocessed']
    return template
Beispiel #9
0
    def eligible_nodes_filter(self, g, node, func):
        """Used to eventually filter out nodes that would otherwise be considered as applicable to some features.
        Useful to limit the graph growth.

        Active filters:
        -avoid infinite loops
        -do not expand nodes that originate from an artist relationship
        -if the feature is entailment, then apply to a synset only if it directly discend from a word
        -if the feature is member_holonyms, then apply to a synset only if it directly discend from a word
        -if the feature is member_meronyms, then apply to a synset only if it directly discend from a word
        -if the feature is part_holonyms, then apply to a synset only if it directly discend from a word
        -if the feature is part_meronyms, then apply to a synset only if it directly discend from a word
        -if the feature is substance_holonyms, then apply to a synset only if it directly discend from a word
        -if the feature is substance_meronyms, then apply to a synset only if it directly discend from a word
        -if the feature is synset_also_sees, then apply to a synset only if it directly discend from a word
        -if the feature is synset_attributes, then apply to a synset only if it directly discend from a word
        -if the feature is synset_similar_tos, then apply to a synset only if it directly discend from a word
        -if the feature is synset_verb_groups, then apply to a synset only if it directly discend from a word
        -if the feature is hypernyms, then apply to a synset only if it directly discend from a word
        -if the feature is hyponyms, then apply to a synset only if it directly discend from a word

        Args:
            g (ngx Graph): Graph to be built
            node (ngx Node): The eligible node to keep or filter out
            func (function): Feature to which node would be eventually fed

        Returns:
            Bool: True if keep it, False if discard it
        """
        # a => b is equivalent to not a or b
        if g[father(node)['id']][node['id']]['generating_function'] != func.__name__ and \
                g[father(node)['id']][node['id']]['generating_function'] != 'artist_relationships' and \
                (not func.__name__ == 'entailment' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'member_holonyms' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'member_meronyms' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'part_holonyms' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'part_meronyms' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'substance_holonyms' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'substance_meronyms' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'synset_also_sees' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'synset_attributes' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'synset_similar_tos' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'synset_verb_groups' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'hypernyms' or father(node)['type'] == 'word') and \
                (not func.__name__ == 'hyponyms' or father(node)['type'] == 'word'):
            return True
        else:
            return False