Ejemplo n.º 1
0
def get_answer(question):
    """
    :param question: String with the question.
    :return: Filled Answer object.
    """
    a = Answer(Question(question))

    checked = check_keywords(a.q)
    if not checked:
        a.text = 'Didn\'t understand the question'
        return a

    # print warning on weird keywords
    # (XXX: should we give up on these questions instead?)
    check_unknowns(a)
    if len(a.q.unknown) > 0:
        print 'we have no information on these words:', a.q.unknown

    found_sources, found_anything = get_content_elastic(a, search_all=False)

    if found_sources:
        gen_features(a)
        a.text = answer_all(a)
        return a

    if not found_anything:
        a.text = 'Absolutely no result'
    else:
        a.text = 'No result'
    return a
Ejemplo n.º 2
0
def get_answer(question):
    """
    :param question: String with the question.
    :return: Filled Answer object.
    note: if question starts with '>>>', just ask elasticsearch
    """
    if question.startswith('>>>'):
        return ask_only(question[3:])
    a = Answer(Question(question))

    checked = check_keywords(a.q)
    if not checked:
        a.q.query += ' (' + ','.join(a.q.not_in_kw) + ' not in keywords)'
        a.text = 'Didn\'t understand the question'
        return a

    # print warning on weird keywords
    # (XXX: should we give up on these questions instead?)
    check_unknowns(a)
    if len(a.q.unknown) > 0:
        print 'we have no information on these words:', a.q.unknown

    found_sources, found_anything = get_content_elastic(a, search_all=False)

    if found_sources:
        gen_features(a)
        a.text = answer_all(a)
        return a

    if not found_anything:
        a.text = 'Absolutely no result'
    else:
        a.text = 'No result'
    return a
Ejemplo n.º 3
0
def get_answer(question):
    """
    :param question: String with the question.
    :return: Filled Answer object.
    note: if question starts with '>>>', just ask elasticsearch
    """
    if question.startswith('>>>'):
        return ask_only(question[3:])
    a = Answer(Question(question))

    checked = check_keywords(a.q)
    if not checked:
        a.q.query += ' (' + ','.join(a.q.not_in_kw) + ' not in keywords)'
        a.text = 'Didn\'t understand the question'
        return a

    # print warning on weird keywords
    # (XXX: should we give up on these questions instead?)
    check_unknowns(a)
    if len(a.q.unknown) > 0:
        print 'we have no information on these words:', a.q.unknown

    found_sources, found_anything = get_content_elastic(a, search_all=False)

    if found_sources:
        gen_features(a)
        a.text = answer_all(a)
        return a

    if not found_anything:
        a.text = 'Absolutely no result'
    else:
        a.text = 'No result'
    return a
Ejemplo n.º 4
0
def ask_only(query):
    a = Answer(Question(preprocess_question(query)))
    check_unknowns(a)
    if len(a.q.unknown) > 0:
        print 'we have no information on these words:', a.q.unknown
    found_sources, found_anything = ask(a, query)
    if found_sources:
        gen_features(a)
        a.text = answer_all(a)
        a.text = 'Query only'
        return a
    if not found_anything:
        a.text = 'Absolutely no result'
    return a
Ejemplo n.º 5
0
def ask_only(query):
    a = Answer(Question(preprocess_question(query)))
    check_unknowns(a)
    if len(a.q.unknown) > 0:
        print 'we have no information on these words:', a.q.unknown
    found_sources, found_anything = ask(a, query)
    if found_sources:
        gen_features(a)
        a.text = answer_all(a)
        a.text = 'Query only'
        return a
    if not found_anything:
        a.text = 'Absolutely no result'
    return a
