File size: 2,493 Bytes
552b858
 
bbc0abd
552b858
 
bbc0abd
5e0e1bf
911d6f0
 
 
 
 
 
5e0e1bf
911d6f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import matplotlib.pyplot as plt
from wordcloud import WordCloud , STOPWORDS
import networkx as nx
import plotly.graph_objects as go
from src.metadata_extractor import looks_like_affiliation
from src.constants import STOP_WORDS
def generate_wordcloud(papers):
    text = " ".join([p.get("abstract", "") for p in papers if p.get("abstract")])
    if not text:
        st.info("No abstracts to build a word cloud.")
        return

    custom_stopwords = set(STOPWORDS).union(STOP_WORDS)
    wc = WordCloud(
        width=800,
        height=400,
        stopwords=custom_stopwords,
        background_color="white",
        colormap="viridis"
    ).generate(text)

    fig, ax = plt.subplots()
    ax.imshow(wc, interpolation="bilinear")
    ax.axis("off")
    st.pyplot(fig)


# ========== CO-AUTHOR GRAPH ==========
def build_coauthor_graph(papers):
    G = nx.Graph()

    for p in papers:
        authors = p.get("authors", ["Unknown"])
        # normalize
        authors = [a.strip() for a in authors if a and isinstance(a, str)]
        # filter obvious affiliation tokens
        authors = [a for a in authors if not looks_like_affiliation(a) and a.lower() != "unknown"]

        # add nodes
        for a in authors:
            G.add_node(a)

        # add edges (with weight)
        for i in range(len(authors)):
            for j in range(i + 1, len(authors)):
                u, v = authors[i], authors[j]
                if G.has_edge(u, v):
                    G[u][v]['weight'] += 1
                else:
                    G.add_edge(u, v, weight=1)

    if G.number_of_nodes() == 0:
        st.info("No author data available to build co-author graph.")
        return

    pos = nx.spring_layout(G, seed=42)
    edge_x, edge_y = [], []
    for u, v, data in G.edges(data=True):
        x0, y0 = pos[u]
        x1, y1 = pos[v]
        edge_x += [x0, x1, None]
        edge_y += [y0, y1, None]

    node_x, node_y, node_text = [], [], []
    for n in G.nodes():
        x, y = pos[n]
        node_x.append(x); node_y.append(y); node_text.append(n)

    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=edge_x, y=edge_y, mode='lines',
        line=dict(width=0.5, color='#888'),
        hoverinfo='none'
    ))
    fig.add_trace(go.Scatter(
        x=node_x, y=node_y, mode='markers+text',
        text=node_text, textposition="top center",
        marker=dict(size=12, color='skyblue')
    ))
    st.plotly_chart(fig, use_container_width=True)