File size: 18,038 Bytes
e5b7184
 
 
 
 
 
 
 
 
 
 
c8c01fa
 
 
 
 
 
 
 
 
a3849be
 
c8c01fa
e5b7184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8c01fa
e5b7184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8c01fa
 
 
e5b7184
 
 
 
 
 
 
 
 
a3849be
e5b7184
a3849be
 
e5b7184
 
 
 
 
 
 
 
 
a3849be
e5b7184
 
a3849be
e5b7184
 
 
 
 
 
 
 
 
 
 
 
 
 
c8c01fa
 
 
 
 
 
 
 
a3849be
e5b7184
 
 
 
 
 
c8c01fa
e5b7184
 
 
 
 
c8c01fa
 
 
 
 
a3849be
 
 
 
 
e5b7184
 
 
a3849be
e5b7184
 
a3849be
 
 
 
 
 
 
 
 
 
e5b7184
 
c8c01fa
 
 
 
a3849be
c8c01fa
 
a3849be
c8c01fa
e5b7184
a3849be
 
c8c01fa
e5b7184
a3849be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b7184
 
a3849be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b7184
 
 
 
a3849be
 
 
e5b7184
c8c01fa
e5b7184
 
 
 
a3849be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b7184
a3849be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b7184
a3849be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b7184
a3849be
 
 
 
 
e5b7184
 
 
 
 
c8c01fa
 
 
 
 
 
e5b7184
a3849be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b7184
 
 
 
 
a3849be
e5b7184
 
 
 
c8c01fa
e5b7184
c8c01fa
 
 
 
e5b7184
 
a3849be
 
 
 
c8c01fa
a3849be
 
e5b7184
a3849be
 
e5b7184
 
a3849be
e5b7184
 
 
a3849be
e5b7184
 
 
 
 
 
 
 
 
c8c01fa
 
e5b7184
 
 
 
 
 
c8c01fa
e5b7184
 
 
 
 
 
 
 
 
 
 
 
 
 
c8c01fa
e5b7184
 
 
c8c01fa
e5b7184
 
c8c01fa
e5b7184
a3849be
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
"""
tools.py
--------
Topic modeling module using BERTopic for analyzing research paper abstracts and titles.
"""

import re
import logging
import pandas as pd
from typing import Optional

from bertopic import BERTopic
from sentence_transformers import SentenceTransformer
from umap import UMAP
from hdbscan import HDBSCAN                          # --- Cluster Balancing Logic ---
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
from nltk.corpus import stopwords
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from collections import defaultdict, Counter

# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logging.basicConfig(level=logging.INFO, format="%(levelname)s | %(message)s")
logger = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
def _ensure_nltk_stopwords() -> None:
    try:
        stopwords.words("english")
    except LookupError:
        nltk.download("stopwords", quiet=True)


# ---------------------------------------------------------------------------
# Data Loading
# ---------------------------------------------------------------------------
def load_csv(filepath: str) -> pd.DataFrame:
    df = pd.read_csv(filepath)
    required_cols = {"title", "abstract"}
    missing = required_cols - set(df.columns.str.lower())
    if missing:
        raise ValueError(f"CSV is missing required column(s): {missing}")

    df.columns = df.columns.str.lower()
    logger.info("Loaded %d rows from '%s'.", len(df), filepath)
    return df


# ---------------------------------------------------------------------------
# Preprocessing
# ---------------------------------------------------------------------------
def preprocess_text(texts: pd.Series) -> list[str]:
    _ensure_nltk_stopwords()
    stop_words = set(stopwords.words("english"))

    cleaned: list[str] = []
    for raw in texts.fillna(""):
        text = raw.lower()
        text = re.sub(r"[^a-z\s]", " ", text)
        tokens = text.split()
        tokens = [t for t in tokens if t not in stop_words and len(t) > 1]
        cleaned.append(" ".join(tokens))

    logger.info("Preprocessed %d documents.", len(cleaned))
    return cleaned


