Ejemplo n.º 1
0
def _get_archive(category: taxonomy.Category) -> str:
    if category.endswith(".*"):
        return category.split(".", 1)[0]
    try:
        return taxonomy.CATEGORIES_ACTIVE[category]['in_archive']
    except KeyError:
        if "." in category:
            return category.split(".", 1)[0]
        return ""
Ejemplo n.º 2
0
def _get_archive(category: taxonomy.Category) -> str:
    archive: str
    if category.endswith(".*"):
        archive = category.split(".", 1)[0]
    else:
        try:
            archive = taxonomy.CATEGORIES_ACTIVE[category]['in_archive']
        except KeyError:
            if "." in category:
                archive = category.split(".", 1)[0]
            else:
                archive = ""
    return archive
Ejemplo n.º 3
0
 def before_init(cls, data: dict) -> None:
     """Make sure that endorsements are :class:`.Category` instances."""
     # Iterative coercion is hard. It's a lot easier to handle this here
     # than to implement a general-purpose coercsion.
     # if self.endorsements and type(self.endorsements[0]) is not Category:
     data['endorsements'] = [
         Category(obj) for obj in data.get('endorsements', [])
     ]
     if 'scopes' in data:
         if type(data['scopes']) is str:
             data['scopes'] = [
                 Scope(*scope.split(':'))
                 for scope in data['scopes'].split()
             ]
         elif type(data['scopes']) is list:
             data['scopes'] = [
                 Scope(**scope) if type(scope) is dict else Scope(
                     *scope.split(':')) for scope in data['scopes']
             ]
Ejemplo n.º 4
0
    def classify(self,
                 article: Article,
                 top_k: int = 5) -> List[ClassifierPrediction]:
        """
        Classify an article.

        Parameters
        ----------
        article
            Article object to predict category for.
        top_k
            Number of the best predictions to return.

        Returns
        -------
        list
            List of :class:`ClassifierPrediction` objects.
        """
        predictions = [
            ClassifierPrediction(Category(category), float(probability))
            for category, probability in self._predict(article)
        ]

        predictions = sorted(predictions,
                             key=lambda p: p.probability,
                             reverse=True)
        top = predictions[:top_k]

        if article.primary:
            primary_pred_in_top = next(
                (pred for pred in top if pred.category == article.primary),
                None)
            if not primary_pred_in_top:
                primary_pred = next((pred for pred in predictions
                                     if pred.category == article.primary),
                                    None)
                if primary_pred:
                    top.append(primary_pred)

        return top
Ejemplo n.º 5
0
    def endorsed_for(self, category: Category) -> bool:
        """
        Check whether category is included in this endorsement authorization.

        If a user/client is authorized for all categories in a particular
        archive, the category names in :attr:`Authorization.endorsements` will
        be compressed to a wilcard ``archive.*`` representation. If the
        user/client is authorized for all categories in the system, this will
        be compressed to "*.*".

        Parameters
        ----------
        category : :class:`.Category`

        Returns
        -------
        bool

        """
        archive = category.split(".", 1)[0] if "." in category else category
        return category in self.endorsements \
            or f"{archive}.*" in self.endorsements \
            or "*.*" in self.endorsements