3454a55636
cffconvert / validate (push) Has been cancelled
ci-workflow / pre-commit (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (macos-latest) (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (ubuntu-latest) (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (windows-latest) (push) Has been cancelled
ci-workflow / Python 3.10 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.11 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.12 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.13 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.14 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.14t on macos-latest (push) Has been cancelled
ci-workflow / Python 3.10 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.11 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.12 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.13 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.14 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.14t on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.10 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.11 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.12 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.13 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.14 on windows-latest (push) Has been cancelled
75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
# Natural Language Toolkit: Clusterer Interfaces
|
|
#
|
|
# Copyright (C) 2001-2026 NLTK Project
|
|
# Author: Trevor Cohn <tacohn@cs.mu.oz.au>
|
|
# Porting: Steven Bird <stevenbird1@gmail.com>
|
|
# URL: <https://www.nltk.org/>
|
|
# For license information, see LICENSE.TXT
|
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
from nltk.probability import DictionaryProbDist
|
|
|
|
|
|
class ClusterI(metaclass=ABCMeta):
|
|
"""
|
|
Interface covering basic clustering functionality.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def cluster(self, vectors, assign_clusters=False):
|
|
"""
|
|
Assigns the vectors to clusters, learning the clustering parameters
|
|
from the data. Returns a cluster identifier for each vector.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def classify(self, token):
|
|
"""
|
|
Classifies the token into a cluster, setting the token's CLUSTER
|
|
parameter to that cluster identifier.
|
|
"""
|
|
|
|
def likelihood(self, vector, label):
|
|
"""
|
|
Returns the likelihood (a float) of the token having the
|
|
corresponding cluster.
|
|
"""
|
|
if self.classify(vector) == label:
|
|
return 1.0
|
|
else:
|
|
return 0.0
|
|
|
|
def classification_probdist(self, vector):
|
|
"""
|
|
Classifies the token into a cluster, returning
|
|
a probability distribution over the cluster identifiers.
|
|
"""
|
|
likelihoods = {}
|
|
sum = 0.0
|
|
for cluster in self.cluster_names():
|
|
likelihoods[cluster] = self.likelihood(vector, cluster)
|
|
sum += likelihoods[cluster]
|
|
for cluster in self.cluster_names():
|
|
likelihoods[cluster] /= sum
|
|
return DictionaryProbDist(likelihoods)
|
|
|
|
@abstractmethod
|
|
def num_clusters(self):
|
|
"""
|
|
Returns the number of clusters.
|
|
"""
|
|
|
|
def cluster_names(self):
|
|
"""
|
|
Returns the names of the clusters.
|
|
:rtype: list
|
|
"""
|
|
return list(range(self.num_clusters()))
|
|
|
|
def cluster_name(self, index):
|
|
"""
|
|
Returns the names of the cluster at index.
|
|
"""
|
|
return index
|