-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster.py
144 lines (111 loc) · 4.62 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import re
import time
from bertopic import BERTopic
from bertopic.vectorizers import OnlineCountVectorizer, ClassTfidfTransformer
from river import stream, cluster
from umap import UMAP
from datetime import datetime
class RiverCluster:
def __init__(self, model):
"""
Initialize the RiverCluster class.
Args:
model: A clustering model from the river library.
"""
self.model = model
def partial_fit(self, umap_embeddings):
"""
Partially fit the clustering model with UMAP embeddings.
Args:
umap_embeddings: UMAP embeddings of the data.
Returns:
self: The updated instance of the RiverCluster class.
"""
for umap_embedding, _ in stream.iter_array(umap_embeddings):
self.model = self.model.learn_one(umap_embedding)
labels = []
for umap_embedding, _ in stream.iter_array(umap_embeddings):
label = self.model.predict_one(umap_embedding)
labels.append(label)
self.labels_ = labels
return self
class TopicModeling:
def __init__(self, file_path):
"""
Initialize the TopicModeling class.
Args:
file_path: The path to the file for saving the model.
"""
self.file_path = file_path
def clean_text(self, text):
"""
Clean the text by removing URLs, mentions, and hashtags.
Args:
text: The input text.
Returns:
The cleaned text.
"""
text = re.sub(r'http\S+', '', text, flags=re.MULTILINE)
text = re.sub("@[A-Za-z0-9_]+", "", text)
text = re.sub("#[A-Za-z0-9_]+", "", text)
return text
def save_model(self):
"""
Save the topic modeling model.
Raises:
NotImplementedError: This method should be implemented in subclasses.
"""
raise NotImplementedError("Subclasses should implement this method.")
class BERTopicModel(TopicModeling):
def __init__(self, file_path):
super().__init__(file_path)
if os.path.isfile(file_path):
self.model = BERTopic.load(file_path)
print ('Clustering model loaded from file.')
else:
cluster_model = RiverCluster(cluster.DBSTREAM(fading_factor = 0.05))
vectorizer_model = OnlineCountVectorizer(stop_words="english")
ctfidf_model = ClassTfidfTransformer(reduce_frequent_words=True, bm25_weighting=True)
self.model = BERTopic(
hdbscan_model=cluster_model,
vectorizer_model=vectorizer_model,
ctfidf_model=ctfidf_model,
language='english',
nr_topics='auto',
verbose=True
)
print ('Clustering model initialized.')
def online_topic_modeling(self, tweet_dict):
"""
Perform online topic modeling on a dictionary of tweets.
Args:
tweet_dict: A list of dictionaries containing tweet information.
Returns:
A tuple containing the updated tweet dictionary with assigned topic labels and a dictionary of topic statistics.
"""
textlist = [self.clean_text(d['text']) for d in tweet_dict]
self.model.partial_fit(textlist)
topic_keywords = {k: v.split('_')[1:] for k, v in self.model.topic_labels_.items()}
freq_df = self.model.get_topic_freq().loc[self.model.get_topic_freq().Topic != -1, :]
topics = sorted(freq_df.Topic.to_list())
all_topics = sorted(list(self.model.get_topics().keys()))
indices = [all_topics.index(topic) for topic in topics]
embeddings = [self.model.topic_embeddings_[i] for i in indices]
embeddings = UMAP(n_neighbors=2, n_components=2, metric='cosine', random_state=42).fit_transform(embeddings)
topic_coordinates = {i: embeddings[i].tolist() for i in indices}
topic_sizes = dict(sorted(self.model.topic_sizes_.items()))
#current_time = time.strftime("%Y-%m-%d %H:%M:%S")
current_time = datetime.utcnow()
topic_list = [{'topic_label': key, 'size': topic_sizes[key], 'coordinates': topic_coordinates[key], 'keywords': topic_keywords[key]} for key in topic_keywords]
topic_stats = {'time': current_time, 'topics': topic_list}
for d in tweet_dict:
d.pop('text', None)
for d, value in zip(tweet_dict, self.model.topics_):
d['topic_label'] = value
return tweet_dict, topic_stats
def save_model(self):
"""
Save the BERTopic model.
"""
self.model.save(self.file_path)