# ---------------------------------------------------------------------------
# Model Construction
# ---------------------------------------------------------------------------
def build_bertopic_model(embedding_model: SentenceTransformer, min_topic_size: int = 5) -> BERTopic:
    # --- Cluster Balancing Logic ---
    # (embedding_model is passed explicitly from run_topic_modeling)

    umap_model = UMAP(
        n_neighbors=15,
        n_components=5,
        min_dist=0.0,
        metric="cosine",
        random_state=42,
    )

    # Updated HDBSCAN constraints
    hdbscan_model = HDBSCAN(
        min_cluster_size=5,
        min_samples=3,
        metric="euclidean",
        cluster_selection_method="eom",
        prediction_data=True,
    )

    model = BERTopic(
        embedding_model=embedding_model,
        umap_model=umap_model,
        hdbscan_model=hdbscan_model,
        min_topic_size=5,
        verbose=False,
    )
    logger.info("BERTopic model created with HDBSCAN (min_cluster_size=5, min_samples=3).")
    return model


# ---------------------------------------------------------------------------
# Cluster Balancing Logic
# ---------------------------------------------------------------------------
def _get_cluster_sizes(topics: list[int]) -> dict[int, int]:
    sizes: dict[int, int] = {}
    for t in topics:
        if t != -1:
            sizes[t] = sizes.get(t, 0) + 1
    return sizes


def _split_large_cluster(
    topic_id: int,
    doc_indices: list[int],
    embeddings: np.ndarray,
    topics: list[int],
    next_id: int,
) -> int:
    """Split an oversized cluster into 2 sub-clusters via KMeans. Returns next available ID."""
    if len(doc_indices) < 10:  # Minimum threshold to split
        return next_id
    sub_embs = embeddings[doc_indices]
    km = KMeans(n_clusters=2, random_state=42, n_init=5)
    labels = km.fit_predict(sub_embs)
    new_id = next_id
    for local_idx, global_idx in enumerate(doc_indices):
        if labels[local_idx] == 1:          # half goes to a new cluster ID
            topics[global_idx] = new_id
    logger.info("Split large cluster %d → kept %d, created %d.", topic_id, topic_id, new_id)
    return next_id + 1


def _merge_small_cluster(
    topic_id: int,
    doc_indices: list[int],
    cluster_centroids: dict[int, np.ndarray],
    topics: list[int],
    similarity_threshold: float = 0.5,
) -> bool:
    """Merge a tiny cluster into the nearest cluster by cosine similarity if threshold met."""
    if not cluster_centroids or topic_id not in cluster_centroids:
        return False
    src_centroid = cluster_centroids[topic_id].reshape(1, -1)
    other_ids = [tid for tid in cluster_centroids if tid != topic_id]
    if not other_ids:
        return False
    other_centroids = np.vstack([cluster_centroids[tid] for tid in other_ids])
    sims = cosine_similarity(src_centroid, other_centroids)[0]
    best_idx = int(np.argmax(sims))
    max_sim = sims[best_idx]
    
    if max_sim >= similarity_threshold:
        nearest = other_ids[best_idx]
        for idx in doc_indices:
            topics[idx] = nearest
        logger.info("Merged small cluster %d → cluster %d (sim=%.2f).", topic_id, nearest, max_sim)
        return True
    return False


