soojeongcrystal commited on
Commit
da98c71
·
verified ·
1 Parent(s): 5414a73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -38,25 +38,6 @@ def generate_colors(n):
38
  HSV_tuples = [(x * 1.0 / n, 0.5, 0.9) for x in range(n)]
39
  return ['#%02x%02x%02x' % tuple(int(x*255) for x in colorsys.hsv_to_rgb(*hsv)) for hsv in HSV_tuples]
40
 
41
- def create_network_graph(topic_results, num_words=10):
42
- G = nx.Graph()
43
- colors = generate_colors(len(topic_results))
44
-
45
- for idx, topic in enumerate(topic_results):
46
- words = topic['lda_words'][:num_words]
47
- color = colors[idx]
48
-
49
- for word in words:
50
- if not G.has_node(word):
51
- G.add_node(word, color=color)
52
-
53
- for i in range(len(words)):
54
- for j in range(i+1, len(words)):
55
- if not G.has_edge(words[i], words[j]):
56
- G.add_edge(words[i], words[j])
57
-
58
- return G
59
-
60
  def plot_network_graph(G):
61
  # 한국어 폰트 설정
62
  font_path = "./NanumBarunGothic.ttf"
@@ -64,16 +45,19 @@ def plot_network_graph(G):
64
  plt.rc('font', family=font_prop.get_name())
65
 
66
  plt.figure(figsize=(12, 8))
67
- pos = nx.spring_layout(G, k=0.5, iterations=50)
 
 
68
 
69
  node_colors = [G.nodes[node]['color'] for node in G.nodes()]
70
 
71
- nx.draw(G, pos, node_color=node_colors, with_labels=True, node_size=1000, font_size=8,
72
- font_weight='bold', edge_color='gray', width=0.5, font_family=font_prop.get_name())
73
 
74
- for node, (x, y) in pos.items():
75
- plt.text(x, y, node, fontsize=8, fontweight='bold', ha='center', va='center',
76
- fontproperties=font_prop)
 
77
 
78
  plt.title("Network graph", fontsize=16)
79
  plt.axis('off')
@@ -85,6 +69,25 @@ def plot_network_graph(G):
85
 
86
  return img_bytes
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  # 헤더 스타일 변경
89
  st.markdown("""
90
  <style>
 
38
  HSV_tuples = [(x * 1.0 / n, 0.5, 0.9) for x in range(n)]
39
  return ['#%02x%02x%02x' % tuple(int(x*255) for x in colorsys.hsv_to_rgb(*hsv)) for hsv in HSV_tuples]
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def plot_network_graph(G):
42
  # 한국어 폰트 설정
43
  font_path = "./NanumBarunGothic.ttf"
 
45
  plt.rc('font', family=font_prop.get_name())
46
 
47
  plt.figure(figsize=(12, 8))
48
+
49
+ # 고정된 레이아웃 사용
50
+ pos = nx.spring_layout(G, k=0.5, iterations=50, seed=42)
51
 
52
  node_colors = [G.nodes[node]['color'] for node in G.nodes()]
53
 
54
+ nx.draw(G, pos, node_color=node_colors, with_labels=False, node_size=1000,
55
+ edge_color='gray', width=0.5)
56
 
57
+ # 라벨 위치 조정
58
+ label_pos = {k: (v[0], v[1] + 0.02) for k, v in pos.items()}
59
+ nx.draw_networkx_labels(G, label_pos, font_size=8, font_weight='bold',
60
+ font_family=font_prop.get_name())
61
 
62
  plt.title("Network graph", fontsize=16)
63
  plt.axis('off')
 
69
 
70
  return img_bytes
71
 
72
+ def create_network_graph(topic_results, num_words=10):
73
+ G = nx.Graph()
74
+ colors = generate_colors(len(topic_results))
75
+
76
+ for idx, topic in enumerate(topic_results):
77
+ words = topic['lda_words'][:num_words]
78
+ color = colors[idx]
79
+
80
+ for word in words:
81
+ if not G.has_node(word):
82
+ G.add_node(word, color=color)
83
+
84
+ for i in range(len(words)):
85
+ for j in range(i+1, len(words)):
86
+ if not G.has_edge(words[i], words[j]):
87
+ G.add_edge(words[i], words[j])
88
+
89
+ return G
90
+
91
  # 헤더 스타일 변경
92
  st.markdown("""
93
  <style>