示例#1
0
async def answer_question(question, original_answers):
    print("Searching")
    start = time.time()

    answers = []
    for ans in original_answers:
        answers.append(ans.translate(punctuation_to_none))
        answers.append(ans.translate(punctuation_to_space))
    answers = list(dict.fromkeys(answers))
    print(answers)

    question_lower = question.lower()

    reverse = "NOT" in question or\
              ("least" in question_lower and "at least" not in question_lower) or\
              "NEVER" in question

    quoted = re.findall('"([^"]*)"', question_lower)  # Get all words in quotes
    no_quote = question_lower
    for quote in quoted:
        no_quote = no_quote.replace(f"\"{quote}\"", "1placeholder1")

    question_keywords = search.find_keywords(no_quote)
    for quote in quoted:
        question_keywords[question_keywords.index("1placeholder1")] = quote

    print(question_keywords)
    search_results = await search.search_google("+".join(question_keywords), 5)
    print(search_results)

    search_text = [x.translate(punctuation_to_none) for x in await search.get_clean_texts(search_results)]

    best_answer = await __search_method1(search_text, answers, reverse)
    if best_answer == "":
        best_answer = await __search_method2(search_text, answers, reverse)
    print(f"{Fore.GREEN}{best_answer}{Style.RESET_ALL}\n")

    # Get key nouns for Method 3
    key_nouns = set(quoted)

    if len(key_nouns) == 0:
        q_word_location = -1
        for q_word in ["what", "when", "who", "which", "whom", "where", "why", "how"]:
            q_word_location = question_lower.find(q_word)
            if q_word_location != -1:
                break

        if q_word_location > len(question) // 2 or q_word_location == -1:
            key_nouns.update(search.find_nouns(question, num_words=5))
        else:
            key_nouns.update(search.find_nouns(question, num_words=5, reverse=True))

        key_nouns -= {"type"}

    key_nouns = [noun.lower() for noun in key_nouns]
    print(f"Question nouns: {key_nouns}")
    answer3 = await __search_method3(list(set(question_keywords)), key_nouns, original_answers, reverse)
    print(f"{Fore.GREEN}{answer3}{Style.RESET_ALL}")

    print(f"Search took {time.time() - start} seconds")
示例#2
0
async def search_method3_stub(question_keywords, quoted, question_lower, question, original_answers, reverse):
    start = time.time()
    # Get key nouns for Method 3
    key_nouns = set(quoted)

    q_word_location = search.find_q_word_location(question_lower)
    if len(key_nouns) == 0:
        if q_word_location > len(question) // 2 or q_word_location == -1:
            key_nouns.update(search.find_nouns(question, num_words=5))
        else:
            key_nouns.update(search.find_nouns(question, num_words=5, reverse=True))

        key_nouns -= {"type"}

    # Add consecutive capitalised words (Thanks talentoscope!)
    key_nouns.update(re.findall("([A-Z][a-z]+(?=\s[A-Z])(?:\s[A-Z][a-z]+)+)",
                                " ".join([w for idx, w in enumerate(question.split(" ")) if idx != q_word_location])))

    key_nouns = {noun.lower() for noun in key_nouns}
    print(f"Question nouns: {key_nouns}")
    answer3 = await __search_method3(list(set(question_keywords)), key_nouns, original_answers, reverse)
    print(f"{Fore.GREEN}method3: {answer3}{Style.RESET_ALL}")

    print(f"Search method3 took {time.time() - start} seconds")