def balance_clusters(
    topics: list[int],
    documents: list[str],
    embedding_model: SentenceTransformer,
    embeddings: Optional[np.ndarray] = None,
) -> list[int]:
    """
    Enforce cluster size limits: MIN=5, MAX=30.
    """
    try:
        if embeddings is None:
            embeddings = embedding_model.encode(documents, show_progress_bar=False)

        topics = list(topics)
        MIN_CLUSTER_SIZE = 5
        MAX_CLUSTER_SIZE = 30

        for _ in range(3):  # Iterative refinement
            sizes = _get_cluster_sizes(topics)
            if not sizes:
                break

            cluster_docs: dict[int, list[int]] = {}
            for idx, tid in enumerate(topics):
                if tid != -1:
                    cluster_docs.setdefault(tid, []).append(idx)

            centroids: dict[int, np.ndarray] = {
                tid: embeddings[idxs].mean(axis=0)
                for tid, idxs in cluster_docs.items()
            }

            next_id = max(sizes.keys()) + 1 if sizes else 0
            changed = False

            # Split oversized clusters
            for tid, size in list(sizes.items()):
                if size > MAX_CLUSTER_SIZE:
                    old_next_id = next_id
                    next_id = _split_large_cluster(
                        tid, cluster_docs[tid], embeddings, topics, next_id
                    )
                    if next_id > old_next_id:
                        changed = True

            # Merge undersized clusters
            sizes = _get_cluster_sizes(topics)
            for tid, size in list(sizes.items()):
                if size < MIN_CLUSTER_SIZE and tid in cluster_docs:
                    if _merge_small_cluster(tid, cluster_docs[tid], centroids, topics, similarity_threshold=0.5):
                        changed = True
            
            if not changed:
                break

        return topics
    except Exception as e:
        logger.error("Cluster balancing error: %s", e)
        return topics


def enforce_total_clusters(
    topics: list[int],
    embeddings: np.ndarray,
    min_clusters: int = 15,
    max_clusters: int = 30,
) -> list[int]:
    """Iteratively split or merge to keep total clusters between 15 and 30."""
    topics = list(topics)
    
    while True:
        unique_clusters = [t for t in set(topics) if t != -1]
        count = len(unique_clusters)
        
        if min_clusters <= count <= max_clusters:
            break
            
        cluster_docs: dict[int, list[int]] = {}
        for idx, tid in enumerate(topics):
            if tid != -1:
                cluster_docs.setdefault(tid, []).append(idx)
        
        if not cluster_docs:
            break

        centroids: dict[int, np.ndarray] = {
            tid: embeddings[idxs].mean(axis=0)
            for tid, idxs in cluster_docs.items()
        }

        if count > max_clusters:
            # Merge two closest clusters
            ids = list(centroids.keys())
            c_matrix = np.vstack([centroids[tid] for tid in ids])
            sim_matrix = cosine_similarity(c_matrix)
            np.fill_diagonal(sim_matrix, -1)
            
            i, j = np.unravel_index(np.argmax(sim_matrix), sim_matrix.shape)
            tid_i, tid_j = ids[i], ids[j]
            
            # Merge tid_i into tid_j
            for idx in cluster_docs[tid_i]:
                topics[idx] = tid_j
            logger.info("Reduced clusters: Merged %d and %d (count: %d -> %d)", tid_i, tid_j, count, count-1)
            
        elif count < min_clusters:
            # Split largest cluster
            sizes = _get_cluster_sizes(topics)
            largest_tid = max(sizes, key=sizes.get)
            next_id = max(unique_clusters) + 1
            _split_large_cluster(largest_tid, cluster_docs[largest_tid], embeddings, topics, next_id)
            logger.info("Increased clusters: Split %d (count: %d -> %d)", largest_tid, count, count+1)
            
    final_count = len([t for t in set(topics) if t != -1])
    logger.info("Final cluster count: %d", final_count)
    print(f"Final cluster count: {final_count}")
            
    return topics


def get_top_3_central_docs(
    topics: list[int],
    embeddings: np.ndarray,
    documents: list[str],
) -> dict[int, list[str]]:
    """Select top 3 documents closest to centroid for each topic."""
    cluster_docs_idx: dict[int, list[int]] = {}
    for idx, tid in enumerate(topics):
        if tid != -1:
            cluster_docs_idx.setdefault(tid, []).append(idx)
            
    representative_docs = {}
    for tid, idxs in cluster_docs_idx.items():
        cluster_embs = embeddings[idxs]
        centroid = cluster_embs.mean(axis=0).reshape(1, -1)
        sims = cosine_similarity(centroid, cluster_embs)[0]
        
        # Get top 3 indices
        top_local_idxs = np.argsort(sims)[-3:][::-1]
        representative_docs[tid] = [documents[idxs[li]] for li in top_local_idxs]
        
    return representative_docs


