# Analyzer Service Quick Reference
One-page reference for dashboard developers.
---
## Import
```python
from ytpipe.services.intelligence.analyzer import AnalyzerService
from ytpipe.core.models import VideoMetadata, Chunk
```
---
## Initialize
```python
analyzer = AnalyzerService()
```
---
## Methods
### 1. Summary (3-5 bullet points)
```python
summary = analyzer.generate_summary(metadata, chunks, max_bullets=5)
# Returns: ["bullet 1", "bullet 2", ...]
```
**Display**:
```html
<ul>
{% for bullet in summary %}
<li>{{ bullet }}</li>
{% endfor %}
</ul>
```
---
### 2. Entities (people, orgs, concepts)
```python
entities = analyzer.extract_entities(chunks, max_entities=10)
# Returns: [
# {"entity": "FastAPI", "type": "concept", "count": 15},
# {"entity": "Dr Smith", "type": "person", "count": 3}
# ]
```
**Display**:
```html
{% for entity in entities %}
<span class="tag tag-{{ entity.type }}">
{{ entity.entity }} ({{ entity.count }})
</span>
{% endfor %}
```
---
### 3. Sentiment (positive/neutral/negative)
```python
sentiment = analyzer.analyze_sentiment(chunks)
# Returns: {
# "sentiment": "positive", # or "neutral" or "negative"
# "score": 0.72, # 0=negative, 1=positive
# "distribution": {
# "positive": 45,
# "negative": 18,
# "neutral": 1234
# }
# }
```
**Display**:
```html
<span class="badge badge-{{ sentiment.sentiment }}">
{{ sentiment.sentiment|title }} ({{ sentiment.score * 100 }}%)
</span>
```
---
### 4. Difficulty (beginner/intermediate/advanced/expert)
```python
difficulty = analyzer.calculate_difficulty(chunks)
# Returns: {
# "level": "intermediate",
# "score": 0.45, # 0=beginner, 1=expert
# "factors": {
# "avg_word_length": 5.2,
# "vocab_complexity": 0.38,
# "avg_sentence_length": 15.4,
# "technical_density": 0.023
# }
# }
```
**Display**:
```html
<div class="difficulty difficulty-{{ difficulty.level }}">
<span class="level">{{ difficulty.level|title }}</span>
<div class="progress-bar">
<div style="width: {{ difficulty.score * 100 }}%"></div>
</div>
</div>
```
---
### 5. Action Items (instructions)
```python
actions = analyzer.extract_action_items(chunks, max_items=5)
# Returns: [
# "Install Python 3.8 or higher",
# "Run pip install fastapi",
# ...
# ]
```
**Display**:
```html
<ul class="checklist">
{% for action in actions %}
<li>{{ action }}</li>
{% endfor %}
</ul>
```
---
## Complete Example
```python
from ytpipe.services.intelligence.analyzer import AnalyzerService
# Initialize
analyzer = AnalyzerService()
# Load data (from your existing code)
metadata = load_metadata()
chunks = load_chunks()
# Generate all insights
insights = {
"summary": analyzer.generate_summary(metadata, chunks),
"entities": analyzer.extract_entities(chunks),
"sentiment": analyzer.analyze_sentiment(chunks),
"difficulty": analyzer.calculate_difficulty(chunks),
"action_items": analyzer.extract_action_items(chunks)
}
# Use in template
return render_template("dashboard.html", **insights)
```
---
## Dashboard Layout Suggestion
```html
<!-- Summary Section -->
<section class="summary">
<h3>π Summary</h3>
<ul>
{% for bullet in summary %}
<li>{{ bullet }}</li>
{% endfor %}
</ul>
</section>
<!-- Metadata Row -->
<div class="metadata-row">
<!-- Sentiment -->
<div class="card">
<h4>π Tone</h4>
<span class="badge badge-{{ sentiment.sentiment }}">
{{ sentiment.sentiment|title }}
</span>
</div>
<!-- Difficulty -->
<div class="card">
<h4>π Level</h4>
<span class="level-badge level-{{ difficulty.level }}">
{{ difficulty.level|title }}
</span>
</div>
</div>
<!-- Entities -->
<section class="entities">
<h3>π·οΈ Key Topics</h3>
<div class="tags">
{% for entity in entities %}
<span class="tag">{{ entity.entity }} ({{ entity.count }})</span>
{% endfor %}
</div>
</section>
<!-- Action Items -->
<section class="actions">
<h3>β Quick Start</h3>
<ul class="checklist">
{% for action in action_items %}
<li>{{ action }}</li>
{% endfor %}
</ul>
</section>
```
---
## CSS Suggestions
```css
/* Sentiment badges */
.badge-positive { background: #28a745; color: white; }
.badge-neutral { background: #6c757d; color: white; }
.badge-negative { background: #dc3545; color: white; }
/* Difficulty levels */
.level-beginner { color: #28a745; }
.level-intermediate { color: #ffc107; }
.level-advanced { color: #fd7e14; }
.level-expert { color: #dc3545; }
/* Entity tags */
.tag {
display: inline-block;
padding: 4px 8px;
margin: 4px;
background: #e9ecef;
border-radius: 4px;
}
.tag-person { background: #cfe2ff; }
.tag-org { background: #f8d7da; }
.tag-concept { background: #d1e7dd; }
/* Action items */
.checklist {
list-style: none;
padding: 0;
}
.checklist li::before {
content: "β ";
margin-right: 8px;
}
```
---
## Performance Tips
1. **Cache results**: Analysis is fast but cache if used multiple times
2. **Lazy load**: Only analyze when user views detail page
3. **Batch process**: If analyzing multiple videos, process in parallel
```python
# Good: Cache for repeated access
@lru_cache(maxsize=100)
def get_insights(video_id):
analyzer = AnalyzerService()
# ... generate insights ...
return insights
# Good: Lazy load on detail view
@app.route('/video/<video_id>')
def video_detail(video_id):
insights = get_insights(video_id) # Only when needed
return render_template('detail.html', **insights)
```
---
## Error Handling
All methods handle empty/invalid input gracefully:
```python
# Empty chunks β sensible defaults
empty_chunks = []
summary = analyzer.generate_summary(metadata, empty_chunks)
# Returns: ["Video: Title", "Duration: 10m 30s"]
entities = analyzer.extract_entities(empty_chunks)
# Returns: []
sentiment = analyzer.analyze_sentiment(empty_chunks)
# Returns: {"sentiment": "neutral", "score": 0.5, ...}
difficulty = analyzer.calculate_difficulty(empty_chunks)
# Returns: {"level": "beginner", "score": 0.0, ...}
actions = analyzer.extract_action_items(empty_chunks)
# Returns: []
```
**No try/except needed** - methods never raise exceptions.
---
## Common Patterns
### Conditional Display
```python
# Only show action items if found
{% if action_items %}
<section class="actions">
<h3>Quick Start</h3>
<ul>{% for action in action_items %}<li>{{ action }}</li>{% endfor %}</ul>
</section>
{% endif %}
```
### Filtering Entities
```python
# Show only high-frequency entities
high_freq_entities = [e for e in entities if e['count'] >= 3]
# Show only specific types
people = [e for e in entities if e['type'] == 'person']
concepts = [e for e in entities if e['type'] == 'concept']
```
### Custom Difficulty Ranges
```python
# Custom thresholds
if difficulty['score'] < 0.25:
badge_class = "very-easy"
elif difficulty['score'] < 0.5:
badge_class = "easy"
elif difficulty['score'] < 0.75:
badge_class = "moderate"
else:
badge_class = "challenging"
```
---
## Testing
Quick test with your data:
```python
# test_my_integration.py
from ytpipe.services.intelligence.analyzer import AnalyzerService
from ytpipe.core.models import VideoMetadata, Chunk
import json
# Load your data
with open('data/VIDEO_ID/metadata.json') as f:
metadata = VideoMetadata(**json.load(f))
with open('data/VIDEO_ID/chunks.jsonl') as f:
chunks = [Chunk(**json.loads(line)) for line in f]
# Test all methods
analyzer = AnalyzerService()
print("Summary:", analyzer.generate_summary(metadata, chunks))
print("Entities:", analyzer.extract_entities(chunks))
print("Sentiment:", analyzer.analyze_sentiment(chunks))
print("Difficulty:", analyzer.calculate_difficulty(chunks))
print("Actions:", analyzer.extract_action_items(chunks))
```
---
## Need More?
- **Full docs**: See `ENHANCED_ANALYZER_DOCS.md`
- **Unit tests**: See `test_analyzer_methods.py`
- **Real examples**: See `test_enhanced_analyzer.py`
---
**Happy coding!** π