Update app.py
Browse files
app.py
CHANGED
@@ -770,42 +770,55 @@ 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 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
779 |
|
780 |
# Create graph with explicit weight handling
|
781 |
G = nx.Graph()
|
782 |
edges_with_weights = []
|
783 |
|
784 |
-
#
|
785 |
-
|
786 |
-
|
|
|
|
|
|
|
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))
|
794 |
-
pos = nx.spring_layout(G, k=1) #
|
795 |
|
796 |
-
# Calculate edge widths from
|
797 |
-
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
|
802 |
nx.draw(G, pos,
|
803 |
with_labels=True,
|
804 |
node_color='lightblue',
|
805 |
-
node_size=2000,
|
806 |
-
font_size=14,
|
807 |
width=edge_widths)
|
808 |
|
809 |
plt.title("Character Connection Network")
|
810 |
-
st.pyplot(fig4)
|
811 |
-
|
|
|
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 |
+
|
776 |
+
@st.cache_data
|
777 |
+
def generate_char_network(chars_list):
|
778 |
+
char_bigrams = Counter()
|
779 |
+
for chars in chars_list:
|
780 |
+
for i in range(len(chars)-1):
|
781 |
+
char_bigrams[tuple(chars[i:i+2])] += 1
|
782 |
+
return char_bigrams
|
783 |
+
|
784 |
+
char_bigrams = generate_char_network(chars_list)
|
785 |
|
786 |
# Create graph with explicit weight handling
|
787 |
G = nx.Graph()
|
788 |
edges_with_weights = []
|
789 |
|
790 |
+
# Get the total number of bigrams for percentage calculation
|
791 |
+
total_bigrams = sum(char_bigrams.values())
|
792 |
+
|
793 |
+
# Extract significant bigrams and their weights
|
794 |
+
for (char1, char2), count in char_bigrams.items():
|
795 |
+
if count > total_bigrams * 0.01: # Only include if more than 1% of total
|
796 |
edges_with_weights.append((char1, char2, count))
|
797 |
|
798 |
+
# Sort by weight and take top connections
|
799 |
+
edges_with_weights.sort(key=lambda x: x[2], reverse=True)
|
800 |
+
edges_with_weights = edges_with_weights[:50] # Top 50 connections
|
801 |
+
|
802 |
# Add edges to graph
|
803 |
for char1, char2, weight in edges_with_weights:
|
804 |
G.add_edge(char1, char2, weight=weight)
|
805 |
|
806 |
+
fig4 = plt.figure(figsize=(15, 15))
|
807 |
+
pos = nx.spring_layout(G, k=1, seed=42) # Fixed seed for stable layout
|
808 |
|
809 |
+
# Calculate edge widths directly from weights
|
810 |
+
weights = [G[u][v]['weight'] for u,v in G.edges()]
|
811 |
max_weight = max(weights) if weights else 1
|
812 |
edge_widths = [w/max_weight * 5 for w in weights]
|
813 |
|
814 |
+
# Draw network
|
815 |
nx.draw(G, pos,
|
816 |
with_labels=True,
|
817 |
node_color='lightblue',
|
818 |
+
node_size=2000,
|
819 |
+
font_size=14,
|
820 |
width=edge_widths)
|
821 |
|
822 |
plt.title("Character Connection Network")
|
823 |
+
st.pyplot(fig4, clear_figure=True)
|
824 |
+
|