-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_cluster.py
More file actions
52 lines (38 loc) · 1.4 KB
/
Copy pathabstract_cluster.py
File metadata and controls
52 lines (38 loc) · 1.4 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
# -*- coding: utf-8 -*-
"""Untitled11.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/10YsYsZnP4jzpkptIzqGPP6Sf2UiE2SZ7
"""
!pip install bibtexparser pandas sentence-transformers umap-learn matplotlib
import bibtexparser
import pandas as pd
# Load .bib file
with open("savedrecs.bib", encoding="utf-8") as bibtex_file:
bib_database = bibtexparser.load(bibtex_file)
# Convert entries to DataFrame
df_bib = pd.DataFrame(bib_database.entries)
# Show basic structure
df_bib[['title', 'abstract']].dropna().head()
from sentence_transformers import SentenceTransformer
import umap
import matplotlib.pyplot as plt
# Load transformer model
model = SentenceTransformer('allenai-specter') # SciBERT model trained for science
# Clean and extract abstracts
abstracts = df_bib['abstract'].dropna().tolist()
# Load transformer model
model = SentenceTransformer('allenai-specter') # SciBERT model trained for science
# Encode abstracts
embeddings = model.encode(abstracts, show_progress_bar=True)
# Dimensionality reduction for plotting
reducer = umap.UMAP(n_neighbors=10, min_dist=0.3, metric='cosine')
embedding_2d = reducer.fit_transform(embeddings)
# Visualize
plt.figure(figsize=(10, 7))
plt.scatter(embedding_2d[:, 0], embedding_2d[:, 1], alpha=0.6)
plt.title("Semantic Clustering of Abstracts")
plt.xlabel("UMAP-1")
plt.ylabel("UMAP-2")
plt.grid(True)
plt.show()