Skip to content
Snippets Groups Projects
mistake_counter.py 3.08 KiB
from mistake_enum import Mistake

class MistakeCounter:
    """Class that counts the number of mistakes after alignment of source
    and shadow. Calculates accuracy based on the number of mistakes
    """

    def __init__(self):
        pass

    def analyse_mistakes(self, source, shadow):
        """
        Counts for every type of mistake the number of mistakes in the
        shadowing task.

        Args:
            source: list of words that are aligned and to which errors are
            assigned, of type SourceWord.
            shadow: list of words that are aligned and to which errors are
            assigned, of type ShadowWord.

        Returns:
            repetition: number of repetition mistakes in task
            phonetic: number of phonetic mistakes in task
            semantic: number of semantic mistakes in task
            skipped: number of skipped words in source
            random: number of not-aligned random mistakes in shadow
            form: number of form mistakes in task
        """
        repetition = self.count_mistakes(shadow, Mistake.REPETITION)
        phonetic = self.count_mistakes(source, Mistake.PHONETIC)
        semantic = self.count_mistakes(source, Mistake.SEMANTIC)
        skipped = self.count_mistakes(source, Mistake.SKIPPED)
        random = self.count_mistakes(shadow, Mistake.RANDOM)
        form = self.count_mistakes(source, Mistake.FORM)
        return repetition, phonetic, semantic, skipped, random, form

    def count_mistakes(self, sentence, mistake):
        """
        Counts the number of times the mistake type given is present in the
        list of words.

        Args:
            sentence: list of words that are aligned and to which errors are
            assigned, of type Word.
            mistake: instance of enumeration mistake, of type Mistake

        Returns:
            mistakes: number of mistakes of type mistake in the sentence
        """
        mistakes = 0
        for word in sentence:
            if word.mistake == mistake:
                mistakes += 1
        return mistakes

    def calculate_accuracy(self, source, shadow):
        """
        Calculates the accuracy of the shadow task given by an aligned
        source and shadow list.

        Args:
            source: list of words that are aligned and to which errors are
            assigned, of type SourceWord.
            shadow: list of words that are aligned and to which errors are
            assigned, of type ShadowWord.

        Returns:
            accuracy: number representing the accuracy of the shadowing task
        """
        repetition, phonetic, semantic, skipped, random, form = \
            self.analyse_mistakes(source, shadow)
        mistakes = repetition+phonetic+semantic+skipped+random+form
        accuracy = (len(source)-mistakes)/len(source)
        return ({'accuracy' : accuracy,
                 '#mistakes': mistakes,
                 '#phonetic': phonetic,
                 '#repetition': repetition,
                 '#form': form,
                 '#semantic': semantic,
                 '#skipped': skipped,
                 '#random': random})