Repository navigation
Expand file tree
/
Copy pathcluster_documents.py
More file actions
64 lines (49 loc) · 1.81 KB
/
Copy pathcluster_documents.py
File metadata and controls
64 lines (49 loc) · 1.81 KB
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
# cluster_documents.py
import json
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
def load_index():
with open("index.json") as f:
data = json.load(f)
index = data["index"]
docs = data["documents"]
return index, docs
def reconstruct_docs(index, docs):
doc_texts = {}
for term, posting in index.items():
for doc_id, freq in posting.items():
doc_texts.setdefault(doc_id, [])
doc_texts[doc_id].extend([term] * freq)
# Only keep documents that actually have tokens
valid_docs = []
valid_texts = []
for d in docs:
if d in doc_texts and len(doc_texts[d]) > 0:
valid_docs.append(d)
valid_texts.append(" ".join(doc_texts[d]))
print(f"Using {len(valid_docs)} documents for clustering")
return valid_texts, valid_docs
def cluster_texts(texts, doc_ids, k):
vectorizer = TfidfVectorizer(max_features=5000)
X = vectorizer.fit_transform(texts)
model = KMeans(n_clusters=k, random_state=42)
labels = model.fit_predict(X)
terms = vectorizer.get_feature_names_out()
centers = model.cluster_centers_
clusters = {}
for cluster_idx in range(k):
weights = centers[cluster_idx]
top_idx = weights.argsort()[::-1][:50]
clusters[cluster_idx] = [terms[i] for i in top_idx]
return clusters, labels
def main():
index, docs = load_index()
texts, doc_ids = reconstruct_docs(index, docs)
for k in [2, 10, 20]:
clusters, labels = cluster_texts(texts, doc_ids, k)
print(f"\n======== K = {k} ========")
for c, terms in clusters.items():
print(f"\nCluster {c}:")
print(", ".join(terms))
if __name__ == "__main__":
main()