"""
This is a simple application for sentence embeddings: clustering

Sentences are mapped to sentence embeddings and then k-mean clustering is applied.
"""
from sentence_transformers import SentenceTransformer
import scipy.cluster

embedder = SentenceTransformer('bert-base-nli-mean-tokens')

# Corpus with example sentences
corpus = ['A man is eating a food.',
          'A man is eating a piece of bread.',
          'A man is eating pasta.',
          'The girl is carrying a baby.',
          'The baby is carried by the woman',
          'A man is riding a horse.',
          'A man is riding a white horse on an enclosed ground.',
          'A monkey is playing drums.',
          'Someone in a gorilla costume is playing a set of drums.',
          'A cheetah is running behind its prey.',
          'A cheetah chases prey on across a field.'
          ]
corpus_embeddings = embedder.encode(corpus)

# Perform kmean clustering
num_clusters = 5
whitened_corpus = scipy.cluster.vq.whiten(corpus_embeddings)
code_book, _ = scipy.cluster.vq.kmeans(whitened_corpus, num_clusters)
cluster_assignment, _ = scipy.cluster.vq.vq(whitened_corpus, code_book)

clustered_sentences = [[] for i in range(num_clusters)]
for sentence_id, cluster_id in enumerate(cluster_assignment):
    clustered_sentences[cluster_id].append(corpus[sentence_id])

for i, cluster in enumerate(clustered_sentences):
    print("Cluster ", i+1)
    print(cluster)
    print("")

@@ -208,28 +208,59 @@
 mer%0A
-import scipy.c
+from sklearn.cluster import AgglomerativeC
 luster
+ing
 %0A%0Aem
@@ -980,210 +980,161 @@
 = 5%0A
-whitened_corpus = scipy.cluster.vq.whiten(corpus_embeddings)%0Acode_book, _ = scipy.cluster.vq.kmeans(whitened_corpus, num_clusters)%0Acluster_assignment, _ = scipy.cluster.vq.vq(whitened_corpus, code_book)
+clustering_model = AgglomerativeClustering(n_clusters=num_clusters)%0Aclustering_model.fit(corpus_embeddings)%0Acluster_assignment = clustering_model.labels_
 %0A%0Acl

Replace scipy clustering with sklearn clustering