"""
Tracking sign requests and filter them
"""
from .config import CFG


class SignTracker:
    def __init__(self):
        self.prev_rendered = set()

    def update(self, words, probabilities):
        """Update tracker with current signs and their output probabilities

        Args:
            words (list): list of predicted words
            probabilities (list): list of predicted probabilities

        Returns:
            str: filtered sign to request, if words are filtered, returns None
        """
        # the top prediction
        best_word, best_prob = words[0], probabilities[0]
        # if top prediction is bellow threshold or if it is in set of previously rendered words, ignore it,
        # otherwise add the word to previously rendered words and output it
        if best_prob < CFG.SIGN_PROB_THOLD or best_word in self.prev_rendered:
            output = None
        else:
            self.prev_rendered.add(best_word)
            output = best_word
        # remove words from prev rendered set if they are not in current predictions
        self.prev_rendered = self.prev_rendered.intersection(words)
        return output