def main():
    n = 12
    m = 2
    if len(sys.argv) <= 1:
        print('Usage is main [gen|run] [seed]')
        return
    elif len(sys.argv) >= 2:
        if len(sys.argv) >= 3:
            random.seed(int(sys.argv[2]))
        A, B, a = gen_features(n, m)
        C = [[A[i][0], A[i][1], B[i][0], B[i][1]] for i in range(len(A))]
        if sys.argv[1] == 'gen':
            C.insert(0, ['A-Radius', 'A-Perimeter', 'B-Radius', 'B-Perimeter'])
            tableutils.printmat(C, True)
        elif sys.argv[1] == 'plane':
            a = [a]
            a.insert(0, ['a1', 'a2', 'a0'])
            tableutils.printmat(a)
        elif sys.argv[1] == 'run':
            rc, Value, G = solve_classification(A, B)
            T = [[[rc, Value], G]]
            tableutils.printmat(T)
        elif sys.argv[1] == 'margins':
            rc, a = solve_margins_classification(A, B)
            if rc == 0:
                low = min([A[i][0] for i in range(len(A))] +
                          [B[i][0] for i in range(len(B))])
                high = 1 + max([A[i][0] for i in range(len(A))] +
                               [B[i][0] for i in range(len(B))])
                T = [[x, a[2] / a[1] - a[0] / a[1] * x]
                     for x in range(low, high)]
            else:
                T = [['Infeasible']]
            tableutils.printmat(T, True)
Ejemplo n.º 7
0
 def classify(name):
     name = name.upper()
     if name in cache_dict:
         return cache_dict[name]
     else:
         label = classifier.classify(gen_features(name))
         if cache:
             cache_dict[name] = label
         return label
Ejemplo n.º 8
0
    def classify(name):
        name = name.upper()
        if name in cache_dict:
            return cache_dict[name]
        else:
            feature = gen_features(name)
            label_numeric = classifier.predict(feature)[0]
            label = classes[label_numeric]

            if cache:
                cache_dict[name] = label
            return label
Ejemplo n.º 9
0
def test_compound_surnames():
    name = "Adi du Preez"
    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=False,
                                          include_trigrams=False,
                                          include_4grams=False,
                                          include_surname=True,
                                          include_fullname=False)

    ngrams = ["surname_DU_PREEZ"]
    for ngram in ngrams:
        assert ngram in name_features
    assert len(name_features) == len(ngrams)
Ejemplo n.º 10
0
def test_gen_features():
    name = "Adi Eyal"
    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=False,
                                          include_trigrams=False,
                                          include_4grams=False,
                                          include_surname=False,
                                          include_fullname=False)
    assert len(name_features) == 0

    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=False,
                                          include_trigrams=False,
                                          include_4grams=False,
                                          include_surname=False,
                                          include_fullname=True)
    assert len(name_features) == 1
    assert "name_ADI_EYAL" in name_features

    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=False,
                                          include_trigrams=False,
                                          include_4grams=False,
                                          include_surname=True,
                                          include_fullname=False)
    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=False,
                                          include_trigrams=False,
                                          include_4grams=False,
                                          include_surname=True,
                                          include_fullname=False)
    assert len(name_features) == 1
    assert "surname_EYAL" in name_features

    name_features = features.gen_features(name,
                                          include_name_part=True,
                                          include_bigrams=False,
                                          include_trigrams=False,
                                          include_4grams=False,
                                          include_surname=False,
                                          include_fullname=False)
    assert len(name_features) == 2
    assert "namepart_ADI" in name_features
    assert "namepart_EYAL" in name_features

    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=True,
                                          include_trigrams=False,
                                          include_4grams=False,
                                          include_surname=False,
                                          include_fullname=False)
    assert len(name_features) == 9
    ngrams = ["AD", "DI", "EY", "YA", "AL", "b_AD", "b_EY", "l_DI", "l_AL"]
    for ngram in ngrams:
        assert ngram in name_features
    assert len(name_features) == len(ngrams)

    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=False,
                                          include_trigrams=True,
                                          include_4grams=False,
                                          include_surname=False,
                                          include_fullname=False)
    assert len(name_features) == 7
    ngrams = ["ADI", "EYA", "YAL", "b_ADI", "l_ADI", "b_EYA", "l_YAL"]
    for ngram in ngrams:
        assert ngram in name_features
    assert len(name_features) == len(ngrams)

    name_features = features.gen_features(name,
                                          include_name_part=False,
                                          include_bigrams=False,
                                          include_trigrams=False,
                                          include_4grams=True,
                                          include_surname=False,
                                          include_fullname=False)
    assert len(name_features) == 3
    ngrams = ["EYAL", "b_EYAL", "l_EYAL"]
    for ngram in ngrams:
        assert ngram in name_features
    assert len(name_features) == len(ngrams)
Ejemplo n.º 11
0
 def classify_many(names):
     features = [gen_features(name) for name in names]
     labels = classifier.predict(features)
     return [classes[l] for l in labels]