Spaces:
Sleeping
Sleeping
File size: 6,625 Bytes
d916065 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# Natural Language Toolkit: Classifier Interface
#
# Copyright (C) 2001-2023 NLTK Project
# Author: Edward Loper <[email protected]>
# Steven Bird <[email protected]> (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.
"""
from nltk.internals import overridden
##//////////////////////////////////////////////////////
# { Classification Interfaces
##//////////////////////////////////////////////////////
class ClassifierI:
"""
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)
"""
def labels(self):
"""
:return: the list of category labels used by this classifier.
:rtype: list of (immutable)
"""
raise NotImplementedError()
def classify(self, featureset):
"""
:return: the most appropriate label for the given featureset.
:rtype: label
"""
if overridden(self.classify_many):
return self.classify_many([featureset])[0]
else:
raise NotImplementedError()
def prob_classify(self, featureset):
"""
:return: a probability distribution over labels for the given
featureset.
:rtype: ProbDistI
"""
if overridden(self.prob_classify_many):
return self.prob_classify_many([featureset])[0]
else:
raise NotImplementedError()
def classify_many(self, featuresets):
"""
Apply ``self.classify()`` to each element of ``featuresets``. I.e.:
return [self.classify(fs) for fs in featuresets]
:rtype: list(label)
"""
return [self.classify(fs) for fs in featuresets]
def prob_classify_many(self, featuresets):
"""
Apply ``self.prob_classify()`` to each element of ``featuresets``. I.e.:
return [self.prob_classify(fs) for fs in featuresets]
:rtype: list(ProbDistI)
"""
return [self.prob_classify(fs) for fs in featuresets]
class MultiClassifierI:
"""
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)
"""
def labels(self):
"""
:return: the list of category labels used by this classifier.
:rtype: list of (immutable)
"""
raise NotImplementedError()
def classify(self, featureset):
"""
:return: the most appropriate set of labels for the given featureset.
:rtype: set(label)
"""
if overridden(self.classify_many):
return self.classify_many([featureset])[0]
else:
raise NotImplementedError()
def prob_classify(self, featureset):
"""
:return: a probability distribution over sets of labels for the
given featureset.
:rtype: ProbDistI
"""
if overridden(self.prob_classify_many):
return self.prob_classify_many([featureset])[0]
else:
raise NotImplementedError()
def classify_many(self, featuresets):
"""
Apply ``self.classify()`` to each element of ``featuresets``. I.e.:
return [self.classify(fs) for fs in featuresets]
:rtype: list(set(label))
"""
return [self.classify(fs) for fs in featuresets]
def prob_classify_many(self, featuresets):
"""
Apply ``self.prob_classify()`` to each element of ``featuresets``. I.e.:
return [self.prob_classify(fs) for fs in featuresets]
:rtype: list(ProbDistI)
"""
return [self.prob_classify(fs) for fs in featuresets]
# # [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()
|