Skip to content

Commit 006dd77

Browse files
committed
fix(examples): update text intelligence example to use correct response structure
- Access results from response.results object instead of direct attributes - Properly access sentiments, summary, topics, and intents from nested structure - Add example output for segments and improve formatting
1 parent e562da7 commit 006dd77

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

examples/40-text-intelligence.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,56 @@
2424
)
2525

2626
print("Analysis received:")
27-
print(f"Sentiment: {response.sentiment}")
28-
if response.summary:
29-
print(f"Summary: {response.summary}")
30-
if response.topics:
31-
print(f"Topics: {response.topics}")
32-
if response.intents:
33-
print(f"Intents: {response.intents}")
27+
28+
# Access results from the response.results object
29+
if response.results.sentiments:
30+
if response.results.sentiments.average:
31+
print(f"Average Sentiment: {response.results.sentiments.average.sentiment} (score: {response.results.sentiments.average.sentiment_score})")
32+
if response.results.sentiments.segments:
33+
print(f"Sentiment segments: {len(response.results.sentiments.segments)} found")
34+
35+
if response.results.summary:
36+
if response.results.summary.results and response.results.summary.results.summary:
37+
print(f"Summary: {response.results.summary.results.summary.summary}")
38+
39+
if response.results.topics:
40+
if response.results.topics.results and response.results.topics.results.topics:
41+
segments = response.results.topics.results.topics.segments
42+
if segments:
43+
print(f"Topics found: {len(segments)} segments")
44+
# Access topics from segments
45+
for segment in segments[:3]: # Show first 3 segments
46+
if segment.topics:
47+
topic_names = [topic.topic for topic in segment.topics if hasattr(topic, 'topic')]
48+
if topic_names:
49+
print(f" - {', '.join(topic_names)}")
50+
51+
if response.results.intents:
52+
if response.results.intents.results and response.results.intents.results.intents:
53+
segments = response.results.intents.results.intents.segments
54+
if segments:
55+
print(f"Intents found: {len(segments)} segments")
56+
# Access intents from segments
57+
for segment in segments[:3]: # Show first 3 segments
58+
if segment.intents:
59+
intent_names = [intent.intent for intent in segment.intents if hasattr(intent, 'intent')]
60+
if intent_names:
61+
print(f" - {', '.join(intent_names)}")
3462

3563
# For async version:
3664
# from deepgram import AsyncDeepgramClient
3765
# client = AsyncDeepgramClient()
38-
# response = await client.read.v1.text.analyze(...)
66+
# response = await client.read.v1.text.analyze(
67+
# request={"text": "Hello, world! This is a sample text for analysis."},
68+
# language="en",
69+
# sentiment=True,
70+
# summarize=True,
71+
# topics=True,
72+
# intents=True,
73+
# )
74+
75+
# With access token:
76+
# client = DeepgramClient(access_token="your-access-token")
3977

4078
except Exception as e:
4179
print(f"Error: {e}")

0 commit comments

Comments
 (0)