kambris commited on
Commit
4e56ffe
·
verified ·
1 Parent(s): 0bf32c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -770,29 +770,42 @@ if uploaded_file is not None:
770
  sns.heatmap(pivot, ax=axes[word_pos], cmap='YlOrRd')
771
  axes[word_pos].set_title(f'Word Position {word_pos+1}')
772
  st.pyplot(fig)
773
-
774
  st.subheader("4. Character Connection Patterns")
775
  char_bigrams = Counter()
776
  for chars in chars_list:
777
  for i in range(len(chars)-1):
778
  char_bigrams[tuple(chars[i:i+2])] += 1
779
 
 
780
  G = nx.Graph()
781
- for (char1, char2), count in char_bigrams.most_common(20):
782
- G.add_edge(char1, char2, weight=count)
783
 
784
- fig4 = plt.figure(figsize=(10, 10))
785
- pos = nx.spring_layout(G)
 
 
786
 
787
- # Get edge weights properly
788
- edge_weights = [G[u][v]['weight'] for u,v in G.edges()]
789
- max_weight = max(edge_weights) if edge_weights else 1
790
 
791
- # Draw with correct weight normalization
792
- nx.draw(G, pos, with_labels=True,
 
 
 
 
 
 
 
 
 
793
  node_color='lightblue',
794
- node_size=1000,
795
- font_size=12,
796
- width=[G[u][v]['weight']/max_weight * 5 for u,v in G.edges()])
797
- plt.title("Top Character Connections")
 
798
  st.pyplot(fig4)
 
 
770
  sns.heatmap(pivot, ax=axes[word_pos], cmap='YlOrRd')
771
  axes[word_pos].set_title(f'Word Position {word_pos+1}')
772
  st.pyplot(fig)
773
+
774
  st.subheader("4. Character Connection Patterns")
775
  char_bigrams = Counter()
776
  for chars in chars_list:
777
  for i in range(len(chars)-1):
778
  char_bigrams[tuple(chars[i:i+2])] += 1
779
 
780
+ # Create graph with explicit weight handling
781
  G = nx.Graph()
782
+ edges_with_weights = []
 
783
 
784
+ # Extract top 50 bigrams and their weights
785
+ for (char1, char2), count in char_bigrams.most_common(50): # Increased to 50
786
+ if count > len(chars_list) * 0.01: # Add frequency threshold
787
+ edges_with_weights.append((char1, char2, count))
788
 
789
+ # Add edges to graph
790
+ for char1, char2, weight in edges_with_weights:
791
+ G.add_edge(char1, char2, weight=weight)
792
 
793
+ fig4 = plt.figure(figsize=(15, 15)) # Increased figure size
794
+ pos = nx.spring_layout(G, k=1) # Adjusted layout spacing
795
+
796
+ # Calculate edge widths from explicit weight values
797
+ weights = [w for _, _, w in edges_with_weights]
798
+ max_weight = max(weights) if weights else 1
799
+ edge_widths = [w/max_weight * 5 for w in weights]
800
+
801
+ # Draw network with pre-calculated widths
802
+ nx.draw(G, pos,
803
+ with_labels=True,
804
  node_color='lightblue',
805
+ node_size=2000, # Increased node size
806
+ font_size=14, # Increased font size
807
+ width=edge_widths)
808
+
809
+ plt.title("Character Connection Network")
810
  st.pyplot(fig4)
811
+