Пример #1
0
def rel_emb_score(premise, hypothesis, type_prem, id_prem, type_hypo, id_hypo,
                  is_premise_reversed, is_hypothesis_reversed):
    global rel_emb_model
    if rel_emb_model is None:
        logger.info("Loading relation embeddings.")
        rel_emb_model = KeyedVectors.load_word2vec_format(
            "embeddings/filtered/typed_rel_emb.txt", binary=False)
        logger.info("Done.")
    return rel_emb_model.similarity(combine_type(type_prem, id_prem),
                                    combine_type(type_hypo, id_hypo))
Пример #2
0
def extract_relations(datasets: List[str], with_types: bool):
    rels = set()
    for d in datasets:
        for row in read_in_dataset(d):
            if with_types:
                pr = combine_type(row[2], row[3])
                hy = combine_type(row[4], row[5])
            else:
                pr = str(row[3])
                hy = str(row[5])
            rels.add(pr)
            rels.add(hy)
    return rels
Пример #3
0
def complEx_typed_score(premise, hypothesis, type_prem, id_prem, type_hypo,
                        id_hypo, is_premise_reversed, is_hypothesis_reversed):
    global typed_ComplEx_re_model
    global typed_ComplEx_im_model
    if typed_ComplEx_re_model is None:
        logger.info("Loading typed ComplEx embeddings.")
        typed_ComplEx_re_model = KeyedVectors.load_word2vec_format(
            "embeddings/filtered/typed_ComplEx-real.txt", binary=False)
        typed_ComplEx_im_model = KeyedVectors.load_word2vec_format(
            "embeddings/filtered/typed_ComplEx-imaginary.txt", binary=False)
        logger.info("Done.")

    prem_vec = build_complex_vector(combine_type(type_prem, id_prem),
                                    typed_ComplEx_re_model,
                                    typed_ComplEx_im_model)

    hypo_vec = build_complex_vector(combine_type(type_hypo, id_hypo),
                                    typed_ComplEx_re_model,
                                    typed_ComplEx_im_model)

    return np.real(
        prem_vec.dot(hypo_vec) / np.linalg.norm(prem_vec) /
        np.linalg.norm(hypo_vec))
Пример #4
0
def get_vectors(id1, type1, id2, type2, typed=True):
    if typed:
        id1, id2 = combine_type(type1, id1), combine_type(type2, id2)
    return vecForRel(id1, typed=typed), vecForRel(id2, typed=typed)