コード例 #1
0
ファイル: convert_question.py プロジェクト: brmson/Xserpy
def convert_question(dag, candidates, question, labels, pos_tagged, filename, path):
    """Convert a question from dataset to list of phrases

    Keyword arguments:
    dag -- model for dag parsing
    candidates -- list of lists of entity candidates
    question -- question in object form
    labels -- list of lists phrase tags
    pos_tagged -- list of lists of POS tags
    filename -- output file name
    path -- path to files

    """
    phr, pos = sr.parse_to_phrases([question], [labels], [pos_tagged])
    DAG = sr.shift_reduce(phr[0], pos[0], dag, 50).dag

    ent_path = path + "ent_lr_trn_641.pickle"
    edge_path = path + "edge_lr_trn.pickle"
    rel_path = path + "relation_lr_trn_641.pickle"
    bow_path = path + "bow_dict_all.pickle"
    g_path = path + "rel_dict.pickle"
    dct_path = path + "edge_dict.pickle"
    a_path = path + "bow_all_words_dict.pickle"

    intent = el.label_all(phr[0], DAG, candidates, ent_path, edge_path, rel_path, bow_path, g_path, dct_path, a_path)
    queries = sq.convert_to_queries(intent)
    if len(queries) > 0:
        # sq.create_query_file(filename, queries, phr[0])
        query = sq.create_query(queries, phr[0])
    else:
        query = "SELECT ?a WHERE {}"

    return sq.query_fb_endpoint(query, 'http://freebase.ailao.eu:3030/freebase/query')
コード例 #2
0
def get_phrases_free(question, model, nlp_path, java_path):
    """Convert a string question to list of phrases

    Keyword arguments:
    question -- question in string form
    model -- model for phrase detection
    nlp_path -- path to Stanford NLP tagger
    java_path -- path to Java instalation

    """
    pd_model = pickle.load(open(model))

    q = Question(question, "")
    utterance = question.split()

    labels = []
    pos = pos_tag([q])
    ner = ner_tag([q], nlp_path, java_path)
    l = 4

    u = ["", ""] + utterance + ["", ""]
    p = ['', ''] + [pp[1] for pp in pos[0]] + ['', '']
    n = ['', ''] + [nn[1] for nn in ner[0]] + ['', '']

    for j in range(2, len(u) - 2):
        feature = construct_feature(p, u, n, j, l)
        label = predict(pd_model, feature, 4)
        labels.append(label)
        l = label
    phr, pos_t = sr.parse_to_phrases([q], [labels], pos)
    candidates = el.obtain_entity_candidates(phr, 5)
    return labels, pos, q, candidates
コード例 #3
0
ファイル: convert_question.py プロジェクト: brmson/Xserpy
def get_phrases_free(question, model, nlp_path, java_path):
    """Convert a string question to list of phrases

    Keyword arguments:
    question -- question in string form
    model -- model for phrase detection
    nlp_path -- path to Stanford NLP tagger
    java_path -- path to Java instalation

    """
    pd_model = pickle.load(open(model))

    q = Question(question, "")
    utterance = question.split()

    labels = []
    pos = pos_tag([q])
    ner = ner_tag([q], nlp_path, java_path)
    l = 4

    u = ["", ""] + utterance + ["", ""]
    p = ['', ''] + [pp[1] for pp in pos[0]] + ['', '']
    n = ['', ''] + [nn[1] for nn in ner[0]] + ['', '']

    for j in range(2, len(u)-2):
        feature = construct_feature(p, u, n, j, l)
        label = predict(pd_model, feature, 4)
        labels.append(label)
        l = label
    phr, pos_t = sr.parse_to_phrases([q], [labels], pos)
    candidates = el.obtain_entity_candidates(phr, 5)
    return labels, pos, q, candidates
コード例 #4
0
def convert_question(dag, candidates, question, labels, pos_tagged, filename,
                     path):
    """Convert a question from dataset to list of phrases

    Keyword arguments:
    dag -- model for dag parsing
    candidates -- list of lists of entity candidates
    question -- question in object form
    labels -- list of lists phrase tags
    pos_tagged -- list of lists of POS tags
    filename -- output file name
    path -- path to files

    """
    phr, pos = sr.parse_to_phrases([question], [labels], [pos_tagged])
    DAG = sr.shift_reduce(phr[0], pos[0], dag, 50).dag

    ent_path = path + "ent_lr_trn_641.pickle"
    edge_path = path + "edge_lr_trn.pickle"
    rel_path = path + "relation_lr_trn_641.pickle"
    bow_path = path + "bow_dict_all.pickle"
    g_path = path + "rel_dict.pickle"
    dct_path = path + "edge_dict.pickle"
    a_path = path + "bow_all_words_dict.pickle"

    intent = el.label_all(phr[0], DAG, candidates, ent_path, edge_path,
                          rel_path, bow_path, g_path, dct_path, a_path)
    queries = sq.convert_to_queries(intent)
    if len(queries) > 0:
        # sq.create_query_file(filename, queries, phr[0])
        query = sq.create_query(queries, phr[0])
    else:
        query = "SELECT ?a WHERE {}"

    return sq.query_fb_endpoint(
        query, 'http://freebase.ailao.eu:3030/freebase/query')