api.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Natural Language Toolkit: Classifier Interface
  2. #
  3. # Copyright (C) 2001-2019 NLTK Project
  4. # Author: Edward Loper <edloper@gmail.com>
  5. # Steven Bird <stevenbird1@gmail.com> (minor additions)
  6. # URL: <http://nltk.org/>
  7. # For license information, see LICENSE.TXT
  8. """
  9. Interfaces for labeling tokens with category labels (or "class labels").
  10. ``ClassifierI`` is a standard interface for "single-category
  11. classification", in which the set of categories is known, the number
  12. of categories is finite, and each text belongs to exactly one
  13. category.
  14. ``MultiClassifierI`` is a standard interface for "multi-category
  15. classification", which is like single-category classification except
  16. that each text belongs to zero or more categories.
  17. """
  18. from nltk.internals import overridden
  19. ##//////////////////////////////////////////////////////
  20. # { Classification Interfaces
  21. ##//////////////////////////////////////////////////////
  22. class ClassifierI(object):
  23. """
  24. A processing interface for labeling tokens with a single category
  25. label (or "class"). Labels are typically strs or
  26. ints, but can be any immutable type. The set of labels
  27. that the classifier chooses from must be fixed and finite.
  28. Subclasses must define:
  29. - ``labels()``
  30. - either ``classify()`` or ``classify_many()`` (or both)
  31. Subclasses may define:
  32. - either ``prob_classify()`` or ``prob_classify_many()`` (or both)
  33. """
  34. def labels(self):
  35. """
  36. :return: the list of category labels used by this classifier.
  37. :rtype: list of (immutable)
  38. """
  39. raise NotImplementedError()
  40. def classify(self, featureset):
  41. """
  42. :return: the most appropriate label for the given featureset.
  43. :rtype: label
  44. """
  45. if overridden(self.classify_many):
  46. return self.classify_many([featureset])[0]
  47. else:
  48. raise NotImplementedError()
  49. def prob_classify(self, featureset):
  50. """
  51. :return: a probability distribution over labels for the given
  52. featureset.
  53. :rtype: ProbDistI
  54. """
  55. if overridden(self.prob_classify_many):
  56. return self.prob_classify_many([featureset])[0]
  57. else:
  58. raise NotImplementedError()
  59. def classify_many(self, featuresets):
  60. """
  61. Apply ``self.classify()`` to each element of ``featuresets``. I.e.:
  62. return [self.classify(fs) for fs in featuresets]
  63. :rtype: list(label)
  64. """
  65. return [self.classify(fs) for fs in featuresets]
  66. def prob_classify_many(self, featuresets):
  67. """
  68. Apply ``self.prob_classify()`` to each element of ``featuresets``. I.e.:
  69. return [self.prob_classify(fs) for fs in featuresets]
  70. :rtype: list(ProbDistI)
  71. """
  72. return [self.prob_classify(fs) for fs in featuresets]
  73. class MultiClassifierI(object):
  74. """
  75. A processing interface for labeling tokens with zero or more
  76. category labels (or "labels"). Labels are typically strs
  77. or ints, but can be any immutable type. The set of labels
  78. that the multi-classifier chooses from must be fixed and finite.
  79. Subclasses must define:
  80. - ``labels()``
  81. - either ``classify()`` or ``classify_many()`` (or both)
  82. Subclasses may define:
  83. - either ``prob_classify()`` or ``prob_classify_many()`` (or both)
  84. """
  85. def labels(self):
  86. """
  87. :return: the list of category labels used by this classifier.
  88. :rtype: list of (immutable)
  89. """
  90. raise NotImplementedError()
  91. def classify(self, featureset):
  92. """
  93. :return: the most appropriate set of labels for the given featureset.
  94. :rtype: set(label)
  95. """
  96. if overridden(self.classify_many):
  97. return self.classify_many([featureset])[0]
  98. else:
  99. raise NotImplementedError()
  100. def prob_classify(self, featureset):
  101. """
  102. :return: a probability distribution over sets of labels for the
  103. given featureset.
  104. :rtype: ProbDistI
  105. """
  106. if overridden(self.prob_classify_many):
  107. return self.prob_classify_many([featureset])[0]
  108. else:
  109. raise NotImplementedError()
  110. def classify_many(self, featuresets):
  111. """
  112. Apply ``self.classify()`` to each element of ``featuresets``. I.e.:
  113. return [self.classify(fs) for fs in featuresets]
  114. :rtype: list(set(label))
  115. """
  116. return [self.classify(fs) for fs in featuresets]
  117. def prob_classify_many(self, featuresets):
  118. """
  119. Apply ``self.prob_classify()`` to each element of ``featuresets``. I.e.:
  120. return [self.prob_classify(fs) for fs in featuresets]
  121. :rtype: list(ProbDistI)
  122. """
  123. return [self.prob_classify(fs) for fs in featuresets]
  124. # # [XX] IN PROGRESS:
  125. # class SequenceClassifierI(object):
  126. # """
  127. # A processing interface for labeling sequences of tokens with a
  128. # single category label (or "class"). Labels are typically
  129. # strs or ints, but can be any immutable type. The set
  130. # of labels that the classifier chooses from must be fixed and
  131. # finite.
  132. # """
  133. # def labels(self):
  134. # """
  135. # :return: the list of category labels used by this classifier.
  136. # :rtype: list of (immutable)
  137. # """
  138. # raise NotImplementedError()
  139. # def prob_classify(self, featureset):
  140. # """
  141. # Return a probability distribution over labels for the given
  142. # featureset.
  143. # If ``featureset`` is a list of featuresets, then return a
  144. # corresponding list containing the probability distribution
  145. # over labels for each of the given featuresets, where the
  146. # *i*\ th element of this list is the most appropriate label for
  147. # the *i*\ th element of ``featuresets``.
  148. # """
  149. # raise NotImplementedError()
  150. # def classify(self, featureset):
  151. # """
  152. # Return the most appropriate label for the given featureset.
  153. # If ``featureset`` is a list of featuresets, then return a
  154. # corresponding list containing the most appropriate label for
  155. # each of the given featuresets, where the *i*\ th element of
  156. # this list is the most appropriate label for the *i*\ th element
  157. # of ``featuresets``.
  158. # """
  159. # raise NotImplementedError()