forked from tazeek/BullyDetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict_cluster.py
43 lines (30 loc) · 1.1 KB
/
dict_cluster.py
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
import pickle
import os
os.system('cls')
# Specify the file
cluster = 400
FILE = "K-Means Models/full_" + str(cluster) + "C.pk"
# Load using pickle
word_centroid_map = pickle.load(open(FILE,"rb"))
# Get the total number of clusters
total_clusters = max(word_centroid_map.values()) + 1
# Cluster all the words from various indexes as one. Then store them as dictionary
# Dictionary contents: Cluster number, cluster contents
array_dict = []
# Loop cluster by cluster
for cluster_num in range(0, total_clusters):
# Progress report for every 50 clusters
if (len(array_dict) % 50 == 0):
print("%i clusters stored" % (len(array_dict)))
# Create a dictionary
cluster_dict = {}
# Get word list
word_list = [ word for word, cluster in word_centroid_map.items() if cluster == cluster_num ]
# Store in dictionary
cluster_dict["cluster_num"] = cluster_num
cluster_dict["word_list"] = word_list
# Append to array
array_dict.append(cluster_dict)
# Save the array
FILE = "Word Dictionaries/dict_" + str(cluster) + "C.pk"
pickle.dump(array_dict, open(FILE, "wb"))