natural language toolkit classifier interface c 20012023 nltk project edward loper edlopergmail com steven bird stevenbird1gmail com minor additions url https www nltk org for license information see license txt interfaces for labeling tokens with category labels or class labels classifieri is a standard interface for singlecategory classification in which the set of categories is known the number of categories is finite and each text belongs to exactly one category multiclassifieri is a standard interface for multicategory classification which is like singlecategory classification except that each text belongs to zero or more categories classification interfaces a processing interface for labeling tokens with a single category label or class labels are typically strs or ints but can be any immutable type the set of labels that the classifier chooses from must be fixed and finite subclasses must define labels either classify or classifymany or both subclasses may define either probclassify or probclassifymany or both return the list of category labels used by this classifier rtype list of immutable return the most appropriate label for the given featureset rtype label return a probability distribution over labels for the given featureset rtype probdisti apply self classify to each element of featuresets i e return self classifyfs for fs in featuresets rtype listlabel apply self probclassify to each element of featuresets i e return self probclassifyfs for fs in featuresets rtype listprobdisti a processing interface for labeling tokens with zero or more category labels or labels labels are typically strs or ints but can be any immutable type the set of labels that the multiclassifier chooses from must be fixed and finite subclasses must define labels either classify or classifymany or both subclasses may define either probclassify or probclassifymany or both return the list of category labels used by this classifier rtype list of immutable return the most appropriate set of labels for the given featureset rtype setlabel return a probability distribution over sets of labels for the given featureset rtype probdisti apply self classify to each element of featuresets i e return self classifyfs for fs in featuresets rtype listsetlabel apply self probclassify to each element of featuresets i e return self probclassifyfs for fs in featuresets rtype listprobdisti xx in progress class sequenceclassifieri a processing interface for labeling sequences of tokens with a single category label or class labels are typically strs or ints but can be any immutable type the set of labels that the classifier chooses from must be fixed and finite def labelsself return the list of category labels used by this classifier rtype list of immutable raise notimplementederror def probclassifyself featureset return a probability distribution over labels for the given featureset if featureset is a list of featuresets then return a corresponding list containing the probability distribution over labels for each of the given featuresets where the i th element of this list is the most appropriate label for the i th element of featuresets raise notimplementederror def classifyself featureset return the most appropriate label for the given featureset if featureset is a list of featuresets then return a corresponding list containing the most appropriate label for each of the given featuresets where the i th element of this list is the most appropriate label for the i th element of featuresets raise notimplementederror natural language toolkit classifier interface c 2001 2023 nltk project edward loper edloper gmail com steven bird stevenbird1 gmail com minor additions url https www nltk org for license information see license txt interfaces for labeling tokens with category labels or class labels classifieri is a standard interface for single category classification in which the set of categories is known the number of categories is finite and each text belongs to exactly one category multiclassifieri is a standard interface for multi category classification which is like single category classification except that each text belongs to zero or more categories classification interfaces a processing interface for labeling tokens with a single category label or class labels are typically strs or ints but can be any immutable type the set of labels that the classifier chooses from must be fixed and finite subclasses must define labels either classify or classify_many or both subclasses may define either prob_classify or prob_classify_many or both return the list of category labels used by this classifier rtype list of immutable return the most appropriate label for the given featureset rtype label return a probability distribution over labels for the given featureset rtype probdisti apply self classify to each element of featuresets i e return self classify fs for fs in featuresets rtype list label apply self prob_classify to each element of featuresets i e return self prob_classify fs for fs in featuresets rtype list probdisti a processing interface for labeling tokens with zero or more category labels or labels labels are typically strs or ints but can be any immutable type the set of labels that the multi classifier chooses from must be fixed and finite subclasses must define labels either classify or classify_many or both subclasses may define either prob_classify or prob_classify_many or both return the list of category labels used by this classifier rtype list of immutable return the most appropriate set of labels for the given featureset rtype set label return a probability distribution over sets of labels for the given featureset rtype probdisti apply self classify to each element of featuresets i e return self classify fs for fs in featuresets rtype list set label apply self prob_classify to each element of featuresets i e return self prob_classify fs for fs in featuresets rtype list probdisti xx in progress class sequenceclassifieri a processing interface for labeling sequences of tokens with a single category label or class labels are typically strs or ints but can be any immutable type the set of labels that the classifier chooses from must be fixed and finite def labels self return the list of category labels used by this classifier rtype list of immutable raise notimplementederror def prob_classify self featureset return a probability distribution over labels for the given featureset if featureset is a list of featuresets then return a corresponding list containing the probability distribution over labels for each of the given featuresets where the i th element of this list is the most appropriate label for the i th element of featuresets raise notimplementederror def classify self featureset return the most appropriate label for the given featureset if featureset is a list of featuresets then return a corresponding list containing the most appropriate label for each of the given featuresets where the i th element of this list is the most appropriate label for the i th element of featuresets raise notimplementederror
from nltk.internals import overridden






class ClassifierI:
    

    def labels(self):
        
        raise NotImplementedError()

    def classify(self, featureset):
        
        if overridden(self.classify_many):
            return self.classify_many([featureset])[0]
        else:
            raise NotImplementedError()

    def prob_classify(self, featureset):
        
        if overridden(self.prob_classify_many):
            return self.prob_classify_many([featureset])[0]
        else:
            raise NotImplementedError()

    def classify_many(self, featuresets):
        
        return [self.classify(fs) for fs in featuresets]

    def prob_classify_many(self, featuresets):
        
        return [self.prob_classify(fs) for fs in featuresets]


class MultiClassifierI:
    

    def labels(self):
        
        raise NotImplementedError()

    def classify(self, featureset):
        
        if overridden(self.classify_many):
            return self.classify_many([featureset])[0]
        else:
            raise NotImplementedError()

    def prob_classify(self, featureset):
        
        if overridden(self.prob_classify_many):
            return self.prob_classify_many([featureset])[0]
        else:
            raise NotImplementedError()

    def classify_many(self, featuresets):
        
        return [self.classify(fs) for fs in featuresets]

    def prob_classify_many(self, featuresets):
        
        return [self.prob_classify(fs) for fs in featuresets]