Пример #1
0
 def score_answers(self, question: Question, answers: List[Answer]) -> float:
     """
     Return a score between 0.0 and 1.0 indicating how similar the answers in
     <answers> are.
     This score is calculated by finding the similarity of every
     combination of two answers in <answers> and taking the average of all
     of these similarity scores.
     If there is only one answer in <answers> and it is valid return 1.0
     since a single answer is always identical to itself.
     Raise InvalidAnswerError if any answer in <answers> is not a valid
     answer to <question>.
     === Precondition ===
     len(answers) > 0
     """
     if len(answers) == 1:
         if answers[0].is_valid(question):
             return 1.0
         else:
             raise InvalidAnswerError
     sigma = 0.0
     combos = 0.0
     for i in range(len(answers) - 1):
         for j in range(i + 1, len(answers)):
             first = answers[i]
             second = answers[j]
             if first.is_valid(question) and second.is_valid(question):
                 sigma += question.get_similarity(first, second)
                 combos += 1.0
             else:
                 raise InvalidAnswerError
     if combos == 0.0:
         return 0.0
     else:
         return sigma/combos
Пример #2
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        if len(answers) == 1:
            if answers[0] is None:
                raise InvalidAnswerError
            if answers[0].is_valid(question):
                return 1.0
            else:
                raise InvalidAnswerError
        num_pairs = 0
        score = 0.0
        for i in range(len(answers)):
            if not answers[i].is_valid(question):
                raise InvalidAnswerError
            for j in range(i + 1, len(answers)):
                num_pairs += 1
                score += question.get_similarity(answers[i], answers[j])
        return score / num_pairs
Пример #3
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        # TODO: complete the body of this method
        if len(answers) == 1 and answers[0].is_valid(question):
            return 1.0
        for a in answers:
            if not a.is_valid(question):
                raise InvalidAnswerError
        score = []
        for a in list(self._combinations(answers)):
            score.append(question.get_similarity(a[0], a[1]))
        return sum(score) / len(score)
Пример #4
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers>, finding the average of all
        of these similarity scores, and then subtracting this average from 1.0

        If there is only one answer in <answers> and it is valid, return 0.0
        since a single answer is never identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        for answer in answers:
            if not answer.is_valid(question):
                raise InvalidAnswerError
        if len(answers) == 1:
            return 0.0
        lst_of_similarity = []
        for opt1 in answers:
            index = answers.index(opt1)
            for opt2 in answers[index + 1:]:
                # after the opt index, check the end
                similarity = question.get_similarity(opt1, opt2)
                lst_of_similarity.append(similarity)
        average = sum(lst_of_similarity) / len(lst_of_similarity)
        # average value
        return float(1.0 - average)
Пример #5
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        invalid_answer(question, answers)
        if only_one_valid(question, answers):
            return 1.0
        sim_scores = 0
        num = 0
        for i in range(len(answers) - 1):
            for j in range(i + 1, len(answers)):
                sim_scores += question.get_similarity(answers[i], answers[j])
                num += 1
        return sim_scores / num
Пример #6
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers>, finding the average of all
        of these similarity scores, and then subtracting this average from 1.0

        If there is only one answer in <answers> and it is valid, return 0.0
        since a single answer is never identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        acc1 = 0
        acc2 = 0
        if len(answers) == 1:
            return 0.0
        valid = []
        for answer in answers:
            valid.append(answer.is_valid(question))
        if False in valid:
            raise InvalidAnswerError
        for answer in answers:
            lst = answers[:]
            lst.remove(answer)
            for sub in lst:
                acc1 += question.get_similarity(answer, sub)
                acc2 += 1
        return 1.0 - acc1 / acc2
Пример #7
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """

        super().check_answers(question, answers)

        if len(answers) == 1:
            return 0.0

        count = 0.0
        score = 0.0
        for [a, b] in combinations(answers):
            count += 1.0
            score += question.get_similarity(a, b)

        return score / count
Пример #8
0
    def score_answers(self, question: Question, answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        for answer in answers:
            if not question.validate_answer(answer):
                raise InvalidAnswerError
        if len(answers) == 1:
            return 1.0
        score = 0
        permute = 0
        for ans in answers:
            i = answers.index(ans)
            for other in answers:
                j = answers.index(other)
                if i < j:
                    score += question.get_similarity(ans, other)
                    permute += 1
        return score / permute
Пример #9
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        for ans in answers:
            if question.validate_answer(ans) is False:
                raise InvalidAnswerError
        if len(answers) == 1:
            return 1.0
        else:
            count = 0.0
            count_num = 0
            for index_a, v1 in enumerate(answers):
                for index_b in range(index_a + 1, len(answers)):
                    count += question.get_similarity(v1, answers[index_b])
                    count_num += 1
            return count / count_num  # yield [v1, answers[index_b]]
Пример #10
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.
        # TODO Do we count answer pairs of the same answer with itself? would
        # this make a difference?

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        # Single answer case
        if len(answers) == 1:
            if answers[0].is_valid(question):
                return 1.0
            else:
                raise InvalidAnswerError

        # Check if any answers are not valid
        for answer in answers:
            if answer is None or not answer.is_valid(question):
                raise InvalidAnswerError

        # Multiple answers case
        comparison_count = 0
        total_similarity = 0
        for i in range(len(answers)):
            for j in range(i + 1, len(answers)):
                comparison_count += 1
                total_similarity += question.get_similarity(
                    answers[i], answers[j])
        return total_similarity / comparison_count
Пример #11
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        # TODO: complete the body of this method

        #check answers are valid
        for ans in answers:
            if not ans.is_valid(question):
                raise InvalidAnswerError

        if len(answers) == 1:
            return 1

        scores = []

        #        answers=[1,2,3,4,5,6]
        for i in range(len(answers) - 1):
            for j in range(i + 1, len(answers)):
                #                print(i, j)
                similarity = question.get_similarity(answers[i], answers[j])
                scores.append(similarity)
        return sum(scores) / len(scores)
Пример #12
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """

        for ans in answers:  # checking if any answer in not valid
            if not ans.is_valid(question):
                raise InvalidAnswerError
        if len(answers) == 1:
            return 1.0
        lst_score = []
        for opt in answers:  # looping through to get the combination
            ind = answers.index(opt)
            for other_opt in answers[ind + 1:]:
                # to not include the current option
                # checking for indexing errors
                similarity = question.get_similarity(opt, other_opt)
                lst_score.append(similarity)
                # similarity of the two answers
        average = sum(lst_score) / len(lst_score)
        # sum of all the score divided by number of scores
        return average