Spaces:
Sleeping
Sleeping
import streamlit as st | |
from pyvis.network import Network | |
import networkx as nx | |
import random | |
import streamlit.components.v1 as components | |
st.title('ํํธ๋ ๋งค์นญ ํ๋ก๊ทธ๋จ') | |
st.write('์ด๋ฆ์ ํ ์ค์ ํ๋์ฉ ์ ๋ ฅํ์ธ์.') | |
names_input = st.text_area('์ด๋ฆ ๋ชฉ๋ก', height=200) | |
if st.button('ํํธ๋ ๋งค์นญํ๊ธฐ'): | |
names = [name.strip() for name in names_input.strip().split('\n') if name.strip()] | |
if len(names) < 2: | |
st.error('๋งค์นญํ ์ด๋ฆ์ด 2๊ฐ ์ด์ ํ์ํฉ๋๋ค.') | |
else: | |
random.shuffle(names) | |
mid = len(names) // 2 | |
group_A = names[:mid] | |
group_B = names[mid:] | |
# ๊ทธ๋ํ ์์ฑ | |
G = nx.Graph() | |
G.add_nodes_from(group_A, bipartite=0) | |
G.add_nodes_from(group_B, bipartite=1) | |
# ๋ชจ๋ ๊ฐ๋ฅํ ๊ฐ์ ์ถ๊ฐ | |
for a in group_A: | |
for b in group_B: | |
G.add_edge(a, b) | |
# ๋งค์นญ ์๊ณ ๋ฆฌ์ฆ ์คํ (์ต๋ ๋งค์นญ) | |
mate = nx.algorithms.bipartite.maximum_matching(G, top_nodes=group_A) | |
# ๋งค์นญ ๊ฒฐ๊ณผ๋ง์ผ๋ก ์๋ก์ด ๊ทธ๋ํ ์์ฑ | |
matching_edges = [(u, v) for u, v in mate.items() if u in group_A] | |
# pyvis ๋คํธ์ํฌ ์์ฑ | |
net = Network(height='600px', width='100%', bgcolor='#222222', font_color='white') | |
# ๋ ธ๋ ์ถ๊ฐ | |
for node in G.nodes(): | |
net.add_node(node, label=node, title=node) | |
# ๋งค์นญ๋ ๊ฐ์ ์ ๊ฐ์กฐํ์ฌ ์ถ๊ฐ | |
for edge in matching_edges: | |
net.add_edge(edge[0], edge[1], color='red', width=3) | |
# ๋คํธ์ํฌ ์ค์ | |
net.toggle_physics(True) | |
# HTML ์ฝ๋ ์์ฑ | |
net_html = net.generate_html() | |
# Streamlit์ ํ์ | |
components.html(net_html, height=600) | |
# ๋งค์นญ ๊ฒฐ๊ณผ ์ถ๋ ฅ | |
st.subheader('๋งค์นญ ๊ฒฐ๊ณผ:') | |
for u, v in matching_edges: | |
st.write(f'{u} - {v}') | |
unmatched = set(names) - set(mate.keys()) - set(mate.values()) | |
if unmatched: | |
st.write('๋งค์นญ๋์ง ์์ ์ฌ๋:') | |
for name in unmatched: | |
st.write(name) | |