qa_bot.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:20Qs 作者: Cypher1 项目源码 文件源码
def get_entropy(question, animals):
    """ Finds the entropy of the answers of a question for a set of animals """
    response_set = {answer:0.000001 for answer in ANSWERS}

    all_responses = query_all_responses(question=question, animals=animals)
    for animal in animals:
        responses = all_responses.get(animal.name, NO_RESPONSE)
        if responses is not None:
            responses = sorted(
                responses.items(),
                key=lambda resp: -resp[1]
            )
            (first, _) = responses[0]
            (last, _) = responses[-1]
            response_set[first] += animal.prob
            response_set[last] += (1-animal.prob)
        else:
            print("COULDN'T FIND ANIMAL \"{}\"".format(animal.name))

    total = sum(response_set.values())
    probs = [count/total for count in response_set.values()]

    entropy = sum([-(p)*log2(p) for p in probs])
    return entropy
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号