Beispiel #1
0
def answer_question(input_question):

    en_doc = nlp(u'' + input_question)

    question_class = classify_question(en_doc)
    print("Class:", question_class)

    question_keywords = extract_features(question_class, en_doc)
    print("Question Features:", question_keywords)

    question_query = construct_query(question_keywords, en_doc)
    print("Question Query:", question_query)

    print("Fetching Knowledge source...")
    wiki_pages = fetch_wiki(question_keywords, number_of_search=3)
    print("Pages Fetched:", len(wiki_pages))

    # Anaphora Resolution

    ranked_wiki_docs = rank_docs(question_keywords)
    print("Ranked Pages:", ranked_wiki_docs)

    candidate_answers, split_keywords = get_candidate_answers(
        question_query, ranked_wiki_docs, nlp)
    print("Candidate Answer:", "(" + str(len(candidate_answers)) + ")",
          candidate_answers)

    print("Answer:", " ".join(candidate_answers))

    answer = " ".join(candidate_answers)

    return answer
Beispiel #2
0
def answer_question(q, num_sentences):

    q = nlp(u'' + q)

    question_class = classify_question(q)
    _logger.info("Question Class: {}".format(question_class))

    question_keywords = extract_features(question_class, q)
    _logger.debug("Question Features: {}".format(question_keywords))

    query = construct_query(question_keywords, q)
    _logger.debug("Query: {}".format(query))

    _logger.info("Retrieving {} wikipedia pages...".format(num_sentences))
    wiki_pages = fetch_wiki(question_keywords, number_of_search=num_sentences)
    _logger.debug("Pages retrieved: {}".format(len(wiki_pages)))

    # Anaphora Resolution

    ranked_wiki_docs = rank_docs(question_keywords)
    _logger.debug("Ranked pages: {}".format(ranked_wiki_docs))

    candidate_answers, keywords = get_candidate_answers(query, ranked_wiki_docs, nlp)
    _logger.info("Candidate answers ({}):\n{}".format(len(candidate_answers), '\n'.join(candidate_answers)))

    return " ".join(candidate_answers)
Beispiel #3
0
def answer_question(input_question):
    warnings.warn("This method is now deprecated.", DeprecationWarning)

    en_nlp = spacy.load('en_core_web_md')

    en_doc = en_nlp(u'' + input_question)

    question_class = classify_question(en_doc)
    print("Class:", question_class)

    question_keywords = extract_features(question_class, en_doc)
    print("Question Features:", question_keywords)

    question_query = construct_query(question_keywords, en_doc)
    print("Question Query:", question_query)

    print("Fetching Knowledge source...")
    wiki_pages = fetch_wiki(question_keywords, number_of_search=3)
    print("Pages Fetched:", len(wiki_pages))

    # Anaphora Resolution

    ranked_wiki_docs = rank_docs(question_keywords)
    print("Ranked Pages:", ranked_wiki_docs)

    candidate_answers, split_keywords = get_candidate_answers(
        question_query, ranked_wiki_docs, en_nlp)
    print("Candidate Answer:", "(" + str(len(candidate_answers)) + ")",
          candidate_answers)

    print("Answer:", " ".join(candidate_answers))

    answer = " ".join(candidate_answers)

    return answer
Beispiel #4
0
    def process_question(self):

        self.question_class = classify_question(self.question_doc)
        _logger.info("Question Class: {}".format(self.question_class))

        self.question_keywords = extract_features(self.question_class,
                                                  self.question_doc)
        _logger.info("Question Features: {}".format(self.question_keywords))

        self.query = construct_query(self.question_keywords, self.question_doc)
        _logger.info("Query: {}".format(self.query))
Beispiel #5
0
    def process_question(self, dfOut):

        self.question_class = classify_question(self.question_doc)
        _logger.info("Question Class: {}".format(self.question_class))
        temp = self.question_class.tostring()
        dfentry[0] = "" + self.question_class

        self.question_keywords = extract_features(self.question_class,
                                                  self.question_doc)
        _logger.info("Question Features: {}".format(self.question_keywords))
        dfentry[1] = ','.join(self.question_keywords)

        self.query = construct_query(self.question_keywords, self.question_doc)
        _logger.info("Query: {}".format(self.query))
        dfentry[2] = "{}".format(self.query)
        insert(dfOut, dfentry)
    def test_extract_features(self):
        sql_man = SqLiteManager()
        en_nlp_l = spacy.load(EN_MODEL_MD)

        result = sql_man.get_questions_between(5, 7)

        for row in result:
            qid = row[0]
            with self.subTest(qid):
                question = row[1]
                question_type = row[2]

                en_doc = en_nlp_l(u'' + question)

                features = extract_features(question_type, en_doc, True)
                print("{0}){1} :\nExtracted: {2}".format(qid, question, features))
                js_feat = json.dumps(features)
                sql_man.update_feature(qid, js_feat)
                assert features is not None