Update app.py
Browse files
app.py
CHANGED
|
@@ -279,18 +279,44 @@ def main():
|
|
| 279 |
st.subheader("Emotional Trajectory")
|
| 280 |
emotional_trajectory = analyzer.analyze_emotional_trajectory(text)
|
| 281 |
|
| 282 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
trajectory_fig = go.Figure(data=go.Scatter(
|
| 284 |
-
|
|
|
|
| 285 |
mode='lines+markers',
|
| 286 |
-
name='Emotional Intensity'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
))
|
|
|
|
| 288 |
trajectory_fig.update_layout(
|
| 289 |
-
title='Speech Emotional
|
| 290 |
-
xaxis_title='Speech
|
| 291 |
-
yaxis_title='
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
)
|
|
|
|
| 293 |
st.plotly_chart(trajectory_fig)
|
|
|
|
| 294 |
|
| 295 |
with tab3:
|
| 296 |
st.subheader("Linguistic Complexity")
|
|
@@ -379,17 +405,70 @@ def main():
|
|
| 379 |
with tab5:
|
| 380 |
st.subheader("Advanced NLP Analysis")
|
| 381 |
|
| 382 |
-
# Named Entities
|
| 383 |
-
st.write("###
|
| 384 |
named_entities = analyzer.detect_named_entities(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
entities_df = pd.DataFrame(named_entities)
|
| 386 |
-
|
|
|
|
| 387 |
|
| 388 |
-
#
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
rhetorical_devices = analyzer.detect_rhetorical_devices(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
for device, count in rhetorical_devices.items():
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
if __name__ == "__main__":
|
| 395 |
main()
|
|
|
|
| 279 |
st.subheader("Emotional Trajectory")
|
| 280 |
emotional_trajectory = analyzer.analyze_emotional_trajectory(text)
|
| 281 |
|
| 282 |
+
# Scale values to a -1 to 1 range
|
| 283 |
+
scaled_trajectory = np.array(emotional_trajectory)
|
| 284 |
+
scaled_trajectory = np.clip(scaled_trajectory, -1, 1)
|
| 285 |
+
|
| 286 |
+
# Create segment labels for x-axis
|
| 287 |
+
num_segments = len(scaled_trajectory)
|
| 288 |
+
segment_labels = [f"Segment {i+1}" for i in range(num_segments)]
|
| 289 |
+
|
| 290 |
trajectory_fig = go.Figure(data=go.Scatter(
|
| 291 |
+
x=segment_labels,
|
| 292 |
+
y=scaled_trajectory,
|
| 293 |
mode='lines+markers',
|
| 294 |
+
name='Emotional Intensity',
|
| 295 |
+
line=dict(
|
| 296 |
+
color='#1f77b4',
|
| 297 |
+
width=3
|
| 298 |
+
),
|
| 299 |
+
marker=dict(
|
| 300 |
+
size=8,
|
| 301 |
+
color='#1f77b4'
|
| 302 |
+
)
|
| 303 |
))
|
| 304 |
+
|
| 305 |
trajectory_fig.update_layout(
|
| 306 |
+
title='Speech Emotional Flow',
|
| 307 |
+
xaxis_title='Speech Progression',
|
| 308 |
+
yaxis_title='Sentiment',
|
| 309 |
+
yaxis=dict(
|
| 310 |
+
ticktext=['Very Negative', 'Neutral', 'Very Positive'],
|
| 311 |
+
tickvals=[-1, 0, 1],
|
| 312 |
+
range=[-1, 1]
|
| 313 |
+
),
|
| 314 |
+
hovermode='x unified',
|
| 315 |
+
showlegend=False
|
| 316 |
)
|
| 317 |
+
|
| 318 |
st.plotly_chart(trajectory_fig)
|
| 319 |
+
|
| 320 |
|
| 321 |
with tab3:
|
| 322 |
st.subheader("Linguistic Complexity")
|
|
|
|
| 405 |
with tab5:
|
| 406 |
st.subheader("Advanced NLP Analysis")
|
| 407 |
|
| 408 |
+
# Named Entities with clear explanations
|
| 409 |
+
st.write("### Key People, Organizations, and Places")
|
| 410 |
named_entities = analyzer.detect_named_entities(text)
|
| 411 |
+
|
| 412 |
+
# Create intuitive mapping of entity types
|
| 413 |
+
entity_type_mapping = {
|
| 414 |
+
'PER': 'Person',
|
| 415 |
+
'ORG': 'Organization',
|
| 416 |
+
'LOC': 'Location',
|
| 417 |
+
'GPE': 'Country/City',
|
| 418 |
+
'MISC': 'Miscellaneous'
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
# Transform the entities dataframe
|
| 422 |
entities_df = pd.DataFrame(named_entities)
|
| 423 |
+
entities_df['entity_type'] = entities_df['entity_group'].map(entity_type_mapping)
|
| 424 |
+
entities_df['confidence'] = entities_df['score'].apply(lambda x: f"{x*100:.1f}%")
|
| 425 |
|
| 426 |
+
# Display enhanced table
|
| 427 |
+
display_df = entities_df[['word', 'entity_type', 'confidence']].rename(columns={
|
| 428 |
+
'word': 'Name/Term',
|
| 429 |
+
'entity_type': 'Type',
|
| 430 |
+
'confidence': 'Confidence Level'
|
| 431 |
+
})
|
| 432 |
+
|
| 433 |
+
st.dataframe(
|
| 434 |
+
display_df,
|
| 435 |
+
column_config={
|
| 436 |
+
"Name/Term": st.column_config.TextColumn(
|
| 437 |
+
help="The identified name or term from the text"
|
| 438 |
+
),
|
| 439 |
+
"Type": st.column_config.TextColumn(
|
| 440 |
+
help="Category of the identified term"
|
| 441 |
+
),
|
| 442 |
+
"Confidence Level": st.column_config.TextColumn(
|
| 443 |
+
help="How certain the AI is about this identification"
|
| 444 |
+
)
|
| 445 |
+
},
|
| 446 |
+
hide_index=True
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
# Enhanced Rhetorical Devices section
|
| 450 |
+
st.write("### Persuasive Language Techniques")
|
| 451 |
rhetorical_devices = analyzer.detect_rhetorical_devices(text)
|
| 452 |
+
|
| 453 |
+
# Create columns for better layout
|
| 454 |
+
col1, col2 = st.columns(2)
|
| 455 |
+
|
| 456 |
+
# Define friendly names and descriptions
|
| 457 |
+
device_explanations = {
|
| 458 |
+
'analogy': 'Comparisons (using "like" or "as")',
|
| 459 |
+
'repetition': 'Repeated phrases for emphasis',
|
| 460 |
+
'metaphor': 'Symbolic comparisons',
|
| 461 |
+
'hyperbole': 'Dramatic exaggerations',
|
| 462 |
+
'rhetorical_question': 'Questions asked for effect'
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
for device, count in rhetorical_devices.items():
|
| 466 |
+
with col1:
|
| 467 |
+
st.metric(
|
| 468 |
+
label=device_explanations[device],
|
| 469 |
+
value=f"{count} times"
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
|
| 473 |
if __name__ == "__main__":
|
| 474 |
main()
|