def rebuild_topic_keywords(
    topics: list[int],
    documents: list[str],
) -> dict[int, list[tuple[str, float]]]:
    """
    Rebuild topic keywords based on updated cluster assignments using CountVectorizer.
    Skips clusters with fewer than 3 documents.
    """
    cluster_docs: dict = defaultdict(list)
    for doc, t in zip(documents, topics):
        if t != -1:
            cluster_docs[t].append(doc)

    topic_keywords = {}
    for topic_id, docs in cluster_docs.items():
        if len(docs) < 2:
            continue
        vectorizer = CountVectorizer(stop_words='english', max_features=50)
        try:
            X = vectorizer.fit_transform(docs)
            words = vectorizer.get_feature_names_out()
            scores = X.sum(axis=0).A1
            top_idx = scores.argsort()[::-1][:10]
            topic_keywords[topic_id] = [
                (words[i], float(scores[i])) for i in top_idx
            ]
        except Exception as e:
            logger.warning("rebuild_topic_keywords failed for topic %d: %s", topic_id, e)

    return topic_keywords


def reassign_outliers(
    topics: list[int],
    embeddings: np.ndarray,
    similarity_threshold: float = 0.5,
) -> list[int]:
    """
    Reassign outlier documents (topic == -1) to the nearest cluster centroid
    if cosine similarity >= similarity_threshold AND cluster size < MAX_CLUSTER_SIZE.
    Otherwise keep as -1.
    """
    topics = list(topics)
    MAX_CLUSTER_SIZE = 100  # Per instructor spec: max 100 papers per cluster

    # Build centroid map and current sizes
    cluster_docs: dict[int, list[int]] = {}
    current_sizes: dict[int, int] = {}
    for idx, tid in enumerate(topics):
        if tid != -1:
            cluster_docs.setdefault(tid, []).append(idx)
            current_sizes[tid] = current_sizes.get(tid, 0) + 1

    if not cluster_docs:
        return topics

    cluster_ids = list(cluster_docs.keys())
    centroids = np.vstack([
        embeddings[cluster_docs[tid]].mean(axis=0)
        for tid in cluster_ids
    ])  # shape: (n_clusters, embed_dim)

    outlier_indices = [idx for idx, tid in enumerate(topics) if tid == -1]
    reassigned = 0

    for idx in outlier_indices:
        doc_emb = embeddings[idx].reshape(1, -1)
        sims = cosine_similarity(doc_emb, centroids)[0]  # (n_clusters,)
        best_i = int(np.argmax(sims))
        
        target_tid = cluster_ids[best_i]
        if sims[best_i] >= similarity_threshold and current_sizes.get(target_tid, 0) < MAX_CLUSTER_SIZE:
            topics[idx] = target_tid
            current_sizes[target_tid] = current_sizes.get(target_tid, 0) + 1
            reassigned += 1

    logger.info(
        "Outlier reassignment: %d / %d outliers reassigned (threshold=%.2f, max_size=%d).",
        reassigned, len(outlier_indices), similarity_threshold, MAX_CLUSTER_SIZE
    )
    return topics