示例#3
0
async def answer_question(question, original_answers, q_number, q_count):
    print("Searching")
    start = time.time()

    answers = []
    for ans in original_answers:
        answers.append(ans.translate(punctuation_to_none))
        answers.append(ans.translate(punctuation_to_space))
    answers = list(dict.fromkeys(answers))
    print(answers)

    question_lower = question.lower()

    reverse = "NOT" in question or\
              ("least" in question_lower and "at least" not in question_lower) or\
              "NEVER" in question

    quoted = re.findall('"([^"]*)"', question_lower)  # Get all words in quotes
    no_quote = question_lower
    for quote in quoted:
        no_quote = no_quote.replace(f"\"{quote}\"", "1placeholder1")

    question_keywords = search.find_keywords(no_quote)
    for quote in quoted:
        question_keywords[question_keywords.index("1placeholder1")] = quote

    print(question_keywords)
    search_results = await search.search_google("+".join(question_keywords), 5)
    print(search_results)

    search_text = [
        x.translate(punctuation_to_none)
        for x in await search.get_clean_texts(search_results)
    ]

    best_answer, c = await __search_method1(search_text, answers, reverse)
    if best_answer == "":
        best_answer, c = await __search_method2(search_text, answers, reverse)

    if best_answer != "":
        print(f"{Fore.GREEN}{best_answer}{Style.RESET_ALL}\n")
        title = f"Question {q_number} out of {q_count} ============ Method1=============="
        answer = f"{c}\n **best answer: {best_answer}**"
        ans = Webhook(
            "https://discordapp.com/api/webhooks/454166751525994496/c1oeJxqmO_ysy0ricw78rPcTlDrpauISrxYKWe0-xyds7lUU1Z49JYFPa8QMEQPwSDru",
            color=111111)
        ans.set_title(title=title)
        ans.set_desc(answer)
        ans.post()
    # Get key nouns for Method 3
    key_nouns = set(quoted)

    q_word_location = search.find_q_word_location(question_lower)
    if len(key_nouns) == 0:
        if q_word_location > len(question) // 2 or q_word_location == -1:
            key_nouns.update(search.find_nouns(question, num_words=5))
        else:
            key_nouns.update(
                search.find_nouns(question, num_words=5, reverse=True))

        key_nouns -= {"type"}

    # Add consecutive capitalised words (Thanks talentoscope!)
    key_nouns.update(
        re.findall(
            "([A-Z][a-z]+(?=\s[A-Z])(?:\s[A-Z][a-z]+)+)", " ".join([
                w for idx, w in enumerate(question.split(" "))
                if idx != q_word_location
            ])))

    key_nouns = {noun.lower() for noun in key_nouns}
    print(f"Question nouns: {key_nouns}")
    answer3, k, n = await __search_method3(list(set(question_keywords)),
                                           key_nouns, original_answers,
                                           reverse)
    title = f"Question {q_number} out of {q_count} ============ Method2=============="
    answer = f"{k}\n{n}\n **best answer: {answer3}**"
    ans2 = Webhook(
        "https://discordapp.com/api/webhooks/454166751525994496/c1oeJxqmO_ysy0ricw78rPcTlDrpauISrxYKWe0-xyds7lUU1Z49JYFPa8QMEQPwSDru",
        color=111111)
    ans2.set_title(title=title)
    ans2.set_desc(answer)
    ans2.post()
    print(f"{Fore.GREEN}{answer3}{Style.RESET_ALL}")

    print(f"Search took {time.time() - start} seconds")
示例#4
0
async def answer_question(question, original_answers):
    print("Searching")
    start = time.time()

    answers = []
    for ans in original_answers:
        answers.append(ans.translate(punctuation_to_none))
        answers.append(ans.translate(punctuation_to_space))
    answers = list(dict.fromkeys(answers))
    print(answers)

    question_lower = question.lower()

    reverse = "NOT" in question or\
              ("least" in question_lower and "at least" not in question_lower) or\
              "NEVER" in question

    quoted = re.findall('"([^"]*)"', question_lower)  # Get all words in quotes
    no_quote = question_lower
    for quote in quoted:
        no_quote = no_quote.replace(f"\"{quote}\"", "1placeholder1")

    question_keywords = search.find_keywords(no_quote)
    for quote in quoted:
        question_keywords[question_keywords.index("1placeholder1")] = quote

    print(question_keywords)
    search_results = await search.search_google("+".join(question_keywords), 5)
    print(search_results)

    search_text = [
        x.translate(punctuation_to_none)
        for x in await search.get_clean_texts(search_results)
    ]

    awnser1 = await __search_method2(search_text, answers, reverse)
    if awnser1 != "":
        print(f"{Fore.GREEN}{awnser1}{Style.RESET_ALL}\n")

    # Get key nouns for Method 3
    key_nouns = set(quoted)

    q_word_location = search.find_q_word_location(question_lower)
    if len(key_nouns) == 0:
        if q_word_location > len(question) // 2 or q_word_location == -1:
            key_nouns.update(search.find_nouns(question, num_words=5))
        else:
            key_nouns.update(
                search.find_nouns(question, num_words=5, reverse=True))

        key_nouns -= {"type"}

    # Add consecutive capitalised words (Thanks talentoscope!)
    key_nouns.update(
        re.findall(
            "([A-Z][a-z]+(?=\s[A-Z])(?:\s[A-Z][a-z]+)+)", " ".join([
                w for idx, w in enumerate(question.split(" "))
                if idx != q_word_location
            ])))

    key_nouns = {noun.lower() for noun in key_nouns}
    print(f"Question nouns: {key_nouns}")
    answer3 = await __search_method3(list(set(question_keywords)), key_nouns,
                                     original_answers, reverse)
    print(f"{Fore.GREEN}{answer3}{Style.RESET_ALL}")

    print(f"Search took {time.time() - start} seconds")