src/python/txtai/scoring.py
def weights(self, document):
        """
        Builds weight vector for each token in the input token.

        Args:
            document: (id, tokens, tags)

        Returns:
            list of weights for each token
        """

        # Weights array
        weights = []

        # Unpack document
        _, tokens, _ = document

        # Document length
        length = len(tokens)

        for token in tokens:
            # Lookup frequency and idf score - default to averages if not in repository
            freq = self.wordfreq[token] if token in self.wordfreq else self.avgfreq
            idf = self.idf[token] if token in self.idf else self.avgidf

            # Calculate score for each token, use as weight
            weights.append(self.score(freq, idf, length))

        # Boost weights of tag tokens to match the largest weight in the list
        if self.tags:
            tags = {token: self.tags[token] for token in tokens if token in self.tags}
            if tags:
                maxWeight = max(weights)
                maxTag = max(tags.values())

                weights = [max(maxWeight * (tags[tokens[x]] / maxTag), weight)
                           if tokens[x] in tags else weight for x, weight in enumerate(weights)]

        return weights

        Builds weight vector for each token in the input token.

        Args:
            document: (id, tokens, tags)

        Returns:
            list of weights for each token
        
Builds weight vector for each token in the input token.
Builds weight vector for each token in the input token .
def weights(self, document):
        weights = []
        _, tokens, _ = document
        length = len(tokens)
        for token in tokens:
            freq = self.wordfreq[token] if token in self.wordfreq else self.avgfreq
            idf = self.idf[token] if token in self.idf else self.avgidf
            weights.append(self.score(freq, idf, length))
        if self.tags:
            tags = {token: self.tags[token] for token in tokens if token in self.tags}
            if tags:
                maxWeight = max(weights)
                maxTag = max(tags.values())
                weights = [max(maxWeight * (tags[tokens[x]] / maxTag), weight)
                           if tokens[x] in tags else weight for x, weight in enumerate(weights)]
        return weights
def weights ( self , document ) : weights = [ ] _ , tokens , _ = document length = len ( tokens ) for token in tokens : freq = self . wordfreq [ token ] if token in self . wordfreq else self . avgfreq idf = self . idf [ token ] if token in self . idf else self . avgidf weights . append ( self . score ( freq , idf , length ) ) if self . tags : tags = { token : self . tags [ token ] for token in tokens if token in self . tags } if tags : maxWeight = max ( weights ) maxTag = max ( tags . values ( ) ) weights = [ max ( maxWeight * ( tags [ tokens [ x ] ] / maxTag ) , weight ) if tokens [ x ] in tags else weight for x , weight in enumerate ( weights ) ] return weights
Builds weight vector for each token in the input token.
Builds weight vector for each token in the input token .
"""
        Builds weight vector for each token in the input token.

        Args:
            document: (id, tokens, tags)

        Returns:
            list of weights for each token
        """ # Weights array # Unpack document # Document length # Lookup frequency and idf score - default to averages if not in repository # Calculate score for each token, use as weight # Boost weights of tag tokens to match the largest weight in the list
list of weights for each token
list of weights for each token
self
document
(id, tokens, tags)
( id tokens tags )