# ---------------------------------------------------------------------------
# Topic Extraction
# ---------------------------------------------------------------------------
def extract_topics(
    model: BERTopic,
    documents: list[str],
    embedding_model: SentenceTransformer,
) -> dict:

    valid_docs = [d if d.strip() else "empty" for d in documents]
    embeddings = embedding_model.encode(valid_docs, show_progress_bar=False)

    topics, _ = model.fit_transform(valid_docs, embeddings=embeddings)

    # 1. Balance cluster sizes (5-30)
    topics = balance_clusters(topics, valid_docs, embedding_model, embeddings=embeddings)
    
    # 2. Enforce total cluster count (15-30)
    topics = enforce_total_clusters(topics, embeddings, min_clusters=15, max_clusters=30)

    # 3. Reassign outliers to nearest cluster (threshold=0.55)
    topics = reassign_outliers(topics, embeddings, similarity_threshold=0.55)

    # 3.5 Re-balance after reassignment (Ensures clusters remain within limits)
    topics = balance_clusters(topics, valid_docs, embedding_model, embeddings=embeddings)

    # 4. Rebuild keywords from final cluster assignments
    topic_keywords = rebuild_topic_keywords(topics, valid_docs)
    
    # 5. Recompute topic_freq from FINAL topics
    topic_freq = Counter(t for t in topics if t != -1)
    
    # 6. Get top-3 central documents
    representative_docs = get_top_3_central_docs(topics, embeddings, documents)

    # Final Validation & Logs
    total_docs = len(topics)
    total_counted = sum(topic_freq.values())
    print(f"total_docs = {total_docs}")
    print(f"total_counted = {total_counted}")
    
    final_cluster_count = len([t for t in set(topics) if t != -1])
    final_topic_count = len(topic_keywords)
    
    print(f"Cluster count: {final_cluster_count}")
    print(f"Topic count: {final_topic_count}")
    
    if final_cluster_count != final_topic_count:
        logger.error(f"CONSISTENCY ERROR: {final_cluster_count} clusters != {final_topic_count} topics")

    return {
        "topics": topics,
        "topic_keywords": topic_keywords,
        "topic_freq": topic_freq,
        "representative_docs": representative_docs,
    }


# ---------------------------------------------------------------------------
# High-Level Pipeline
# ---------------------------------------------------------------------------
def run_topic_modeling(
    filepath: str,
    min_topic_size: int = 5,
) -> dict:

    df = load_csv(filepath)
    
    # Combined column
    df["combined"] = df["title"].fillna("") + ". " + df["abstract"].fillna("")
    clean_docs = preprocess_text(df["combined"])

    # New embedding model
    embedding_model = SentenceTransformer("allenai/specter2_base")

    model = build_bertopic_model(embedding_model, min_topic_size=min_topic_size)
    results = extract_topics(model, clean_docs, embedding_model)

    return {
        "documents": results
    }



# ---------------------------------------------------------------------------
# Pretty Printing Helper
# ---------------------------------------------------------------------------
def print_results(results: dict, top_n_keywords: int = 10) -> None:
    for section, data in results.items():
        print(f"\n{'='*60}")
        print(f"  Topic Modeling Results – {section.upper()}")
        print(f"{'='*60}")

        keywords: dict = data["topic_keywords"]
        freq: dict = data["topic_freq"]

        if not keywords:
            print("  No topics found.")
            continue

        for topic_id, words in sorted(keywords.items()):
            count = freq.get(topic_id, 0)
            kw_str = ", ".join(w for w, _ in words[:top_n_keywords])
            print(f"\n  Topic {topic_id:>3}  |  docs: {count:>4}")
            print(f"  Keywords : {kw_str}")

        outlier_count = freq.get(-1, 0)
        if outlier_count:
            print(f"\n  Outlier topic (-1): {outlier_count} document(s).")


# ---------------------------------------------------------------------------
# CLI Entry Point
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    import sys

    if len(sys.argv) < 2:
        print("Usage: python tools.py <path_to_csv> [min_topic_size]")
        sys.exit(1)

    csv_path = sys.argv[1]
    mts = int(sys.argv[2]) if len(sys.argv) > 2 else 5

    output = run_topic_modeling(csv_path, min_topic_size=mts)
    print_results(output)