diff --git a/umbra/mistake_enum.py b/umbra/mistake_enum.py new file mode 100644 index 0000000000000000000000000000000000000000..cbad0cff910dcb01f4d6a2fd329bc3e735d13094 --- /dev/null +++ b/umbra/mistake_enum.py @@ -0,0 +1,18 @@ +from enum import Enum + + +class NoValue(Enum): + """Enum class for which the value is not important. The __repr__ hides + the unimportant value, when a NoValue Enum is printed. + """ + def __repr__(self): + return '<%s.%s>' % (self.__class__.__name__, self.name) + + +class Mistake(Enum): + """Enum for all types of mistakes that can be made in the shadow task.""" + PHONETIC = "phonetic" + REPETITION = "repetition" + SEMANTIC = "semantic" + SKIPPED = "skipped" # For source words that are not shadowed + RANDOM = "random" # For shadow words that do not reflect a source word diff --git a/umbra/words.py b/umbra/words.py index acbb63e91198f0f49fd60b6e68b32a8f58817d2f..69513cee754a2b121054e169951104a206aae985 100644 --- a/umbra/words.py +++ b/umbra/words.py @@ -1,9 +1,13 @@ +from mistake_enum import Mistake + + class Word: def __init__(self, word, onset, offset): self._word = word self._onset = onset self._offset = offset self._anchor = None + self._mistake = None def __str__(self): return "%s | %f | %f" % (self._word, self._onset, self._offset) @@ -14,7 +18,6 @@ class Word: def __eq__(self, word): return self._word == word.word - @property def word(self): """Getter for the word. @@ -51,6 +54,29 @@ class Word: """ return self._anchor + @property + def mistake(self): + """Getter for the mistake. + + Return: + mistake: Enum of the type of mistake that this word is. None if + not marked as a mistake. + """ + return self._mistake + + @mistake.setter + def mistake(self, mistake): + """Setter for the type of mistake. + + Args: + mistake: Mistake Enum + """ + self._set_mistake(mistake) + + def _set_mistake(self, mistake): + """Mistake setter. Has to be overridden in the subclass.""" + raise NotImplementedError + def is_anchor(self): """Whether this word is a anchor or not. @@ -61,7 +87,7 @@ class Word: @anchor.setter def anchor(self, anchor): - """ Anchor this Word object to another Word object. + """Anchor this Word object to another Word object. Args: anchor: Word instance to anchor to @@ -127,6 +153,18 @@ class ShadowWord(Word): """ return self._source is not None + def _set_mistake(self, mistake): + """Setter for the type of mistake. Only works if the word is not marked + as correct yet and if mistake is not SKIPPED. + + Args: + mistake: Mistake Enum + """ + assert self._correct is False + assert mistake != Mistake.SKIPPED + self._mistake(mistake) + + class SourceWord(Word): def __init__(self, word, onset, offset): super().__init__(word, onset, offset) @@ -150,6 +188,15 @@ class SourceWord(Word): """ self._shadowed = value + def _set_mistake(self, mistake): + """Setter for the type of mistake. Only works if mistake is not RANDOM. + + Args: + mistake: Mistake Enum + """ + assert mistake != Mistake.RANDOM + self._mistake(mistake) + class Sentence(list): def __init__(self, words):