Base code
This commit is contained in:
106
backend/services/persona/README.md
Normal file
106
backend/services/persona/README.md
Normal file
@@ -0,0 +1,106 @@
|
||||
# Persona Services Package
|
||||
|
||||
This package contains platform-specific persona generation and analysis services, providing a modular and extensible architecture for creating platform-optimized writing personas.
|
||||
|
||||
## Structure
|
||||
|
||||
```
|
||||
services/persona/
|
||||
├── __init__.py # Package initialization
|
||||
├── linkedin/ # LinkedIn-specific persona services
|
||||
│ ├── __init__.py # LinkedIn package initialization
|
||||
│ ├── linkedin_persona_service.py # Main LinkedIn persona service
|
||||
│ ├── linkedin_persona_prompts.py # LinkedIn-specific prompts
|
||||
│ └── linkedin_persona_schemas.py # LinkedIn-specific schemas
|
||||
└── README.md # This documentation
|
||||
```
|
||||
|
||||
## LinkedIn Persona Services
|
||||
|
||||
### LinkedInPersonaService
|
||||
The main service class for generating LinkedIn-specific persona adaptations.
|
||||
|
||||
**Key Features:**
|
||||
- Enhanced LinkedIn-specific prompt generation
|
||||
- Professional networking optimization
|
||||
- Industry-specific adaptations
|
||||
- Algorithm optimization for LinkedIn
|
||||
- Persona validation and quality scoring
|
||||
|
||||
**Methods:**
|
||||
- `generate_linkedin_persona()` - Generate LinkedIn-optimized persona
|
||||
- `validate_linkedin_persona()` - Validate persona data quality
|
||||
- `optimize_for_linkedin_algorithm()` - Algorithm-specific optimizations
|
||||
- `get_linkedin_constraints()` - Get LinkedIn platform constraints
|
||||
|
||||
### LinkedInPersonaPrompts
|
||||
Handles LinkedIn-specific prompt generation with professional optimization.
|
||||
|
||||
**Key Features:**
|
||||
- Industry-specific targeting (technology, business, etc.)
|
||||
- Professional networking focus
|
||||
- Thought leadership positioning
|
||||
- B2B optimization
|
||||
- LinkedIn algorithm awareness
|
||||
|
||||
### LinkedInPersonaSchemas
|
||||
Defines LinkedIn-specific JSON schemas for persona generation.
|
||||
|
||||
**Key Features:**
|
||||
- Enhanced LinkedIn schema with professional fields
|
||||
- Algorithm optimization fields
|
||||
- Professional networking elements
|
||||
- LinkedIn feature-specific adaptations
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
from services.persona.linkedin.linkedin_persona_service import LinkedInPersonaService
|
||||
|
||||
# Initialize the service
|
||||
linkedin_service = LinkedInPersonaService()
|
||||
|
||||
# Generate LinkedIn persona
|
||||
linkedin_persona = linkedin_service.generate_linkedin_persona(
|
||||
core_persona=core_persona_data,
|
||||
onboarding_data=onboarding_data
|
||||
)
|
||||
|
||||
# Validate persona quality
|
||||
validation_results = linkedin_service.validate_linkedin_persona(linkedin_persona)
|
||||
|
||||
# Optimize for LinkedIn algorithm
|
||||
optimized_persona = linkedin_service.optimize_for_linkedin_algorithm(linkedin_persona)
|
||||
```
|
||||
|
||||
## Integration with Main Persona Service
|
||||
|
||||
The main `PersonaAnalysisService` automatically uses the LinkedIn service when generating LinkedIn personas:
|
||||
|
||||
```python
|
||||
# In PersonaAnalysisService._generate_single_platform_persona()
|
||||
if platform.lower() == "linkedin":
|
||||
return self.linkedin_service.generate_linkedin_persona(core_persona, onboarding_data)
|
||||
```
|
||||
|
||||
## Benefits of This Architecture
|
||||
|
||||
1. **Modularity**: Each platform has its own dedicated service
|
||||
2. **Extensibility**: Easy to add new platforms (Facebook, Instagram, etc.)
|
||||
3. **Maintainability**: Platform-specific logic is isolated
|
||||
4. **Testability**: Each service can be tested independently
|
||||
5. **Reusability**: Services can be used across different parts of the application
|
||||
|
||||
## Future Extensions
|
||||
|
||||
This architecture makes it easy to add new platform-specific services:
|
||||
|
||||
- `services/persona/facebook/` - Facebook-specific persona services
|
||||
- `services/persona/instagram/` - Instagram-specific persona services
|
||||
- `services/persona/twitter/` - Twitter-specific persona services
|
||||
- `services/persona/blog/` - Blog-specific persona services
|
||||
|
||||
Each platform service would follow the same pattern:
|
||||
- `{platform}_persona_service.py` - Main service class
|
||||
- `{platform}_persona_prompts.py` - Platform-specific prompts
|
||||
- `{platform}_persona_schemas.py` - Platform-specific schemas
|
||||
1052
backend/services/persona/TBD_persona_enhancements.md
Normal file
1052
backend/services/persona/TBD_persona_enhancements.md
Normal file
File diff suppressed because it is too large
Load Diff
8
backend/services/persona/__init__.py
Normal file
8
backend/services/persona/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""
|
||||
Persona Services Package
|
||||
Contains platform-specific persona generation and analysis services.
|
||||
"""
|
||||
|
||||
from .linkedin.linkedin_persona_service import LinkedInPersonaService
|
||||
|
||||
__all__ = ['LinkedInPersonaService']
|
||||
16
backend/services/persona/core_persona/__init__.py
Normal file
16
backend/services/persona/core_persona/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
Core Persona Generation Module
|
||||
|
||||
This module contains the core persona generation logic extracted from persona_analysis_service.py
|
||||
to improve maintainability and modularity.
|
||||
"""
|
||||
|
||||
from .core_persona_service import CorePersonaService
|
||||
from .data_collector import OnboardingDataCollector
|
||||
from .prompt_builder import PersonaPromptBuilder
|
||||
|
||||
__all__ = [
|
||||
'CorePersonaService',
|
||||
'OnboardingDataCollector',
|
||||
'PersonaPromptBuilder'
|
||||
]
|
||||
176
backend/services/persona/core_persona/core_persona_service.py
Normal file
176
backend/services/persona/core_persona/core_persona_service.py
Normal file
@@ -0,0 +1,176 @@
|
||||
"""
|
||||
Core Persona Service
|
||||
|
||||
Handles the core persona generation logic using Gemini AI.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, List
|
||||
from loguru import logger
|
||||
from datetime import datetime
|
||||
|
||||
from services.llm_providers.gemini_provider import gemini_structured_json_response
|
||||
from .data_collector import OnboardingDataCollector
|
||||
from .prompt_builder import PersonaPromptBuilder
|
||||
from services.persona.linkedin.linkedin_persona_service import LinkedInPersonaService
|
||||
from services.persona.facebook.facebook_persona_service import FacebookPersonaService
|
||||
|
||||
|
||||
class CorePersonaService:
|
||||
"""Core service for generating writing personas using Gemini AI."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(CorePersonaService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the core persona service (only once)."""
|
||||
if not self._initialized:
|
||||
self.data_collector = OnboardingDataCollector()
|
||||
self.prompt_builder = PersonaPromptBuilder()
|
||||
self.linkedin_service = LinkedInPersonaService()
|
||||
self.facebook_service = FacebookPersonaService()
|
||||
logger.debug("CorePersonaService initialized")
|
||||
self._initialized = True
|
||||
|
||||
def generate_core_persona(self, onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Generate core writing persona using Gemini structured response."""
|
||||
|
||||
# Build analysis prompt
|
||||
prompt = self.prompt_builder.build_persona_analysis_prompt(onboarding_data)
|
||||
|
||||
# Get schema for structured response
|
||||
persona_schema = self.prompt_builder.get_persona_schema()
|
||||
|
||||
try:
|
||||
# Generate structured response using Gemini
|
||||
response = gemini_structured_json_response(
|
||||
prompt=prompt,
|
||||
schema=persona_schema,
|
||||
temperature=0.2, # Low temperature for consistent analysis
|
||||
max_tokens=8192,
|
||||
system_prompt="You are an expert writing style analyst and persona developer. Analyze the provided data to create a precise, actionable writing persona."
|
||||
)
|
||||
|
||||
if "error" in response:
|
||||
logger.error(f"Gemini API error: {response['error']}")
|
||||
return {"error": f"AI analysis failed: {response['error']}"}
|
||||
|
||||
logger.info("✅ Core persona generated successfully")
|
||||
return response
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating core persona: {str(e)}")
|
||||
return {"error": f"Failed to generate core persona: {str(e)}"}
|
||||
|
||||
def generate_platform_adaptations(self, core_persona: Dict[str, Any], onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Generate platform-specific persona adaptations."""
|
||||
|
||||
platforms = ["twitter", "linkedin", "instagram", "facebook", "blog", "medium", "substack"]
|
||||
platform_personas = {}
|
||||
|
||||
for platform in platforms:
|
||||
try:
|
||||
platform_persona = self._generate_single_platform_persona(core_persona, platform, onboarding_data)
|
||||
if "error" not in platform_persona:
|
||||
platform_personas[platform] = platform_persona
|
||||
else:
|
||||
logger.warning(f"Failed to generate {platform} persona: {platform_persona['error']}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating {platform} persona: {str(e)}")
|
||||
|
||||
return platform_personas
|
||||
|
||||
def _generate_single_platform_persona(self, core_persona: Dict[str, Any], platform: str, onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Generate persona adaptation for a specific platform."""
|
||||
|
||||
# Use LinkedIn service for LinkedIn platform
|
||||
if platform.lower() == "linkedin":
|
||||
return self.linkedin_service.generate_linkedin_persona(core_persona, onboarding_data)
|
||||
|
||||
# Use Facebook service for Facebook platform
|
||||
if platform.lower() == "facebook":
|
||||
return self.facebook_service.generate_facebook_persona(core_persona, onboarding_data)
|
||||
|
||||
# Use generic platform adaptation for other platforms
|
||||
platform_constraints = self._get_platform_constraints(platform)
|
||||
prompt = self.prompt_builder.build_platform_adaptation_prompt(core_persona, platform, onboarding_data, platform_constraints)
|
||||
|
||||
# Get platform-specific schema
|
||||
platform_schema = self.prompt_builder.get_platform_schema()
|
||||
|
||||
try:
|
||||
response = gemini_structured_json_response(
|
||||
prompt=prompt,
|
||||
schema=platform_schema,
|
||||
temperature=0.2,
|
||||
max_tokens=4096,
|
||||
system_prompt=f"You are an expert in {platform} content strategy and platform-specific writing optimization."
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating {platform} persona: {str(e)}")
|
||||
return {"error": f"Failed to generate {platform} persona: {str(e)}"}
|
||||
|
||||
def _get_platform_constraints(self, platform: str) -> Dict[str, Any]:
|
||||
"""Get platform-specific constraints and best practices."""
|
||||
|
||||
constraints = {
|
||||
"twitter": {
|
||||
"character_limit": 280,
|
||||
"optimal_length": "120-150 characters",
|
||||
"hashtag_limit": 3,
|
||||
"image_support": True,
|
||||
"thread_support": True,
|
||||
"link_shortening": True
|
||||
},
|
||||
"linkedin": self.linkedin_service.get_linkedin_constraints(),
|
||||
"instagram": {
|
||||
"caption_limit": 2200,
|
||||
"optimal_length": "125-150 words",
|
||||
"hashtag_limit": 30,
|
||||
"visual_first": True,
|
||||
"story_support": True,
|
||||
"emoji_friendly": True
|
||||
},
|
||||
"facebook": {
|
||||
"character_limit": 63206,
|
||||
"optimal_length": "40-80 words",
|
||||
"algorithm_favors": "engagement",
|
||||
"link_preview": True,
|
||||
"event_support": True,
|
||||
"group_sharing": True
|
||||
},
|
||||
"blog": {
|
||||
"word_count": "800-2000 words",
|
||||
"seo_important": True,
|
||||
"header_structure": True,
|
||||
"internal_linking": True,
|
||||
"meta_descriptions": True,
|
||||
"readability_score": True
|
||||
},
|
||||
"medium": {
|
||||
"word_count": "1000-3000 words",
|
||||
"storytelling_focus": True,
|
||||
"subtitle_support": True,
|
||||
"publication_support": True,
|
||||
"clap_optimization": True,
|
||||
"follower_building": True
|
||||
},
|
||||
"substack": {
|
||||
"newsletter_format": True,
|
||||
"email_optimization": True,
|
||||
"subscription_focus": True,
|
||||
"long_form": True,
|
||||
"personal_connection": True,
|
||||
"monetization_support": True
|
||||
}
|
||||
}
|
||||
|
||||
return constraints.get(platform, {})
|
||||
306
backend/services/persona/core_persona/data_collector.py
Normal file
306
backend/services/persona/core_persona/data_collector.py
Normal file
@@ -0,0 +1,306 @@
|
||||
"""
|
||||
Onboarding Data Collector
|
||||
|
||||
Handles comprehensive collection of onboarding data for persona generation.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, List, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from loguru import logger
|
||||
|
||||
from services.database import get_db_session
|
||||
from models.onboarding import OnboardingSession, WebsiteAnalysis, ResearchPreferences, APIKey
|
||||
|
||||
|
||||
class OnboardingDataCollector:
|
||||
"""Collects comprehensive onboarding data for persona analysis."""
|
||||
|
||||
def collect_onboarding_data(self, user_id: int, session_id: int = None) -> Optional[Dict[str, Any]]:
|
||||
"""Collect comprehensive onboarding data for persona analysis."""
|
||||
try:
|
||||
session = get_db_session()
|
||||
|
||||
# Find onboarding session
|
||||
if session_id:
|
||||
onboarding_session = session.query(OnboardingSession).filter(
|
||||
OnboardingSession.id == session_id,
|
||||
OnboardingSession.user_id == user_id
|
||||
).first()
|
||||
else:
|
||||
onboarding_session = session.query(OnboardingSession).filter(
|
||||
OnboardingSession.user_id == user_id
|
||||
).order_by(OnboardingSession.updated_at.desc()).first()
|
||||
|
||||
if not onboarding_session:
|
||||
return None
|
||||
|
||||
# Get ALL website analyses (there might be multiple)
|
||||
website_analyses = session.query(WebsiteAnalysis).filter(
|
||||
WebsiteAnalysis.session_id == onboarding_session.id
|
||||
).order_by(WebsiteAnalysis.updated_at.desc()).all()
|
||||
|
||||
# Get research preferences
|
||||
research_prefs = session.query(ResearchPreferences).filter(
|
||||
ResearchPreferences.session_id == onboarding_session.id
|
||||
).first()
|
||||
|
||||
# Get API keys
|
||||
api_keys = session.query(APIKey).filter(
|
||||
APIKey.session_id == onboarding_session.id
|
||||
).all()
|
||||
|
||||
# Compile comprehensive data with ALL available information
|
||||
onboarding_data = {
|
||||
"session_info": {
|
||||
"session_id": onboarding_session.id,
|
||||
"user_id": onboarding_session.user_id,
|
||||
"current_step": onboarding_session.current_step,
|
||||
"progress": onboarding_session.progress,
|
||||
"started_at": onboarding_session.started_at.isoformat() if onboarding_session.started_at else None,
|
||||
"updated_at": onboarding_session.updated_at.isoformat() if onboarding_session.updated_at else None
|
||||
},
|
||||
"api_keys": [key.to_dict() for key in api_keys] if api_keys else [],
|
||||
"website_analyses": [analysis.to_dict() for analysis in website_analyses] if website_analyses else [],
|
||||
"research_preferences": research_prefs.to_dict() if research_prefs else None,
|
||||
|
||||
# Legacy compatibility - use the latest website analysis
|
||||
"website_analysis": website_analyses[0].to_dict() if website_analyses else None,
|
||||
|
||||
# Enhanced data extraction for persona generation
|
||||
"enhanced_analysis": self._extract_enhanced_analysis_data(website_analyses, research_prefs)
|
||||
}
|
||||
|
||||
session.close()
|
||||
return onboarding_data
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error collecting onboarding data: {str(e)}")
|
||||
return None
|
||||
|
||||
def _extract_enhanced_analysis_data(self, website_analyses: List, research_prefs) -> Dict[str, Any]:
|
||||
"""Extract and structure all the rich AI analysis data for persona generation."""
|
||||
enhanced_data = {
|
||||
"comprehensive_style_analysis": {},
|
||||
"content_insights": {},
|
||||
"audience_intelligence": {},
|
||||
"brand_voice_analysis": {},
|
||||
"technical_writing_metrics": {},
|
||||
"competitive_analysis": {},
|
||||
"content_strategy_insights": {}
|
||||
}
|
||||
|
||||
if not website_analyses:
|
||||
return enhanced_data
|
||||
|
||||
# Use the latest (most comprehensive) website analysis
|
||||
latest_analysis = website_analyses[0]
|
||||
|
||||
# Extract comprehensive style analysis
|
||||
if latest_analysis.writing_style:
|
||||
enhanced_data["comprehensive_style_analysis"] = {
|
||||
"tone_analysis": latest_analysis.writing_style.get("tone", ""),
|
||||
"voice_characteristics": latest_analysis.writing_style.get("voice", ""),
|
||||
"complexity_assessment": latest_analysis.writing_style.get("complexity", ""),
|
||||
"engagement_level": latest_analysis.writing_style.get("engagement_level", ""),
|
||||
"brand_personality": latest_analysis.writing_style.get("brand_personality", ""),
|
||||
"formality_level": latest_analysis.writing_style.get("formality_level", ""),
|
||||
"emotional_appeal": latest_analysis.writing_style.get("emotional_appeal", "")
|
||||
}
|
||||
|
||||
# Extract content insights
|
||||
if latest_analysis.content_characteristics:
|
||||
enhanced_data["content_insights"] = {
|
||||
"sentence_structure_analysis": latest_analysis.content_characteristics.get("sentence_structure", ""),
|
||||
"vocabulary_level": latest_analysis.content_characteristics.get("vocabulary_level", ""),
|
||||
"paragraph_organization": latest_analysis.content_characteristics.get("paragraph_organization", ""),
|
||||
"content_flow": latest_analysis.content_characteristics.get("content_flow", ""),
|
||||
"readability_score": latest_analysis.content_characteristics.get("readability_score", ""),
|
||||
"content_density": latest_analysis.content_characteristics.get("content_density", ""),
|
||||
"visual_elements_usage": latest_analysis.content_characteristics.get("visual_elements_usage", "")
|
||||
}
|
||||
|
||||
# Extract audience intelligence
|
||||
if latest_analysis.target_audience:
|
||||
enhanced_data["audience_intelligence"] = {
|
||||
"demographics": latest_analysis.target_audience.get("demographics", []),
|
||||
"expertise_level": latest_analysis.target_audience.get("expertise_level", ""),
|
||||
"industry_focus": latest_analysis.target_audience.get("industry_focus", ""),
|
||||
"geographic_focus": latest_analysis.target_audience.get("geographic_focus", ""),
|
||||
"psychographic_profile": latest_analysis.target_audience.get("psychographic_profile", ""),
|
||||
"pain_points": latest_analysis.target_audience.get("pain_points", []),
|
||||
"motivations": latest_analysis.target_audience.get("motivations", [])
|
||||
}
|
||||
|
||||
# Extract brand voice analysis
|
||||
if latest_analysis.content_type:
|
||||
enhanced_data["brand_voice_analysis"] = {
|
||||
"primary_content_type": latest_analysis.content_type.get("primary_type", ""),
|
||||
"secondary_content_types": latest_analysis.content_type.get("secondary_types", []),
|
||||
"content_purpose": latest_analysis.content_type.get("purpose", ""),
|
||||
"call_to_action_style": latest_analysis.content_type.get("call_to_action", ""),
|
||||
"conversion_focus": latest_analysis.content_type.get("conversion_focus", ""),
|
||||
"educational_value": latest_analysis.content_type.get("educational_value", "")
|
||||
}
|
||||
|
||||
# Extract technical writing metrics
|
||||
if latest_analysis.style_patterns:
|
||||
enhanced_data["technical_writing_metrics"] = {
|
||||
"sentence_length_preference": latest_analysis.style_patterns.get("patterns", {}).get("sentence_length", ""),
|
||||
"vocabulary_patterns": latest_analysis.style_patterns.get("patterns", {}).get("vocabulary_patterns", []),
|
||||
"rhetorical_devices": latest_analysis.style_patterns.get("patterns", {}).get("rhetorical_devices", []),
|
||||
"paragraph_structure": latest_analysis.style_patterns.get("patterns", {}).get("paragraph_structure", ""),
|
||||
"transition_phrases": latest_analysis.style_patterns.get("patterns", {}).get("transition_phrases", []),
|
||||
"style_consistency": latest_analysis.style_patterns.get("style_consistency", ""),
|
||||
"unique_elements": latest_analysis.style_patterns.get("unique_elements", [])
|
||||
}
|
||||
|
||||
# Extract competitive analysis from crawl results
|
||||
if latest_analysis.crawl_result:
|
||||
crawl_data = latest_analysis.crawl_result
|
||||
enhanced_data["competitive_analysis"] = {
|
||||
"domain_info": crawl_data.get("domain_info", {}),
|
||||
"social_media_presence": crawl_data.get("social_media", {}),
|
||||
"brand_info": crawl_data.get("brand_info", {}),
|
||||
"content_structure": crawl_data.get("content_structure", {}),
|
||||
"meta_optimization": crawl_data.get("meta_tags", {})
|
||||
}
|
||||
|
||||
# Extract content strategy insights from style guidelines
|
||||
if latest_analysis.style_guidelines:
|
||||
guidelines = latest_analysis.style_guidelines
|
||||
enhanced_data["content_strategy_insights"] = {
|
||||
"tone_recommendations": guidelines.get("guidelines", {}).get("tone_recommendations", []),
|
||||
"structure_guidelines": guidelines.get("guidelines", {}).get("structure_guidelines", []),
|
||||
"vocabulary_suggestions": guidelines.get("guidelines", {}).get("vocabulary_suggestions", []),
|
||||
"engagement_tips": guidelines.get("guidelines", {}).get("engagement_tips", []),
|
||||
"audience_considerations": guidelines.get("guidelines", {}).get("audience_considerations", []),
|
||||
"brand_alignment": guidelines.get("guidelines", {}).get("brand_alignment", []),
|
||||
"seo_optimization": guidelines.get("guidelines", {}).get("seo_optimization", []),
|
||||
"conversion_optimization": guidelines.get("guidelines", {}).get("conversion_optimization", []),
|
||||
"best_practices": guidelines.get("best_practices", []),
|
||||
"avoid_elements": guidelines.get("avoid_elements", []),
|
||||
"content_strategy": guidelines.get("content_strategy", ""),
|
||||
"ai_generation_tips": guidelines.get("ai_generation_tips", []),
|
||||
"competitive_advantages": guidelines.get("competitive_advantages", []),
|
||||
"content_calendar_suggestions": guidelines.get("content_calendar_suggestions", [])
|
||||
}
|
||||
|
||||
# Add research preferences insights
|
||||
if research_prefs:
|
||||
enhanced_data["research_preferences"] = {
|
||||
"research_depth": research_prefs.research_depth,
|
||||
"content_types": research_prefs.content_types,
|
||||
"auto_research": research_prefs.auto_research,
|
||||
"factual_content": research_prefs.factual_content
|
||||
}
|
||||
|
||||
return enhanced_data
|
||||
|
||||
def calculate_data_sufficiency(self, onboarding_data: Dict[str, Any]) -> float:
|
||||
"""Calculate how sufficient the onboarding data is for persona generation."""
|
||||
score = 0.0
|
||||
|
||||
# Get enhanced analysis data
|
||||
enhanced_analysis = onboarding_data.get("enhanced_analysis", {})
|
||||
website_analysis = onboarding_data.get("website_analysis", {}) or {}
|
||||
research_prefs = onboarding_data.get("research_preferences", {}) or {}
|
||||
|
||||
# Enhanced scoring based on comprehensive data availability
|
||||
|
||||
# Comprehensive Style Analysis (25% of score)
|
||||
style_analysis = enhanced_analysis.get("comprehensive_style_analysis", {})
|
||||
if style_analysis.get("tone_analysis"):
|
||||
score += 5
|
||||
if style_analysis.get("voice_characteristics"):
|
||||
score += 5
|
||||
if style_analysis.get("brand_personality"):
|
||||
score += 5
|
||||
if style_analysis.get("formality_level"):
|
||||
score += 5
|
||||
if style_analysis.get("emotional_appeal"):
|
||||
score += 5
|
||||
|
||||
# Content Insights (20% of score)
|
||||
content_insights = enhanced_analysis.get("content_insights", {})
|
||||
if content_insights.get("sentence_structure_analysis"):
|
||||
score += 4
|
||||
if content_insights.get("vocabulary_level"):
|
||||
score += 4
|
||||
if content_insights.get("readability_score"):
|
||||
score += 4
|
||||
if content_insights.get("content_flow"):
|
||||
score += 4
|
||||
if content_insights.get("visual_elements_usage"):
|
||||
score += 4
|
||||
|
||||
# Audience Intelligence (15% of score)
|
||||
audience_intel = enhanced_analysis.get("audience_intelligence", {})
|
||||
if audience_intel.get("demographics"):
|
||||
score += 3
|
||||
if audience_intel.get("expertise_level"):
|
||||
score += 3
|
||||
if audience_intel.get("industry_focus"):
|
||||
score += 3
|
||||
if audience_intel.get("psychographic_profile"):
|
||||
score += 3
|
||||
if audience_intel.get("pain_points"):
|
||||
score += 3
|
||||
|
||||
# Technical Writing Metrics (15% of score)
|
||||
tech_metrics = enhanced_analysis.get("technical_writing_metrics", {})
|
||||
if tech_metrics.get("vocabulary_patterns"):
|
||||
score += 3
|
||||
if tech_metrics.get("rhetorical_devices"):
|
||||
score += 3
|
||||
if tech_metrics.get("paragraph_structure"):
|
||||
score += 3
|
||||
if tech_metrics.get("style_consistency"):
|
||||
score += 3
|
||||
if tech_metrics.get("unique_elements"):
|
||||
score += 3
|
||||
|
||||
# Content Strategy Insights (15% of score)
|
||||
strategy_insights = enhanced_analysis.get("content_strategy_insights", {})
|
||||
if strategy_insights.get("tone_recommendations"):
|
||||
score += 3
|
||||
if strategy_insights.get("best_practices"):
|
||||
score += 3
|
||||
if strategy_insights.get("competitive_advantages"):
|
||||
score += 3
|
||||
if strategy_insights.get("content_strategy"):
|
||||
score += 3
|
||||
if strategy_insights.get("ai_generation_tips"):
|
||||
score += 3
|
||||
|
||||
# Research Preferences (10% of score)
|
||||
if research_prefs.get("research_depth"):
|
||||
score += 5
|
||||
if research_prefs.get("content_types"):
|
||||
score += 5
|
||||
|
||||
# Legacy compatibility - add points for basic data if enhanced data is missing
|
||||
if score < 50: # If enhanced data is insufficient, fall back to legacy scoring
|
||||
legacy_score = 0.0
|
||||
|
||||
# Website analysis components (70% of legacy score)
|
||||
if website_analysis.get("writing_style"):
|
||||
legacy_score += 25
|
||||
if website_analysis.get("content_characteristics"):
|
||||
legacy_score += 20
|
||||
if website_analysis.get("target_audience"):
|
||||
legacy_score += 15
|
||||
if website_analysis.get("style_patterns"):
|
||||
legacy_score += 10
|
||||
|
||||
# Research preferences components (30% of legacy score)
|
||||
if research_prefs.get("research_depth"):
|
||||
legacy_score += 10
|
||||
if research_prefs.get("content_types"):
|
||||
legacy_score += 10
|
||||
if research_prefs.get("writing_style"):
|
||||
legacy_score += 10
|
||||
|
||||
# Use the higher of enhanced or legacy score
|
||||
score = max(score, legacy_score)
|
||||
|
||||
return min(score, 100.0)
|
||||
333
backend/services/persona/core_persona/prompt_builder.py
Normal file
333
backend/services/persona/core_persona/prompt_builder.py
Normal file
@@ -0,0 +1,333 @@
|
||||
"""
|
||||
Persona Prompt Builder
|
||||
|
||||
Handles building comprehensive prompts for persona generation.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any
|
||||
import json
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class PersonaPromptBuilder:
|
||||
"""Builds comprehensive prompts for persona generation."""
|
||||
|
||||
def build_persona_analysis_prompt(self, onboarding_data: Dict[str, Any]) -> str:
|
||||
"""Build the main persona analysis prompt with comprehensive data."""
|
||||
|
||||
# Handle both frontend-style data and backend database-style data
|
||||
# Frontend sends: {websiteAnalysis, competitorResearch, sitemapAnalysis, businessData}
|
||||
# Backend sends: {enhanced_analysis, website_analysis, research_preferences}
|
||||
|
||||
# Normalize data structure
|
||||
if "websiteAnalysis" in onboarding_data:
|
||||
# Frontend-style data - adapt to expected structure
|
||||
website_analysis = onboarding_data.get("websiteAnalysis", {}) or {}
|
||||
competitor_research = onboarding_data.get("competitorResearch", {}) or {}
|
||||
sitemap_analysis = onboarding_data.get("sitemapAnalysis", {}) or {}
|
||||
business_data = onboarding_data.get("businessData", {}) or {}
|
||||
|
||||
# Create enhanced_analysis from frontend data
|
||||
enhanced_analysis = {
|
||||
"comprehensive_style_analysis": website_analysis.get("writing_style", {}),
|
||||
"content_insights": website_analysis.get("content_characteristics", {}),
|
||||
"audience_intelligence": website_analysis.get("target_audience", {}),
|
||||
"technical_writing_metrics": website_analysis.get("style_patterns", {}),
|
||||
"competitive_analysis": competitor_research,
|
||||
"sitemap_data": sitemap_analysis,
|
||||
"business_context": business_data
|
||||
}
|
||||
research_prefs = {}
|
||||
else:
|
||||
# Backend database-style data
|
||||
enhanced_analysis = onboarding_data.get("enhanced_analysis", {})
|
||||
website_analysis = onboarding_data.get("website_analysis", {}) or {}
|
||||
research_prefs = onboarding_data.get("research_preferences", {}) or {}
|
||||
|
||||
prompt = f"""
|
||||
COMPREHENSIVE PERSONA GENERATION TASK: Create a highly detailed, data-driven writing persona based on extensive AI analysis of user's website and content strategy.
|
||||
|
||||
=== COMPREHENSIVE ONBOARDING DATA ANALYSIS ===
|
||||
|
||||
WEBSITE ANALYSIS OVERVIEW:
|
||||
- URL: {website_analysis.get('website_url', 'Not provided')}
|
||||
- Analysis Date: {website_analysis.get('analysis_date', 'Not provided')}
|
||||
- Status: {website_analysis.get('status', 'Not provided')}
|
||||
|
||||
=== DETAILED STYLE ANALYSIS ===
|
||||
{json.dumps(enhanced_analysis.get('comprehensive_style_analysis', {}), indent=2)}
|
||||
|
||||
=== CONTENT INSIGHTS ===
|
||||
{json.dumps(enhanced_analysis.get('content_insights', {}), indent=2)}
|
||||
|
||||
=== AUDIENCE INTELLIGENCE ===
|
||||
{json.dumps(enhanced_analysis.get('audience_intelligence', {}), indent=2)}
|
||||
|
||||
=== BRAND VOICE ANALYSIS ===
|
||||
{json.dumps(enhanced_analysis.get('brand_voice_analysis', {}), indent=2)}
|
||||
|
||||
=== TECHNICAL WRITING METRICS ===
|
||||
{json.dumps(enhanced_analysis.get('technical_writing_metrics', {}), indent=2)}
|
||||
|
||||
=== COMPETITIVE ANALYSIS ===
|
||||
{json.dumps(enhanced_analysis.get('competitive_analysis', {}), indent=2)}
|
||||
|
||||
=== CONTENT STRATEGY INSIGHTS ===
|
||||
{json.dumps(enhanced_analysis.get('content_strategy_insights', {}), indent=2)}
|
||||
|
||||
=== RESEARCH PREFERENCES ===
|
||||
{json.dumps(enhanced_analysis.get('research_preferences', {}), indent=2)}
|
||||
|
||||
=== LEGACY DATA (for compatibility) ===
|
||||
Website Analysis: {json.dumps(website_analysis.get('writing_style', {}), indent=2)}
|
||||
Content Characteristics: {json.dumps(website_analysis.get('content_characteristics', {}) or {}, indent=2)}
|
||||
Target Audience: {json.dumps(website_analysis.get('target_audience', {}), indent=2)}
|
||||
Style Patterns: {json.dumps(website_analysis.get('style_patterns', {}), indent=2)}
|
||||
|
||||
=== COMPREHENSIVE PERSONA GENERATION REQUIREMENTS ===
|
||||
|
||||
1. IDENTITY CREATION (Based on Brand Analysis):
|
||||
- Create a memorable persona name that captures the essence of the brand personality and writing style
|
||||
- Define a clear archetype that reflects the brand's positioning and audience appeal
|
||||
- Articulate a core belief that drives the writing philosophy and brand values
|
||||
- Write a comprehensive brand voice description incorporating all style elements
|
||||
|
||||
2. LINGUISTIC FINGERPRINT (Quantitative Analysis from Technical Metrics):
|
||||
- Calculate precise average sentence length from sentence structure analysis
|
||||
- Determine preferred sentence types based on paragraph organization patterns
|
||||
- Analyze active vs passive voice ratio from voice characteristics
|
||||
- Extract go-to words and phrases from vocabulary patterns and style analysis
|
||||
- List words and phrases to avoid based on brand alignment guidelines
|
||||
- Determine contraction usage patterns from formality level
|
||||
- Assess vocabulary complexity level from readability scores
|
||||
|
||||
3. RHETORICAL ANALYSIS (From Style Patterns):
|
||||
- Identify metaphor patterns and themes from rhetorical devices
|
||||
- Analyze analogy usage from content strategy insights
|
||||
- Assess rhetorical question frequency from engagement tips
|
||||
- Determine storytelling approach from content flow analysis
|
||||
|
||||
4. TONAL RANGE (From Comprehensive Style Analysis):
|
||||
- Define the default tone from tone analysis and brand personality
|
||||
- List permissible tones based on emotional appeal and audience considerations
|
||||
- Identify forbidden tones from avoid elements and brand alignment
|
||||
- Describe emotional range from psychographic profile and engagement level
|
||||
|
||||
5. STYLISTIC CONSTRAINTS (From Technical Writing Metrics):
|
||||
- Define punctuation preferences from paragraph structure analysis
|
||||
- Set formatting guidelines from content structure insights
|
||||
- Establish paragraph structure preferences from organization patterns
|
||||
- Include transition phrase preferences from style patterns
|
||||
|
||||
6. PLATFORM-SPECIFIC ADAPTATIONS (From Content Strategy):
|
||||
- Incorporate SEO optimization strategies
|
||||
- Include conversion optimization techniques
|
||||
- Apply engagement tips for different platforms
|
||||
- Use competitive advantages for differentiation
|
||||
|
||||
7. CONTENT STRATEGY INTEGRATION:
|
||||
- Incorporate best practices from content strategy insights
|
||||
- Include AI generation tips for consistent output
|
||||
- Apply content calendar suggestions for timing
|
||||
- Use competitive advantages for positioning
|
||||
|
||||
=== ENHANCED ANALYSIS INSTRUCTIONS ===
|
||||
- Base your analysis on ALL the comprehensive data provided above
|
||||
- Use the detailed technical metrics for precise linguistic analysis
|
||||
- Incorporate brand voice analysis for authentic personality
|
||||
- Apply audience intelligence for targeted communication
|
||||
- Include competitive analysis for market positioning
|
||||
- Use content strategy insights for practical application
|
||||
- Ensure the persona reflects the brand's unique elements and competitive advantages
|
||||
|
||||
Generate a comprehensive, data-driven persona profile that accurately captures the writing style and brand voice to replicate consistently across different platforms.
|
||||
"""
|
||||
|
||||
return prompt
|
||||
|
||||
def build_platform_adaptation_prompt(self, core_persona: Dict[str, Any], platform: str, onboarding_data: Dict[str, Any], platform_constraints: Dict[str, Any]) -> str:
|
||||
"""Build prompt for platform-specific persona adaptation."""
|
||||
|
||||
prompt = f"""
|
||||
PLATFORM ADAPTATION TASK: Adapt the core writing persona for {platform.upper()}.
|
||||
|
||||
CORE PERSONA:
|
||||
{json.dumps(core_persona, indent=2)}
|
||||
|
||||
PLATFORM: {platform.upper()}
|
||||
|
||||
PLATFORM CONSTRAINTS:
|
||||
{json.dumps(platform_constraints, indent=2)}
|
||||
|
||||
ADAPTATION REQUIREMENTS:
|
||||
|
||||
1. SENTENCE METRICS:
|
||||
- Adjust sentence length for platform optimal performance
|
||||
- Adapt sentence variety for platform engagement
|
||||
- Consider platform reading patterns
|
||||
|
||||
2. LEXICAL ADAPTATIONS:
|
||||
- Identify platform-specific vocabulary and slang
|
||||
- Define hashtag strategy (if applicable)
|
||||
- Set emoji usage guidelines
|
||||
- Establish mention and tagging strategy
|
||||
|
||||
3. CONTENT FORMAT RULES:
|
||||
- Respect character/word limits
|
||||
- Optimize paragraph structure for platform
|
||||
- Define call-to-action style
|
||||
- Set link placement strategy
|
||||
|
||||
4. ENGAGEMENT PATTERNS:
|
||||
- Determine optimal posting frequency
|
||||
- Identify best posting times for audience
|
||||
- Define engagement tactics
|
||||
- Set community interaction guidelines
|
||||
|
||||
5. PLATFORM BEST PRACTICES:
|
||||
- List platform-specific optimization techniques
|
||||
- Consider algorithm preferences
|
||||
- Include trending format adaptations
|
||||
|
||||
INSTRUCTIONS:
|
||||
- Maintain the core persona identity while optimizing for platform performance
|
||||
- Ensure all adaptations align with the original brand voice
|
||||
- Consider platform-specific audience behavior
|
||||
- Provide actionable, specific guidelines
|
||||
|
||||
Generate a platform-optimized persona adaptation that maintains brand consistency while maximizing platform performance.
|
||||
"""
|
||||
|
||||
return prompt
|
||||
|
||||
def get_persona_schema(self) -> Dict[str, Any]:
|
||||
"""Get the schema for core persona generation."""
|
||||
return {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"identity": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"persona_name": {"type": "string"},
|
||||
"archetype": {"type": "string"},
|
||||
"core_belief": {"type": "string"},
|
||||
"brand_voice_description": {"type": "string"}
|
||||
},
|
||||
"required": ["persona_name", "archetype", "core_belief"]
|
||||
},
|
||||
"linguistic_fingerprint": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sentence_metrics": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"average_sentence_length_words": {"type": "number"},
|
||||
"preferred_sentence_type": {"type": "string"},
|
||||
"active_to_passive_ratio": {"type": "string"},
|
||||
"complexity_level": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"lexical_features": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"go_to_words": {"type": "array", "items": {"type": "string"}},
|
||||
"go_to_phrases": {"type": "array", "items": {"type": "string"}},
|
||||
"avoid_words": {"type": "array", "items": {"type": "string"}},
|
||||
"contractions": {"type": "string"},
|
||||
"filler_words": {"type": "string"},
|
||||
"vocabulary_level": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"rhetorical_devices": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"metaphors": {"type": "string"},
|
||||
"analogies": {"type": "string"},
|
||||
"rhetorical_questions": {"type": "string"},
|
||||
"storytelling_style": {"type": "string"}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tonal_range": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"default_tone": {"type": "string"},
|
||||
"permissible_tones": {"type": "array", "items": {"type": "string"}},
|
||||
"forbidden_tones": {"type": "array", "items": {"type": "string"}},
|
||||
"emotional_range": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"stylistic_constraints": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"punctuation": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"ellipses": {"type": "string"},
|
||||
"em_dash": {"type": "string"},
|
||||
"exclamation_points": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"formatting": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"paragraphs": {"type": "string"},
|
||||
"lists": {"type": "string"},
|
||||
"markdown": {"type": "string"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["identity", "linguistic_fingerprint", "tonal_range"]
|
||||
}
|
||||
|
||||
def get_platform_schema(self) -> Dict[str, Any]:
|
||||
"""Get the schema for platform-specific persona adaptation."""
|
||||
return {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"platform_type": {"type": "string"},
|
||||
"sentence_metrics": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"max_sentence_length": {"type": "number"},
|
||||
"optimal_sentence_length": {"type": "number"},
|
||||
"sentence_variety": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"lexical_adaptations": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"platform_specific_words": {"type": "array", "items": {"type": "string"}},
|
||||
"hashtag_strategy": {"type": "string"},
|
||||
"emoji_usage": {"type": "string"},
|
||||
"mention_strategy": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"content_format_rules": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"character_limit": {"type": "number"},
|
||||
"paragraph_structure": {"type": "string"},
|
||||
"call_to_action_style": {"type": "string"},
|
||||
"link_placement": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"engagement_patterns": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"posting_frequency": {"type": "string"},
|
||||
"optimal_posting_times": {"type": "array", "items": {"type": "string"}},
|
||||
"engagement_tactics": {"type": "array", "items": {"type": "string"}},
|
||||
"community_interaction": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"platform_best_practices": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"required": ["platform_type", "sentence_metrics", "content_format_rules", "engagement_patterns"]
|
||||
}
|
||||
635
backend/services/persona/enhanced_linguistic_analyzer.py
Normal file
635
backend/services/persona/enhanced_linguistic_analyzer.py
Normal file
@@ -0,0 +1,635 @@
|
||||
"""
|
||||
Enhanced Linguistic Analysis Service
|
||||
Advanced analysis for better writing style mimicry and persona quality.
|
||||
"""
|
||||
|
||||
import re
|
||||
import json
|
||||
from typing import Dict, Any, List, Tuple
|
||||
from collections import Counter, defaultdict
|
||||
from loguru import logger
|
||||
import nltk
|
||||
from nltk.tokenize import sent_tokenize, word_tokenize
|
||||
from nltk.corpus import stopwords
|
||||
from nltk.tag import pos_tag
|
||||
from textstat import flesch_reading_ease, flesch_kincaid_grade
|
||||
class EnhancedLinguisticAnalyzer:
|
||||
"""Advanced linguistic analysis for persona creation and improvement."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the linguistic analyzer with required spaCy dependency."""
|
||||
self.nlp = None
|
||||
self.spacy_available = False
|
||||
|
||||
# spaCy is REQUIRED for high-quality persona generation
|
||||
try:
|
||||
import spacy
|
||||
self.nlp = spacy.load("en_core_web_sm")
|
||||
self.spacy_available = True
|
||||
logger.debug("SUCCESS: spaCy model loaded successfully - Enhanced linguistic analysis available")
|
||||
except ImportError as e:
|
||||
logger.error(f"ERROR: spaCy is REQUIRED for persona generation. Install with: pip install spacy && python -m spacy download en_core_web_sm")
|
||||
raise ImportError("spaCy is required for enhanced persona generation. Install with: pip install spacy && python -m spacy download en_core_web_sm") from e
|
||||
except OSError as e:
|
||||
logger.error(f"ERROR: spaCy model 'en_core_web_sm' is REQUIRED. Download with: python -m spacy download en_core_web_sm")
|
||||
raise OSError("spaCy model 'en_core_web_sm' is required. Download with: python -m spacy download en_core_web_sm") from e
|
||||
|
||||
# Download required NLTK data
|
||||
try:
|
||||
nltk.data.find('tokenizers/punkt_tab') # Updated for newer NLTK versions
|
||||
nltk.data.find('corpora/stopwords')
|
||||
nltk.data.find('taggers/averaged_perceptron_tagger')
|
||||
except LookupError:
|
||||
logger.warning("NLTK data not found. Downloading required data...")
|
||||
nltk.download('punkt_tab', quiet=True) # Updated for newer NLTK versions
|
||||
nltk.download('stopwords', quiet=True)
|
||||
nltk.download('averaged_perceptron_tagger', quiet=True)
|
||||
|
||||
def analyze_writing_style(self, text_samples: List[str]) -> Dict[str, Any]:
|
||||
"""
|
||||
Comprehensive analysis of writing style from multiple text samples.
|
||||
|
||||
Args:
|
||||
text_samples: List of text samples to analyze
|
||||
|
||||
Returns:
|
||||
Detailed linguistic analysis
|
||||
"""
|
||||
try:
|
||||
logger.info(f"Analyzing writing style from {len(text_samples)} text samples")
|
||||
|
||||
# Combine all text samples
|
||||
combined_text = " ".join(text_samples)
|
||||
|
||||
# Basic metrics
|
||||
basic_metrics = self._analyze_basic_metrics(combined_text)
|
||||
|
||||
# Sentence analysis
|
||||
sentence_analysis = self._analyze_sentence_patterns(combined_text)
|
||||
|
||||
# Vocabulary analysis
|
||||
vocabulary_analysis = self._analyze_vocabulary(combined_text)
|
||||
|
||||
# Rhetorical analysis
|
||||
rhetorical_analysis = self._analyze_rhetorical_devices(combined_text)
|
||||
|
||||
# Style patterns
|
||||
style_patterns = self._analyze_style_patterns(combined_text)
|
||||
|
||||
# Readability analysis
|
||||
readability_analysis = self._analyze_readability(combined_text)
|
||||
|
||||
# Emotional tone analysis
|
||||
emotional_analysis = self._analyze_emotional_tone(combined_text)
|
||||
|
||||
# Consistency analysis
|
||||
consistency_analysis = self._analyze_consistency(text_samples)
|
||||
|
||||
return {
|
||||
"basic_metrics": basic_metrics,
|
||||
"sentence_analysis": sentence_analysis,
|
||||
"vocabulary_analysis": vocabulary_analysis,
|
||||
"rhetorical_analysis": rhetorical_analysis,
|
||||
"style_patterns": style_patterns,
|
||||
"readability_analysis": readability_analysis,
|
||||
"emotional_analysis": emotional_analysis,
|
||||
"consistency_analysis": consistency_analysis,
|
||||
"analysis_metadata": {
|
||||
"sample_count": len(text_samples),
|
||||
"total_words": basic_metrics["total_words"],
|
||||
"total_sentences": basic_metrics["total_sentences"],
|
||||
"analysis_confidence": self._calculate_analysis_confidence(text_samples)
|
||||
}
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error analyzing writing style: {str(e)}")
|
||||
return {"error": f"Failed to analyze writing style: {str(e)}"}
|
||||
|
||||
def _analyze_basic_metrics(self, text: str) -> Dict[str, Any]:
|
||||
"""Analyze basic text metrics."""
|
||||
sentences = sent_tokenize(text)
|
||||
words = word_tokenize(text.lower())
|
||||
|
||||
# Filter out punctuation
|
||||
words = [word for word in words if word.isalpha()]
|
||||
|
||||
return {
|
||||
"total_words": len(words),
|
||||
"total_sentences": len(sentences),
|
||||
"average_sentence_length": len(words) / len(sentences) if sentences else 0,
|
||||
"average_word_length": sum(len(word) for word in words) / len(words) if words else 0,
|
||||
"paragraph_count": len(text.split('\n\n')),
|
||||
"character_count": len(text),
|
||||
"character_count_no_spaces": len(text.replace(' ', ''))
|
||||
}
|
||||
|
||||
def _analyze_sentence_patterns(self, text: str) -> Dict[str, Any]:
|
||||
"""Analyze sentence structure patterns."""
|
||||
sentences = sent_tokenize(text)
|
||||
|
||||
sentence_lengths = [len(word_tokenize(sent)) for sent in sentences]
|
||||
sentence_types = []
|
||||
|
||||
for sentence in sentences:
|
||||
if sentence.endswith('?'):
|
||||
sentence_types.append('question')
|
||||
elif sentence.endswith('!'):
|
||||
sentence_types.append('exclamation')
|
||||
else:
|
||||
sentence_types.append('declarative')
|
||||
|
||||
# Analyze sentence beginnings
|
||||
sentence_beginnings = []
|
||||
for sentence in sentences:
|
||||
first_word = word_tokenize(sentence)[0].lower() if word_tokenize(sentence) else ""
|
||||
sentence_beginnings.append(first_word)
|
||||
|
||||
return {
|
||||
"sentence_length_distribution": {
|
||||
"min": min(sentence_lengths) if sentence_lengths else 0,
|
||||
"max": max(sentence_lengths) if sentence_lengths else 0,
|
||||
"average": sum(sentence_lengths) / len(sentence_lengths) if sentence_lengths else 0,
|
||||
"median": sorted(sentence_lengths)[len(sentence_lengths)//2] if sentence_lengths else 0
|
||||
},
|
||||
"sentence_type_distribution": dict(Counter(sentence_types)),
|
||||
"common_sentence_starters": dict(Counter(sentence_beginnings).most_common(10)),
|
||||
"sentence_complexity": self._analyze_sentence_complexity(sentences)
|
||||
}
|
||||
|
||||
def _analyze_vocabulary(self, text: str) -> Dict[str, Any]:
|
||||
"""Analyze vocabulary patterns and preferences."""
|
||||
words = word_tokenize(text.lower())
|
||||
words = [word for word in words if word.isalpha()]
|
||||
|
||||
# Remove stopwords for analysis
|
||||
stop_words = set(stopwords.words('english'))
|
||||
content_words = [word for word in words if word not in stop_words]
|
||||
|
||||
# POS tagging
|
||||
pos_tags = pos_tag(words)
|
||||
pos_distribution = dict(Counter(tag for word, tag in pos_tags))
|
||||
|
||||
# Vocabulary richness
|
||||
unique_words = set(words)
|
||||
unique_content_words = set(content_words)
|
||||
|
||||
return {
|
||||
"vocabulary_size": len(unique_words),
|
||||
"content_vocabulary_size": len(unique_content_words),
|
||||
"lexical_diversity": len(unique_words) / len(words) if words else 0,
|
||||
"most_frequent_words": dict(Counter(words).most_common(20)),
|
||||
"most_frequent_content_words": dict(Counter(content_words).most_common(20)),
|
||||
"pos_distribution": pos_distribution,
|
||||
"word_length_distribution": {
|
||||
"short_words": len([w for w in words if len(w) <= 4]),
|
||||
"medium_words": len([w for w in words if 5 <= len(w) <= 8]),
|
||||
"long_words": len([w for w in words if len(w) > 8])
|
||||
},
|
||||
"vocabulary_sophistication": self._analyze_vocabulary_sophistication(words)
|
||||
}
|
||||
|
||||
def _analyze_rhetorical_devices(self, text: str) -> Dict[str, Any]:
|
||||
"""Analyze rhetorical devices and techniques."""
|
||||
sentences = sent_tokenize(text)
|
||||
|
||||
rhetorical_devices = {
|
||||
"questions": len([s for s in sentences if s.strip().endswith('?')]),
|
||||
"exclamations": len([s for s in sentences if s.strip().endswith('!')]),
|
||||
"repetition": self._find_repetition_patterns(text),
|
||||
"alliteration": self._find_alliteration(text),
|
||||
"metaphors": self._find_metaphors(text),
|
||||
"analogies": self._find_analogies(text),
|
||||
"lists": self._find_lists(text),
|
||||
"contrasts": self._find_contrasts(text)
|
||||
}
|
||||
|
||||
return rhetorical_devices
|
||||
|
||||
def _analyze_style_patterns(self, text: str) -> Dict[str, Any]:
|
||||
"""Analyze writing style patterns."""
|
||||
return {
|
||||
"formality_level": self._assess_formality(text),
|
||||
"personal_pronouns": self._count_personal_pronouns(text),
|
||||
"passive_voice": self._count_passive_voice(text),
|
||||
"contractions": self._count_contractions(text),
|
||||
"transition_words": self._find_transition_words(text),
|
||||
"hedging_language": self._find_hedging_language(text),
|
||||
"emphasis_patterns": self._find_emphasis_patterns(text)
|
||||
}
|
||||
|
||||
def _analyze_readability(self, text: str) -> Dict[str, Any]:
|
||||
"""Analyze readability metrics."""
|
||||
try:
|
||||
return {
|
||||
"flesch_reading_ease": flesch_reading_ease(text),
|
||||
"flesch_kincaid_grade": flesch_kincaid_grade(text),
|
||||
"reading_level": self._determine_reading_level(flesch_reading_ease(text)),
|
||||
"complexity_score": self._calculate_complexity_score(text)
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning(f"Error calculating readability: {e}")
|
||||
return {"error": "Could not calculate readability metrics"}
|
||||
|
||||
def _analyze_emotional_tone(self, text: str) -> Dict[str, Any]:
|
||||
"""Analyze emotional tone and sentiment patterns."""
|
||||
# Simple sentiment analysis based on word patterns
|
||||
positive_words = ['good', 'great', 'excellent', 'amazing', 'wonderful', 'fantastic', 'love', 'like', 'enjoy']
|
||||
negative_words = ['bad', 'terrible', 'awful', 'hate', 'dislike', 'horrible', 'worst', 'problem', 'issue']
|
||||
|
||||
words = word_tokenize(text.lower())
|
||||
positive_count = sum(1 for word in words if word in positive_words)
|
||||
negative_count = sum(1 for word in words if word in negative_words)
|
||||
|
||||
return {
|
||||
"sentiment_bias": "positive" if positive_count > negative_count else "negative" if negative_count > positive_count else "neutral",
|
||||
"positive_word_count": positive_count,
|
||||
"negative_word_count": negative_count,
|
||||
"emotional_intensity": self._calculate_emotional_intensity(text),
|
||||
"tone_consistency": self._assess_tone_consistency(text)
|
||||
}
|
||||
|
||||
def _analyze_consistency(self, text_samples: List[str]) -> Dict[str, Any]:
|
||||
"""Analyze consistency across multiple text samples."""
|
||||
if len(text_samples) < 2:
|
||||
return {"consistency_score": 100, "note": "Only one sample provided"}
|
||||
|
||||
# Analyze consistency in various metrics
|
||||
sentence_lengths = []
|
||||
vocabulary_sets = []
|
||||
|
||||
for sample in text_samples:
|
||||
sentences = sent_tokenize(sample)
|
||||
words = word_tokenize(sample.lower())
|
||||
words = [word for word in words if word.isalpha()]
|
||||
|
||||
sentence_lengths.append([len(word_tokenize(sent)) for sent in sentences])
|
||||
vocabulary_sets.append(set(words))
|
||||
|
||||
# Calculate consistency scores
|
||||
avg_sentence_length_consistency = self._calculate_metric_consistency(
|
||||
[sum(lengths)/len(lengths) for lengths in sentence_lengths]
|
||||
)
|
||||
|
||||
vocabulary_overlap = self._calculate_vocabulary_overlap(vocabulary_sets)
|
||||
|
||||
return {
|
||||
"consistency_score": (avg_sentence_length_consistency + vocabulary_overlap) / 2,
|
||||
"sentence_length_consistency": avg_sentence_length_consistency,
|
||||
"vocabulary_consistency": vocabulary_overlap,
|
||||
"style_stability": self._assess_style_stability(text_samples)
|
||||
}
|
||||
|
||||
def _calculate_analysis_confidence(self, text_samples: List[str]) -> float:
|
||||
"""Calculate confidence in the analysis based on data quality."""
|
||||
if not text_samples:
|
||||
return 0.0
|
||||
|
||||
total_words = sum(len(word_tokenize(sample)) for sample in text_samples)
|
||||
sample_count = len(text_samples)
|
||||
|
||||
# Confidence based on amount of data
|
||||
word_confidence = min(100, (total_words / 1000) * 100) # 1000 words = 100% confidence
|
||||
sample_confidence = min(100, (sample_count / 5) * 100) # 5 samples = 100% confidence
|
||||
|
||||
return (word_confidence + sample_confidence) / 2
|
||||
|
||||
# Helper methods for specific analyses
|
||||
def _analyze_sentence_complexity(self, sentences: List[str]) -> Dict[str, Any]:
|
||||
"""Analyze sentence complexity patterns."""
|
||||
complex_sentences = 0
|
||||
compound_sentences = 0
|
||||
|
||||
for sentence in sentences:
|
||||
if ',' in sentence and ('and' in sentence or 'but' in sentence or 'or' in sentence):
|
||||
compound_sentences += 1
|
||||
if len(word_tokenize(sentence)) > 20:
|
||||
complex_sentences += 1
|
||||
|
||||
return {
|
||||
"complex_sentence_ratio": complex_sentences / len(sentences) if sentences else 0,
|
||||
"compound_sentence_ratio": compound_sentences / len(sentences) if sentences else 0,
|
||||
"average_clauses_per_sentence": self._count_clauses(sentences)
|
||||
}
|
||||
|
||||
def _analyze_vocabulary_sophistication(self, words: List[str]) -> Dict[str, Any]:
|
||||
"""Analyze vocabulary sophistication level."""
|
||||
# Simple heuristic based on word length and frequency
|
||||
long_words = [w for w in words if len(w) > 7]
|
||||
rare_words = [w for w in words if len(w) > 5] # Simplified rare word detection
|
||||
|
||||
return {
|
||||
"sophistication_score": (len(long_words) + len(rare_words)) / len(words) * 100 if words else 0,
|
||||
"long_word_ratio": len(long_words) / len(words) if words else 0,
|
||||
"rare_word_ratio": len(rare_words) / len(words) if words else 0
|
||||
}
|
||||
|
||||
def _find_repetition_patterns(self, text: str) -> Dict[str, Any]:
|
||||
"""Find repetition patterns in text."""
|
||||
words = word_tokenize(text.lower())
|
||||
word_freq = Counter(words)
|
||||
|
||||
# Find words that appear multiple times
|
||||
repeated_words = {word: count for word, count in word_freq.items() if count > 2}
|
||||
|
||||
return {
|
||||
"repeated_words": repeated_words,
|
||||
"repetition_score": len(repeated_words) / len(set(words)) * 100 if words else 0
|
||||
}
|
||||
|
||||
def _find_alliteration(self, text: str) -> List[str]:
|
||||
"""Find alliteration patterns."""
|
||||
sentences = sent_tokenize(text)
|
||||
alliterations = []
|
||||
|
||||
for sentence in sentences:
|
||||
words = word_tokenize(sentence.lower())
|
||||
words = [word for word in words if word.isalpha()]
|
||||
|
||||
if len(words) >= 2:
|
||||
for i in range(len(words) - 1):
|
||||
if words[i][0] == words[i+1][0]:
|
||||
alliterations.append(f"{words[i]} {words[i+1]}")
|
||||
|
||||
return alliterations
|
||||
|
||||
def _find_metaphors(self, text: str) -> List[str]:
|
||||
"""Find potential metaphors in text."""
|
||||
# Simple metaphor detection based on common patterns
|
||||
metaphor_patterns = [
|
||||
r'\b(is|are|was|were)\s+(like|as)\s+',
|
||||
r'\b(like|as)\s+\w+\s+(is|are|was|were)',
|
||||
r'\b(metaphorically|figuratively)'
|
||||
]
|
||||
|
||||
metaphors = []
|
||||
for pattern in metaphor_patterns:
|
||||
matches = re.findall(pattern, text, re.IGNORECASE)
|
||||
metaphors.extend(matches)
|
||||
|
||||
return metaphors
|
||||
|
||||
def _find_analogies(self, text: str) -> List[str]:
|
||||
"""Find analogies in text."""
|
||||
analogy_patterns = [
|
||||
r'\b(just as|similar to|comparable to|akin to)',
|
||||
r'\b(in the same way|likewise|similarly)'
|
||||
]
|
||||
|
||||
analogies = []
|
||||
for pattern in analogy_patterns:
|
||||
matches = re.findall(pattern, text, re.IGNORECASE)
|
||||
analogies.extend(matches)
|
||||
|
||||
return analogies
|
||||
|
||||
def _find_lists(self, text: str) -> List[str]:
|
||||
"""Find list patterns in text."""
|
||||
list_patterns = [
|
||||
r'\b(first|second|third|lastly|finally)',
|
||||
r'\b(one|two|three|four|five)',
|
||||
r'\b(•|\*|\-|\d+\.)'
|
||||
]
|
||||
|
||||
lists = []
|
||||
for pattern in list_patterns:
|
||||
matches = re.findall(pattern, text, re.IGNORECASE)
|
||||
lists.extend(matches)
|
||||
|
||||
return lists
|
||||
|
||||
def _find_contrasts(self, text: str) -> List[str]:
|
||||
"""Find contrast patterns in text."""
|
||||
contrast_words = ['but', 'however', 'although', 'whereas', 'while', 'on the other hand', 'in contrast']
|
||||
contrasts = []
|
||||
|
||||
for word in contrast_words:
|
||||
if word in text.lower():
|
||||
contrasts.append(word)
|
||||
|
||||
return contrasts
|
||||
|
||||
def _assess_formality(self, text: str) -> str:
|
||||
"""Assess formality level of text."""
|
||||
formal_indicators = ['therefore', 'furthermore', 'moreover', 'consequently', 'nevertheless']
|
||||
informal_indicators = ['gonna', 'wanna', 'gotta', 'yeah', 'ok', 'cool']
|
||||
|
||||
formal_count = sum(1 for indicator in formal_indicators if indicator in text.lower())
|
||||
informal_count = sum(1 for indicator in informal_indicators if indicator in text.lower())
|
||||
|
||||
if formal_count > informal_count:
|
||||
return "formal"
|
||||
elif informal_count > formal_count:
|
||||
return "informal"
|
||||
else:
|
||||
return "neutral"
|
||||
|
||||
def _count_personal_pronouns(self, text: str) -> Dict[str, int]:
|
||||
"""Count personal pronouns in text."""
|
||||
pronouns = ['i', 'me', 'my', 'mine', 'myself', 'we', 'us', 'our', 'ours', 'ourselves',
|
||||
'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself',
|
||||
'she', 'her', 'hers', 'herself', 'they', 'them', 'their', 'theirs', 'themselves']
|
||||
|
||||
words = word_tokenize(text.lower())
|
||||
pronoun_count = {pronoun: words.count(pronoun) for pronoun in pronouns}
|
||||
|
||||
return pronoun_count
|
||||
|
||||
def _count_passive_voice(self, text: str) -> int:
|
||||
"""Count passive voice constructions."""
|
||||
passive_patterns = [
|
||||
r'\b(was|were|is|are|been|being)\s+\w+ed\b',
|
||||
r'\b(was|were|is|are|been|being)\s+\w+en\b'
|
||||
]
|
||||
|
||||
passive_count = 0
|
||||
for pattern in passive_patterns:
|
||||
passive_count += len(re.findall(pattern, text, re.IGNORECASE))
|
||||
|
||||
return passive_count
|
||||
|
||||
def _count_contractions(self, text: str) -> int:
|
||||
"""Count contractions in text."""
|
||||
contraction_pattern = r"\b\w+'\w+\b"
|
||||
return len(re.findall(contraction_pattern, text))
|
||||
|
||||
def _find_transition_words(self, text: str) -> List[str]:
|
||||
"""Find transition words in text."""
|
||||
transition_words = ['however', 'therefore', 'furthermore', 'moreover', 'nevertheless',
|
||||
'consequently', 'meanwhile', 'additionally', 'similarly', 'likewise',
|
||||
'on the other hand', 'in contrast', 'for example', 'for instance']
|
||||
|
||||
found_transitions = []
|
||||
for word in transition_words:
|
||||
if word in text.lower():
|
||||
found_transitions.append(word)
|
||||
|
||||
return found_transitions
|
||||
|
||||
def _find_hedging_language(self, text: str) -> List[str]:
|
||||
"""Find hedging language in text."""
|
||||
hedging_words = ['might', 'could', 'possibly', 'perhaps', 'maybe', 'likely', 'probably',
|
||||
'seems', 'appears', 'suggests', 'indicates', 'tends to']
|
||||
|
||||
found_hedging = []
|
||||
for word in hedging_words:
|
||||
if word in text.lower():
|
||||
found_hedging.append(word)
|
||||
|
||||
return found_hedging
|
||||
|
||||
def _find_emphasis_patterns(self, text: str) -> Dict[str, Any]:
|
||||
"""Find emphasis patterns in text."""
|
||||
emphasis_patterns = {
|
||||
'bold_asterisks': len(re.findall(r'\*\w+\*', text)),
|
||||
'bold_underscores': len(re.findall(r'_\w+_', text)),
|
||||
'caps_words': len(re.findall(r'\b[A-Z]{2,}\b', text)),
|
||||
'exclamation_points': text.count('!'),
|
||||
'emphasis_words': len(re.findall(r'\b(very|really|extremely|absolutely|completely)\b', text, re.IGNORECASE))
|
||||
}
|
||||
|
||||
return emphasis_patterns
|
||||
|
||||
def _determine_reading_level(self, flesch_score: float) -> str:
|
||||
"""Determine reading level from Flesch score."""
|
||||
if flesch_score >= 90:
|
||||
return "very_easy"
|
||||
elif flesch_score >= 80:
|
||||
return "easy"
|
||||
elif flesch_score >= 70:
|
||||
return "fairly_easy"
|
||||
elif flesch_score >= 60:
|
||||
return "standard"
|
||||
elif flesch_score >= 50:
|
||||
return "fairly_difficult"
|
||||
elif flesch_score >= 30:
|
||||
return "difficult"
|
||||
else:
|
||||
return "very_difficult"
|
||||
|
||||
def _calculate_complexity_score(self, text: str) -> float:
|
||||
"""Calculate overall complexity score."""
|
||||
sentences = sent_tokenize(text)
|
||||
words = word_tokenize(text.lower())
|
||||
words = [word for word in words if word.isalpha()]
|
||||
|
||||
if not sentences or not words:
|
||||
return 0.0
|
||||
|
||||
# Factors: sentence length, word length, vocabulary diversity
|
||||
avg_sentence_length = len(words) / len(sentences)
|
||||
avg_word_length = sum(len(word) for word in words) / len(words)
|
||||
vocabulary_diversity = len(set(words)) / len(words)
|
||||
|
||||
# Normalize and combine
|
||||
complexity = (avg_sentence_length / 20) * 0.4 + (avg_word_length / 10) * 0.3 + vocabulary_diversity * 0.3
|
||||
|
||||
return min(100, complexity * 100)
|
||||
|
||||
def _calculate_emotional_intensity(self, text: str) -> float:
|
||||
"""Calculate emotional intensity of text."""
|
||||
emotional_words = ['amazing', 'incredible', 'fantastic', 'terrible', 'awful', 'horrible',
|
||||
'love', 'hate', 'passion', 'fury', 'joy', 'sorrow', 'excitement', 'fear']
|
||||
|
||||
words = word_tokenize(text.lower())
|
||||
emotional_word_count = sum(1 for word in words if word in emotional_words)
|
||||
|
||||
return (emotional_word_count / len(words)) * 100 if words else 0
|
||||
|
||||
def _assess_tone_consistency(self, text: str) -> float:
|
||||
"""Assess tone consistency throughout text."""
|
||||
# Simple heuristic: check for tone shifts
|
||||
sentences = sent_tokenize(text)
|
||||
if len(sentences) < 2:
|
||||
return 100.0
|
||||
|
||||
# Analyze first half vs second half
|
||||
mid_point = len(sentences) // 2
|
||||
first_half = " ".join(sentences[:mid_point])
|
||||
second_half = " ".join(sentences[mid_point:])
|
||||
|
||||
first_tone = self._analyze_emotional_tone(first_half)
|
||||
second_tone = self._analyze_emotional_tone(second_half)
|
||||
|
||||
# Calculate consistency based on sentiment similarity
|
||||
if first_tone["sentiment_bias"] == second_tone["sentiment_bias"]:
|
||||
return 100.0
|
||||
else:
|
||||
return 50.0
|
||||
|
||||
def _calculate_metric_consistency(self, values: List[float]) -> float:
|
||||
"""Calculate consistency of a metric across samples."""
|
||||
if len(values) < 2:
|
||||
return 100.0
|
||||
|
||||
mean_value = sum(values) / len(values)
|
||||
variance = sum((x - mean_value) ** 2 for x in values) / len(values)
|
||||
std_dev = variance ** 0.5
|
||||
|
||||
# Convert to consistency score (lower std dev = higher consistency)
|
||||
consistency = max(0, 100 - (std_dev / mean_value * 100)) if mean_value > 0 else 100
|
||||
|
||||
return consistency
|
||||
|
||||
def _calculate_vocabulary_overlap(self, vocabulary_sets: List[set]) -> float:
|
||||
"""Calculate vocabulary overlap across samples."""
|
||||
if len(vocabulary_sets) < 2:
|
||||
return 100.0
|
||||
|
||||
# Calculate pairwise overlaps
|
||||
overlaps = []
|
||||
for i in range(len(vocabulary_sets)):
|
||||
for j in range(i + 1, len(vocabulary_sets)):
|
||||
intersection = len(vocabulary_sets[i] & vocabulary_sets[j])
|
||||
union = len(vocabulary_sets[i] | vocabulary_sets[j])
|
||||
overlap = (intersection / union * 100) if union > 0 else 0
|
||||
overlaps.append(overlap)
|
||||
|
||||
return sum(overlaps) / len(overlaps) if overlaps else 0
|
||||
|
||||
def _assess_style_stability(self, text_samples: List[str]) -> Dict[str, Any]:
|
||||
"""Assess style stability across samples."""
|
||||
if len(text_samples) < 2:
|
||||
return {"stability_score": 100, "note": "Only one sample provided"}
|
||||
|
||||
# Analyze consistency in key style metrics
|
||||
metrics = []
|
||||
for sample in text_samples:
|
||||
sample_metrics = {
|
||||
"avg_sentence_length": len(word_tokenize(sample)) / len(sent_tokenize(sample)),
|
||||
"formality": self._assess_formality(sample),
|
||||
"emotional_intensity": self._calculate_emotional_intensity(sample)
|
||||
}
|
||||
metrics.append(sample_metrics)
|
||||
|
||||
# Calculate stability scores
|
||||
sentence_length_stability = self._calculate_metric_consistency(
|
||||
[m["avg_sentence_length"] for m in metrics]
|
||||
)
|
||||
|
||||
emotional_stability = self._calculate_metric_consistency(
|
||||
[m["emotional_intensity"] for m in metrics]
|
||||
)
|
||||
|
||||
# Formality consistency
|
||||
formality_values = [m["formality"] for m in metrics]
|
||||
formality_consistency = 100 if len(set(formality_values)) == 1 else 50
|
||||
|
||||
overall_stability = (sentence_length_stability + emotional_stability + formality_consistency) / 3
|
||||
|
||||
return {
|
||||
"stability_score": overall_stability,
|
||||
"sentence_length_stability": sentence_length_stability,
|
||||
"emotional_stability": emotional_stability,
|
||||
"formality_consistency": formality_consistency
|
||||
}
|
||||
|
||||
def _count_clauses(self, sentences: List[str]) -> float:
|
||||
"""Count average clauses per sentence."""
|
||||
total_clauses = 0
|
||||
for sentence in sentences:
|
||||
# Simple clause counting based on conjunctions and punctuation
|
||||
clauses = len(re.findall(r'[,;]', sentence)) + 1
|
||||
total_clauses += clauses
|
||||
|
||||
return total_clauses / len(sentences) if sentences else 0
|
||||
213
backend/services/persona/facebook/facebook_persona_prompts.py
Normal file
213
backend/services/persona/facebook/facebook_persona_prompts.py
Normal file
@@ -0,0 +1,213 @@
|
||||
"""
|
||||
Facebook Persona Prompts
|
||||
Contains Facebook-specific persona prompt generation logic.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class FacebookPersonaPrompts:
|
||||
"""Facebook-specific persona prompt generation."""
|
||||
|
||||
@staticmethod
|
||||
def build_facebook_system_prompt(core_persona: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Build optimized system prompt with core persona for Facebook generation.
|
||||
This moves the core persona to system prompt to free up context window.
|
||||
"""
|
||||
import json
|
||||
|
||||
return f"""You are an expert Facebook content strategist specializing in community engagement and social sharing optimization.
|
||||
|
||||
CORE PERSONA FOUNDATION:
|
||||
{json.dumps(core_persona, indent=2)}
|
||||
|
||||
TASK: Create Facebook-optimized persona adaptations that maintain core identity while maximizing community engagement and Facebook algorithm performance.
|
||||
|
||||
FOCUS AREAS:
|
||||
- Community-focused tone and engagement strategies
|
||||
- Facebook algorithm optimization (engagement, reach, timing)
|
||||
- Social sharing and viral content potential
|
||||
- Facebook-specific features (Stories, Reels, Live, Groups, Events)
|
||||
- Audience interaction and community building"""
|
||||
|
||||
@staticmethod
|
||||
def build_focused_facebook_prompt(onboarding_data: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Build focused Facebook prompt without core persona JSON to optimize context usage.
|
||||
"""
|
||||
# Extract audience context
|
||||
audience_context = FacebookPersonaPrompts._extract_audience_context(onboarding_data)
|
||||
|
||||
target_audience = audience_context.get("target_audience", "general")
|
||||
content_goals = audience_context.get("content_goals", "engagement")
|
||||
business_type = audience_context.get("business_type", "general")
|
||||
|
||||
return f"""FACEBOOK OPTIMIZATION TASK: Create Facebook-specific adaptations for the core persona.
|
||||
|
||||
AUDIENCE CONTEXT:
|
||||
- Target: {target_audience} | Goals: {content_goals} | Business: {business_type}
|
||||
- Demographics: {audience_context.get('demographics', [])}
|
||||
- Interests: {audience_context.get('interests', [])}
|
||||
- Behaviors: {audience_context.get('behaviors', [])}
|
||||
|
||||
FACEBOOK SPECS:
|
||||
- Character Limit: 63,206 | Optimal Length: 40-80 words
|
||||
- Algorithm Priority: Engagement, meaningful interactions, community building
|
||||
- Content Types: Posts, Stories, Reels, Live, Events, Groups, Carousels, Polls
|
||||
- Hashtag Strategy: 1-2 recommended (max 30)
|
||||
- Link Strategy: Native content performs better
|
||||
|
||||
OPTIMIZATION REQUIREMENTS:
|
||||
|
||||
1. COMMUNITY-FOCUSED TONE:
|
||||
- Authentic, conversational, approachable language
|
||||
- Balance professionalism with relatability
|
||||
- Incorporate storytelling and personal anecdotes
|
||||
- Community-building elements
|
||||
|
||||
2. CONTENT STRATEGY FOR {business_type.upper()}:
|
||||
- Community engagement content for {target_audience}
|
||||
- Social sharing optimization for {content_goals}
|
||||
- Facebook-specific content formats
|
||||
- Audience interaction strategies
|
||||
- Viral content potential
|
||||
|
||||
3. FACEBOOK-SPECIFIC ADAPTATIONS:
|
||||
- Algorithm optimization (engagement, reach, timing)
|
||||
- Platform-specific vocabulary and terminology
|
||||
- Engagement patterns for Facebook audience
|
||||
- Community interaction strategies
|
||||
- Facebook feature optimization (Stories, Reels, Live, Events, Groups)
|
||||
|
||||
4. AUDIENCE TARGETING:
|
||||
- Demographic-specific positioning
|
||||
- Interest-based content adaptation
|
||||
- Behavioral targeting considerations
|
||||
- Community building strategies
|
||||
- Engagement optimization tactics
|
||||
|
||||
Generate comprehensive Facebook-optimized persona maintaining core identity while maximizing community engagement and social sharing potential."""
|
||||
|
||||
@staticmethod
|
||||
def _extract_audience_context(onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Extract audience context from onboarding data."""
|
||||
try:
|
||||
# Get enhanced analysis data
|
||||
enhanced_analysis = onboarding_data.get("enhanced_analysis", {})
|
||||
website_analysis = onboarding_data.get("website_analysis", {}) or {}
|
||||
research_prefs = onboarding_data.get("research_preferences", {}) or {}
|
||||
|
||||
# Extract audience intelligence
|
||||
audience_intel = enhanced_analysis.get("audience_intelligence", {})
|
||||
|
||||
# Extract target audience from website analysis
|
||||
target_audience_data = website_analysis.get("target_audience", {}) or {}
|
||||
|
||||
# Build audience context
|
||||
audience_context = {
|
||||
"target_audience": target_audience_data.get("primary_audience", "general"),
|
||||
"content_goals": research_prefs.get("content_goals", "engagement"),
|
||||
"business_type": website_analysis.get("business_type", "general"),
|
||||
"demographics": audience_intel.get("demographics", []),
|
||||
"interests": audience_intel.get("interests", []),
|
||||
"behaviors": audience_intel.get("behaviors", []),
|
||||
"psychographic_profile": audience_intel.get("psychographic_profile", "general"),
|
||||
"pain_points": audience_intel.get("pain_points", []),
|
||||
"engagement_level": audience_intel.get("engagement_level", "moderate")
|
||||
}
|
||||
|
||||
return audience_context
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Error extracting audience context: {str(e)}")
|
||||
return {
|
||||
"target_audience": "general",
|
||||
"content_goals": "engagement",
|
||||
"business_type": "general",
|
||||
"demographics": [],
|
||||
"interests": [],
|
||||
"behaviors": [],
|
||||
"psychographic_profile": "general",
|
||||
"pain_points": [],
|
||||
"engagement_level": "moderate"
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def build_facebook_validation_prompt(persona_data: Dict[str, Any]) -> str:
|
||||
"""Build optimized prompt for validating Facebook persona data."""
|
||||
return f"""FACEBOOK PERSONA VALIDATION TASK: Validate Facebook persona data for completeness and quality.
|
||||
|
||||
PERSONA DATA:
|
||||
{persona_data}
|
||||
|
||||
VALIDATION REQUIREMENTS:
|
||||
|
||||
1. COMPLETENESS CHECK:
|
||||
- Verify all required Facebook-specific fields are present
|
||||
- Check for missing algorithm optimization strategies
|
||||
- Validate engagement strategy completeness
|
||||
- Ensure content format rules are defined
|
||||
|
||||
2. QUALITY ASSESSMENT:
|
||||
- Evaluate Facebook algorithm optimization quality
|
||||
- Assess engagement strategy effectiveness
|
||||
- Check content format optimization
|
||||
- Validate audience targeting strategies
|
||||
|
||||
3. FACEBOOK-SPECIFIC VALIDATION:
|
||||
- Verify Facebook platform constraints are respected
|
||||
- Check for Facebook-specific best practices
|
||||
- Validate community building strategies
|
||||
- Ensure Facebook feature optimization
|
||||
|
||||
4. RECOMMENDATIONS:
|
||||
- Provide specific improvement suggestions
|
||||
- Identify missing optimization opportunities
|
||||
- Suggest Facebook-specific enhancements
|
||||
- Recommend engagement strategy improvements
|
||||
|
||||
Generate comprehensive validation report with scores, recommendations, and specific improvement suggestions for Facebook optimization."""
|
||||
|
||||
@staticmethod
|
||||
def build_facebook_optimization_prompt(persona_data: Dict[str, Any]) -> str:
|
||||
"""Build optimized prompt for optimizing Facebook persona data."""
|
||||
return f"""FACEBOOK PERSONA OPTIMIZATION TASK: Optimize Facebook persona data for maximum algorithm performance and community engagement.
|
||||
|
||||
CURRENT PERSONA DATA:
|
||||
{persona_data}
|
||||
|
||||
OPTIMIZATION REQUIREMENTS:
|
||||
|
||||
1. ALGORITHM OPTIMIZATION:
|
||||
- Enhance Facebook algorithm performance strategies
|
||||
- Optimize for Facebook's engagement metrics
|
||||
- Improve content timing and frequency
|
||||
- Enhance audience targeting precision
|
||||
|
||||
2. ENGAGEMENT OPTIMIZATION:
|
||||
- Strengthen community building strategies
|
||||
- Enhance social sharing potential
|
||||
- Improve audience interaction tactics
|
||||
- Optimize content for viral potential
|
||||
|
||||
3. CONTENT FORMAT OPTIMIZATION:
|
||||
- Optimize for Facebook's content formats
|
||||
- Enhance visual content strategies
|
||||
- Improve video content optimization
|
||||
- Optimize for Facebook Stories and Reels
|
||||
|
||||
4. AUDIENCE TARGETING OPTIMIZATION:
|
||||
- Refine demographic targeting
|
||||
- Enhance interest-based targeting
|
||||
- Improve behavioral targeting
|
||||
- Optimize for Facebook's audience insights
|
||||
|
||||
5. COMMUNITY BUILDING OPTIMIZATION:
|
||||
- Enhance group management strategies
|
||||
- Improve event management tactics
|
||||
- Optimize live streaming strategies
|
||||
- Enhance community interaction methods
|
||||
|
||||
Generate optimized Facebook persona data with enhanced algorithm performance, engagement strategies, and community building tactics."""
|
||||
239
backend/services/persona/facebook/facebook_persona_scheduler.py
Normal file
239
backend/services/persona/facebook/facebook_persona_scheduler.py
Normal file
@@ -0,0 +1,239 @@
|
||||
"""
|
||||
Facebook Persona Scheduler
|
||||
Handles scheduled generation of Facebook personas after onboarding.
|
||||
"""
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Dict, Any
|
||||
from loguru import logger
|
||||
|
||||
from services.database import get_db_session
|
||||
from services.persona_data_service import PersonaDataService
|
||||
from services.persona.facebook.facebook_persona_service import FacebookPersonaService
|
||||
from services.onboarding.database_service import OnboardingDatabaseService
|
||||
from models.scheduler_models import SchedulerEventLog
|
||||
|
||||
|
||||
async def generate_facebook_persona_task(user_id: str):
|
||||
"""
|
||||
Async task function to generate Facebook persona for a user.
|
||||
|
||||
This function is called by the scheduler 20 minutes after onboarding completion.
|
||||
|
||||
Args:
|
||||
user_id: User ID (Clerk string)
|
||||
"""
|
||||
db = None
|
||||
try:
|
||||
logger.info(f"Scheduled Facebook persona generation started for user {user_id}")
|
||||
|
||||
db = get_db_session()
|
||||
if not db:
|
||||
logger.error(f"Failed to get database session for Facebook persona generation (user: {user_id})")
|
||||
return
|
||||
|
||||
# Get persona data service
|
||||
persona_data_service = PersonaDataService(db_session=db)
|
||||
onboarding_service = OnboardingDatabaseService(db=db)
|
||||
|
||||
# Get core persona (required for Facebook persona)
|
||||
persona_data = persona_data_service.get_user_persona_data(user_id)
|
||||
if not persona_data or not persona_data.get('core_persona'):
|
||||
logger.warning(f"No core persona found for user {user_id}, cannot generate Facebook persona")
|
||||
return
|
||||
|
||||
core_persona = persona_data.get('core_persona', {})
|
||||
|
||||
# Get onboarding data for context
|
||||
website_analysis = onboarding_service.get_website_analysis(user_id, db)
|
||||
research_prefs = onboarding_service.get_research_preferences(user_id, db)
|
||||
|
||||
onboarding_data = {
|
||||
"website_url": website_analysis.get('website_url', '') if website_analysis else '',
|
||||
"writing_style": website_analysis.get('writing_style', {}) if website_analysis else {},
|
||||
"content_characteristics": website_analysis.get('content_characteristics', {}) if website_analysis else {},
|
||||
"target_audience": website_analysis.get('target_audience', '') if website_analysis else '',
|
||||
"research_preferences": research_prefs or {}
|
||||
}
|
||||
|
||||
# Check if persona already exists to avoid unnecessary API calls
|
||||
platform_personas = persona_data.get('platform_personas', {}) if persona_data else {}
|
||||
if platform_personas.get('facebook'):
|
||||
logger.info(f"Facebook persona already exists for user {user_id}, skipping generation")
|
||||
return
|
||||
|
||||
start_time = datetime.utcnow()
|
||||
# Generate Facebook persona
|
||||
facebook_service = FacebookPersonaService()
|
||||
try:
|
||||
generated_persona = facebook_service.generate_facebook_persona(
|
||||
core_persona,
|
||||
onboarding_data
|
||||
)
|
||||
execution_time = (datetime.utcnow() - start_time).total_seconds()
|
||||
|
||||
if generated_persona and "error" not in generated_persona:
|
||||
# Save to database
|
||||
success = persona_data_service.save_platform_persona(user_id, 'facebook', generated_persona)
|
||||
if success:
|
||||
logger.info(f"✅ Scheduled Facebook persona generation completed for user {user_id}")
|
||||
|
||||
# Log success to scheduler event log for dashboard
|
||||
try:
|
||||
event_log = SchedulerEventLog(
|
||||
event_type='job_completed',
|
||||
event_date=start_time,
|
||||
job_id=f"facebook_persona_{user_id}",
|
||||
job_type='one_time',
|
||||
user_id=user_id,
|
||||
event_data={
|
||||
'job_function': 'generate_facebook_persona_task',
|
||||
'execution_time_seconds': execution_time,
|
||||
'status': 'success'
|
||||
}
|
||||
)
|
||||
db.add(event_log)
|
||||
db.commit()
|
||||
except Exception as log_error:
|
||||
logger.warning(f"Failed to log Facebook persona generation success to scheduler event log: {log_error}")
|
||||
if db:
|
||||
db.rollback()
|
||||
else:
|
||||
error_msg = f"Failed to save Facebook persona for user {user_id}"
|
||||
logger.warning(f"⚠️ {error_msg}")
|
||||
|
||||
# Log failure to scheduler event log
|
||||
try:
|
||||
event_log = SchedulerEventLog(
|
||||
event_type='job_failed',
|
||||
event_date=start_time,
|
||||
job_id=f"facebook_persona_{user_id}",
|
||||
job_type='one_time',
|
||||
user_id=user_id,
|
||||
error_message=error_msg,
|
||||
event_data={
|
||||
'job_function': 'generate_facebook_persona_task',
|
||||
'execution_time_seconds': execution_time,
|
||||
'status': 'failed',
|
||||
'failure_reason': 'save_failed',
|
||||
'expensive_api_call': True
|
||||
}
|
||||
)
|
||||
db.add(event_log)
|
||||
db.commit()
|
||||
except Exception as log_error:
|
||||
logger.warning(f"Failed to log Facebook persona save failure to scheduler event log: {log_error}")
|
||||
if db:
|
||||
db.rollback()
|
||||
else:
|
||||
error_msg = f"Scheduled Facebook persona generation failed for user {user_id}: {generated_persona}"
|
||||
logger.error(f"❌ {error_msg}")
|
||||
|
||||
# Log failure to scheduler event log for dashboard visibility
|
||||
try:
|
||||
event_log = SchedulerEventLog(
|
||||
event_type='job_failed',
|
||||
event_date=start_time,
|
||||
job_id=f"facebook_persona_{user_id}", # Match scheduled job ID format
|
||||
job_type='one_time',
|
||||
user_id=user_id,
|
||||
error_message=error_msg,
|
||||
event_data={
|
||||
'job_function': 'generate_facebook_persona_task',
|
||||
'execution_time_seconds': execution_time,
|
||||
'status': 'failed',
|
||||
'failure_reason': 'generation_returned_error',
|
||||
'expensive_api_call': True
|
||||
}
|
||||
)
|
||||
db.add(event_log)
|
||||
db.commit()
|
||||
except Exception as log_error:
|
||||
logger.warning(f"Failed to log Facebook persona generation failure to scheduler event log: {log_error}")
|
||||
if db:
|
||||
db.rollback()
|
||||
except Exception as gen_error:
|
||||
execution_time = (datetime.utcnow() - start_time).total_seconds()
|
||||
error_msg = f"Exception during scheduled Facebook persona generation for user {user_id}: {str(gen_error)}. Expensive API call may have been made."
|
||||
logger.error(f"❌ {error_msg}")
|
||||
|
||||
# Log exception to scheduler event log for dashboard visibility
|
||||
try:
|
||||
event_log = SchedulerEventLog(
|
||||
event_type='job_failed',
|
||||
event_date=start_time,
|
||||
job_id=f"facebook_persona_{user_id}", # Match scheduled job ID format
|
||||
job_type='one_time',
|
||||
user_id=user_id,
|
||||
error_message=error_msg,
|
||||
event_data={
|
||||
'job_function': 'generate_facebook_persona_task',
|
||||
'execution_time_seconds': execution_time,
|
||||
'status': 'failed',
|
||||
'failure_reason': 'exception',
|
||||
'exception_type': type(gen_error).__name__,
|
||||
'exception_message': str(gen_error),
|
||||
'expensive_api_call': True
|
||||
}
|
||||
)
|
||||
db.add(event_log)
|
||||
db.commit()
|
||||
except Exception as log_error:
|
||||
logger.warning(f"Failed to log Facebook persona generation exception to scheduler event log: {log_error}")
|
||||
if db:
|
||||
db.rollback()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in scheduled Facebook persona generation for user {user_id}: {e}")
|
||||
finally:
|
||||
if db:
|
||||
try:
|
||||
db.close()
|
||||
except Exception as e:
|
||||
logger.error(f"Error closing database session: {e}")
|
||||
|
||||
|
||||
def schedule_facebook_persona_generation(user_id: str, delay_minutes: int = 20) -> str:
|
||||
"""
|
||||
Schedule Facebook persona generation for a user after a delay.
|
||||
|
||||
Args:
|
||||
user_id: User ID (Clerk string)
|
||||
delay_minutes: Delay in minutes before generating persona (default: 20)
|
||||
|
||||
Returns:
|
||||
Job ID
|
||||
"""
|
||||
try:
|
||||
from services.scheduler import get_scheduler
|
||||
|
||||
scheduler = get_scheduler()
|
||||
|
||||
# Calculate run date (current time + delay) - ensure UTC timezone-aware
|
||||
run_date = datetime.now(timezone.utc) + timedelta(minutes=delay_minutes)
|
||||
|
||||
# Generate consistent job ID (without timestamp) for proper restoration
|
||||
# This allows restoration to find and restore the job with original scheduled time
|
||||
# Note: Clerk user_id already includes "user_" prefix, so we don't add it again
|
||||
job_id = f"facebook_persona_{user_id}"
|
||||
|
||||
# Schedule the task
|
||||
scheduled_job_id = scheduler.schedule_one_time_task(
|
||||
func=generate_facebook_persona_task,
|
||||
run_date=run_date,
|
||||
job_id=job_id,
|
||||
kwargs={"user_id": user_id},
|
||||
replace_existing=True
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"Scheduled Facebook persona generation for user {user_id} "
|
||||
f"at {run_date} (job_id: {scheduled_job_id})"
|
||||
)
|
||||
|
||||
return scheduled_job_id
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to schedule Facebook persona generation for user {user_id}: {e}")
|
||||
raise
|
||||
|
||||
364
backend/services/persona/facebook/facebook_persona_schemas.py
Normal file
364
backend/services/persona/facebook/facebook_persona_schemas.py
Normal file
@@ -0,0 +1,364 @@
|
||||
"""
|
||||
Facebook Persona Schemas
|
||||
Defines Facebook-specific persona data structures and validation schemas.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, List, Optional
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class FacebookPersonaSchema(BaseModel):
|
||||
"""Facebook-specific persona schema with platform optimizations."""
|
||||
|
||||
# Core persona fields (inherited from base persona)
|
||||
persona_name: str = Field(..., description="Name of the persona")
|
||||
archetype: str = Field(..., description="Persona archetype")
|
||||
core_belief: str = Field(..., description="Core belief driving the persona")
|
||||
|
||||
# Facebook-specific optimizations
|
||||
facebook_algorithm_optimization: Dict[str, Any] = Field(
|
||||
default_factory=dict,
|
||||
description="Facebook algorithm optimization strategies"
|
||||
)
|
||||
|
||||
facebook_engagement_strategies: Dict[str, Any] = Field(
|
||||
default_factory=dict,
|
||||
description="Facebook-specific engagement strategies"
|
||||
)
|
||||
|
||||
facebook_content_formats: Dict[str, Any] = Field(
|
||||
default_factory=dict,
|
||||
description="Facebook content format optimizations"
|
||||
)
|
||||
|
||||
facebook_audience_targeting: Dict[str, Any] = Field(
|
||||
default_factory=dict,
|
||||
description="Facebook audience targeting strategies"
|
||||
)
|
||||
|
||||
facebook_community_building: Dict[str, Any] = Field(
|
||||
default_factory=dict,
|
||||
description="Facebook community building strategies"
|
||||
)
|
||||
|
||||
|
||||
class FacebookPersonaConstraints:
|
||||
"""Facebook platform constraints and best practices."""
|
||||
|
||||
@staticmethod
|
||||
def get_facebook_constraints() -> Dict[str, Any]:
|
||||
"""Get Facebook-specific platform constraints."""
|
||||
return {
|
||||
"character_limit": 63206,
|
||||
"optimal_length": "40-80 words",
|
||||
"hashtag_limit": 30,
|
||||
"image_support": True,
|
||||
"video_support": True,
|
||||
"link_preview": True,
|
||||
"event_support": True,
|
||||
"group_sharing": True,
|
||||
"story_support": True,
|
||||
"reel_support": True,
|
||||
"carousel_support": True,
|
||||
"poll_support": True,
|
||||
"live_support": True,
|
||||
"algorithm_favors": [
|
||||
"engagement",
|
||||
"meaningful_interactions",
|
||||
"video_content",
|
||||
"community_posts",
|
||||
"authentic_content"
|
||||
],
|
||||
"content_types": [
|
||||
"text_posts",
|
||||
"image_posts",
|
||||
"video_posts",
|
||||
"carousel_posts",
|
||||
"story_posts",
|
||||
"reel_posts",
|
||||
"event_posts",
|
||||
"poll_posts",
|
||||
"live_posts"
|
||||
],
|
||||
"engagement_metrics": [
|
||||
"likes",
|
||||
"comments",
|
||||
"shares",
|
||||
"saves",
|
||||
"clicks",
|
||||
"reactions",
|
||||
"video_views",
|
||||
"story_views"
|
||||
],
|
||||
"posting_frequency": {
|
||||
"optimal": "1-2 times per day",
|
||||
"maximum": "3-4 times per day",
|
||||
"minimum": "3-4 times per week"
|
||||
},
|
||||
"best_posting_times": [
|
||||
"9:00 AM - 11:00 AM",
|
||||
"1:00 PM - 3:00 PM",
|
||||
"5:00 PM - 7:00 PM"
|
||||
],
|
||||
"content_guidelines": {
|
||||
"authenticity": "High priority - Facebook favors authentic content",
|
||||
"community_focus": "Build community and meaningful connections",
|
||||
"visual_content": "Images and videos perform better than text-only",
|
||||
"engagement_bait": "Avoid engagement bait - Facebook penalizes it",
|
||||
"clickbait": "Avoid clickbait headlines and misleading content"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class FacebookPersonaValidation:
|
||||
"""Facebook persona validation rules and scoring."""
|
||||
|
||||
@staticmethod
|
||||
def validate_facebook_persona(persona_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Validate Facebook persona data for completeness and quality."""
|
||||
|
||||
validation_results = {
|
||||
"is_valid": True,
|
||||
"quality_score": 0.0,
|
||||
"completeness_score": 0.0,
|
||||
"facebook_optimization_score": 0.0,
|
||||
"engagement_strategy_score": 0.0,
|
||||
"missing_fields": [],
|
||||
"incomplete_fields": [],
|
||||
"recommendations": [],
|
||||
"quality_issues": [],
|
||||
"strengths": [],
|
||||
"validation_details": {}
|
||||
}
|
||||
|
||||
# Check required fields
|
||||
required_fields = [
|
||||
"persona_name", "archetype", "core_belief",
|
||||
"facebook_algorithm_optimization", "facebook_engagement_strategies"
|
||||
]
|
||||
|
||||
for field in required_fields:
|
||||
if not persona_data.get(field):
|
||||
validation_results["missing_fields"].append(field)
|
||||
validation_results["is_valid"] = False
|
||||
|
||||
# Calculate completeness score
|
||||
total_fields = len(required_fields)
|
||||
present_fields = total_fields - len(validation_results["missing_fields"])
|
||||
validation_results["completeness_score"] = (present_fields / total_fields) * 100
|
||||
|
||||
# Validate Facebook-specific optimizations
|
||||
facebook_opt = persona_data.get("facebook_algorithm_optimization", {})
|
||||
if facebook_opt:
|
||||
validation_results["facebook_optimization_score"] = 85.0
|
||||
validation_results["strengths"].append("Facebook algorithm optimization present")
|
||||
else:
|
||||
validation_results["quality_issues"].append("Missing Facebook algorithm optimization")
|
||||
validation_results["recommendations"].append("Add Facebook-specific algorithm strategies")
|
||||
|
||||
# Validate engagement strategies
|
||||
engagement_strategies = persona_data.get("facebook_engagement_strategies", {})
|
||||
if engagement_strategies:
|
||||
validation_results["engagement_strategy_score"] = 80.0
|
||||
validation_results["strengths"].append("Facebook engagement strategies defined")
|
||||
else:
|
||||
validation_results["quality_issues"].append("Missing Facebook engagement strategies")
|
||||
validation_results["recommendations"].append("Define Facebook-specific engagement tactics")
|
||||
|
||||
# Calculate overall quality score
|
||||
validation_results["quality_score"] = (
|
||||
validation_results["completeness_score"] * 0.4 +
|
||||
validation_results["facebook_optimization_score"] * 0.3 +
|
||||
validation_results["engagement_strategy_score"] * 0.3
|
||||
)
|
||||
|
||||
# Add validation details
|
||||
validation_results["validation_details"] = {
|
||||
"total_fields_checked": total_fields,
|
||||
"present_fields": present_fields,
|
||||
"facebook_optimization_present": bool(facebook_opt),
|
||||
"engagement_strategies_present": bool(engagement_strategies),
|
||||
"validation_timestamp": "2024-01-01T00:00:00Z" # Will be updated with actual timestamp
|
||||
}
|
||||
|
||||
return validation_results
|
||||
|
||||
|
||||
class FacebookPersonaOptimization:
|
||||
"""Facebook persona optimization strategies and techniques."""
|
||||
|
||||
@staticmethod
|
||||
def get_facebook_optimization_strategies() -> Dict[str, Any]:
|
||||
"""Get comprehensive Facebook optimization strategies."""
|
||||
return {
|
||||
"algorithm_optimization": {
|
||||
"engagement_optimization": [
|
||||
"Post when your audience is most active",
|
||||
"Use Facebook's native video uploads instead of external links",
|
||||
"Encourage meaningful comments and discussions",
|
||||
"Respond to comments within 2 hours",
|
||||
"Use Facebook Live for real-time engagement",
|
||||
"Create shareable, valuable content",
|
||||
"Use Facebook Stories for behind-the-scenes content",
|
||||
"Leverage Facebook Groups for community building"
|
||||
],
|
||||
"content_quality_optimization": [
|
||||
"Create authentic, original content",
|
||||
"Use high-quality images and videos",
|
||||
"Write compelling captions that encourage engagement",
|
||||
"Use Facebook's built-in editing tools",
|
||||
"Create content that sparks conversations",
|
||||
"Share user-generated content",
|
||||
"Use Facebook's trending topics and hashtags",
|
||||
"Create content that provides value to your audience"
|
||||
],
|
||||
"timing_optimization": [
|
||||
"Post during peak engagement hours (9-11 AM, 1-3 PM, 5-7 PM)",
|
||||
"Use Facebook Insights to find your best posting times",
|
||||
"Post consistently but not too frequently",
|
||||
"Schedule posts for different time zones if global audience",
|
||||
"Use Facebook's scheduling feature for optimal timing",
|
||||
"Post when your competitors are less active",
|
||||
"Consider your audience's daily routines and habits"
|
||||
],
|
||||
"audience_targeting_optimization": [
|
||||
"Use Facebook's audience insights for targeting",
|
||||
"Create content for specific audience segments",
|
||||
"Use Facebook's lookalike audiences",
|
||||
"Target based on interests and behaviors",
|
||||
"Use Facebook's custom audiences",
|
||||
"Create content that resonates with your core audience",
|
||||
"Use Facebook's demographic targeting",
|
||||
"Leverage Facebook's psychographic targeting"
|
||||
]
|
||||
},
|
||||
"engagement_strategies": {
|
||||
"community_building": [
|
||||
"Create and moderate Facebook Groups",
|
||||
"Host Facebook Live sessions regularly",
|
||||
"Respond to all comments and messages",
|
||||
"Share user-generated content",
|
||||
"Create Facebook Events for community gatherings",
|
||||
"Use Facebook's community features",
|
||||
"Encourage user participation and feedback",
|
||||
"Build relationships with your audience"
|
||||
],
|
||||
"content_engagement": [
|
||||
"Ask questions in your posts",
|
||||
"Use polls and surveys to engage audience",
|
||||
"Create interactive content like quizzes",
|
||||
"Use Facebook's reaction buttons strategically",
|
||||
"Create content that encourages sharing",
|
||||
"Use Facebook's tagging feature appropriately",
|
||||
"Create content that sparks discussions",
|
||||
"Use Facebook's story features for engagement"
|
||||
],
|
||||
"conversion_optimization": [
|
||||
"Use clear call-to-actions in posts",
|
||||
"Create Facebook-specific landing pages",
|
||||
"Use Facebook's conversion tracking",
|
||||
"Create content that drives traffic to your website",
|
||||
"Use Facebook's lead generation features",
|
||||
"Create content that builds trust and credibility",
|
||||
"Use Facebook's retargeting capabilities",
|
||||
"Create content that showcases your products/services"
|
||||
]
|
||||
},
|
||||
"content_formats": {
|
||||
"text_posts": {
|
||||
"optimal_length": "40-80 words",
|
||||
"best_practices": [
|
||||
"Use compelling headlines",
|
||||
"Include relevant hashtags (1-2)",
|
||||
"Ask questions to encourage engagement",
|
||||
"Use emojis sparingly but effectively",
|
||||
"Include clear call-to-actions"
|
||||
]
|
||||
},
|
||||
"image_posts": {
|
||||
"optimal_specs": "1200x630 pixels",
|
||||
"best_practices": [
|
||||
"Use high-quality, original images",
|
||||
"Include text overlay for key messages",
|
||||
"Use consistent branding and colors",
|
||||
"Create visually appealing graphics",
|
||||
"Use Facebook's image editing tools"
|
||||
]
|
||||
},
|
||||
"video_posts": {
|
||||
"optimal_length": "15-60 seconds for feed, 2-3 minutes for longer content",
|
||||
"best_practices": [
|
||||
"Upload videos directly to Facebook",
|
||||
"Create engaging thumbnails",
|
||||
"Add captions for accessibility",
|
||||
"Use Facebook's video editing tools",
|
||||
"Create videos that work without sound"
|
||||
]
|
||||
},
|
||||
"carousel_posts": {
|
||||
"optimal_slides": "3-5 slides",
|
||||
"best_practices": [
|
||||
"Tell a story across slides",
|
||||
"Use consistent design elements",
|
||||
"Include clear navigation",
|
||||
"Create slides that work individually",
|
||||
"Use carousels for product showcases"
|
||||
]
|
||||
}
|
||||
},
|
||||
"audience_targeting": {
|
||||
"demographic_targeting": [
|
||||
"Age and gender targeting",
|
||||
"Location-based targeting",
|
||||
"Education and work targeting",
|
||||
"Relationship status targeting",
|
||||
"Language targeting"
|
||||
],
|
||||
"interest_targeting": [
|
||||
"Hobbies and interests",
|
||||
"Brand and product interests",
|
||||
"Entertainment preferences",
|
||||
"Lifestyle and behavior targeting",
|
||||
"Purchase behavior targeting"
|
||||
],
|
||||
"behavioral_targeting": [
|
||||
"Device usage patterns",
|
||||
"Travel behavior",
|
||||
"Purchase behavior",
|
||||
"Digital activity patterns",
|
||||
"Life events targeting"
|
||||
]
|
||||
},
|
||||
"community_building": {
|
||||
"group_management": [
|
||||
"Create and moderate relevant Facebook Groups",
|
||||
"Set clear group rules and guidelines",
|
||||
"Encourage member participation",
|
||||
"Share valuable content in groups",
|
||||
"Use groups for customer support",
|
||||
"Create group events and activities",
|
||||
"Recognize and reward active members",
|
||||
"Use groups for market research"
|
||||
],
|
||||
"event_management": [
|
||||
"Create Facebook Events for promotions",
|
||||
"Use events for product launches",
|
||||
"Host virtual events and webinars",
|
||||
"Create recurring events for consistency",
|
||||
"Use events for community building",
|
||||
"Promote events across all channels",
|
||||
"Follow up with event attendees",
|
||||
"Use events for lead generation"
|
||||
],
|
||||
"live_streaming": [
|
||||
"Host regular Facebook Live sessions",
|
||||
"Use live streaming for Q&A sessions",
|
||||
"Create behind-the-scenes content",
|
||||
"Use live streaming for product demos",
|
||||
"Engage with viewers in real-time",
|
||||
"Use live streaming for announcements",
|
||||
"Create interactive live content",
|
||||
"Use live streaming for customer support"
|
||||
]
|
||||
}
|
||||
}
|
||||
449
backend/services/persona/facebook/facebook_persona_service.py
Normal file
449
backend/services/persona/facebook/facebook_persona_service.py
Normal file
@@ -0,0 +1,449 @@
|
||||
"""
|
||||
Facebook Persona Service
|
||||
Encapsulates Facebook-specific persona generation logic.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional
|
||||
from loguru import logger
|
||||
from datetime import datetime
|
||||
|
||||
from .facebook_persona_schemas import (
|
||||
FacebookPersonaSchema,
|
||||
FacebookPersonaConstraints,
|
||||
FacebookPersonaValidation,
|
||||
FacebookPersonaOptimization
|
||||
)
|
||||
from .facebook_persona_prompts import FacebookPersonaPrompts
|
||||
from services.llm_providers.gemini_provider import gemini_structured_json_response
|
||||
|
||||
|
||||
class FacebookPersonaService:
|
||||
"""Facebook-specific persona generation and optimization service."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(FacebookPersonaService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the Facebook persona service (only once)."""
|
||||
if not self._initialized:
|
||||
self.schemas = FacebookPersonaSchema
|
||||
self.constraints = FacebookPersonaConstraints()
|
||||
self.validation = FacebookPersonaValidation()
|
||||
self.optimization = FacebookPersonaOptimization()
|
||||
self.prompts = FacebookPersonaPrompts()
|
||||
logger.debug("FacebookPersonaService initialized")
|
||||
self._initialized = True
|
||||
|
||||
def generate_facebook_persona(self, core_persona: Dict[str, Any], onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Generate Facebook-specific persona adaptation using optimized chained prompts.
|
||||
|
||||
Args:
|
||||
core_persona: The core persona data
|
||||
onboarding_data: User onboarding data
|
||||
|
||||
Returns:
|
||||
Facebook-optimized persona data
|
||||
"""
|
||||
try:
|
||||
logger.info("Generating Facebook-specific persona with optimized prompts")
|
||||
|
||||
# Build focused Facebook prompt (without core persona JSON)
|
||||
prompt = self.prompts.build_focused_facebook_prompt(onboarding_data)
|
||||
|
||||
# Create system prompt with core persona
|
||||
system_prompt = self.prompts.build_facebook_system_prompt(core_persona)
|
||||
|
||||
# Get Facebook-specific schema
|
||||
schema = self._get_enhanced_facebook_schema()
|
||||
|
||||
# Generate structured response using Gemini with optimized prompts
|
||||
response = gemini_structured_json_response(
|
||||
prompt=prompt,
|
||||
schema=schema,
|
||||
temperature=0.2,
|
||||
max_tokens=4096,
|
||||
system_prompt=system_prompt
|
||||
)
|
||||
|
||||
if not response or "error" in response:
|
||||
logger.error(f"Failed to generate Facebook persona: {response}")
|
||||
return {"error": f"Failed to generate Facebook persona: {response}"}
|
||||
|
||||
# Validate the generated persona
|
||||
validation_results = self.validate_facebook_persona(response)
|
||||
|
||||
# Apply algorithm optimization
|
||||
optimized_persona = self.optimize_for_facebook_algorithm(response)
|
||||
|
||||
# Add validation results to the persona
|
||||
optimized_persona["validation_results"] = validation_results
|
||||
|
||||
logger.info(f"✅ Facebook persona generated successfully with {validation_results['quality_score']:.1f}% quality score")
|
||||
|
||||
return optimized_persona
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating Facebook persona: {str(e)}")
|
||||
return {"error": f"Failed to generate Facebook persona: {str(e)}"}
|
||||
|
||||
def get_facebook_constraints(self) -> Dict[str, Any]:
|
||||
"""Get Facebook-specific platform constraints."""
|
||||
return self.constraints.get_facebook_constraints()
|
||||
|
||||
def validate_facebook_persona(self, persona_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Validate Facebook persona data for completeness and quality.
|
||||
|
||||
Args:
|
||||
persona_data: Facebook persona data to validate
|
||||
|
||||
Returns:
|
||||
Validation results with scores and recommendations
|
||||
"""
|
||||
try:
|
||||
logger.info("Validating Facebook persona data")
|
||||
|
||||
# Use the validation class
|
||||
validation_results = self.validation.validate_facebook_persona(persona_data)
|
||||
|
||||
# Initialize missing fields if they don't exist
|
||||
if "content_format_score" not in validation_results:
|
||||
validation_results["content_format_score"] = 0.0
|
||||
if "audience_targeting_score" not in validation_results:
|
||||
validation_results["audience_targeting_score"] = 0.0
|
||||
if "community_building_score" not in validation_results:
|
||||
validation_results["community_building_score"] = 0.0
|
||||
|
||||
# Add Facebook-specific validation
|
||||
facebook_opt = persona_data.get("facebook_algorithm_optimization", {})
|
||||
if facebook_opt:
|
||||
validation_results["facebook_optimization_score"] = 90.0
|
||||
validation_results["strengths"].append("Facebook algorithm optimization present")
|
||||
else:
|
||||
validation_results["quality_issues"].append("Missing Facebook algorithm optimization")
|
||||
validation_results["recommendations"].append("Add Facebook-specific algorithm strategies")
|
||||
|
||||
# Validate engagement strategies
|
||||
engagement_strategies = persona_data.get("facebook_engagement_strategies", {})
|
||||
if engagement_strategies:
|
||||
validation_results["engagement_strategy_score"] = 85.0
|
||||
validation_results["strengths"].append("Facebook engagement strategies defined")
|
||||
else:
|
||||
validation_results["quality_issues"].append("Missing Facebook engagement strategies")
|
||||
validation_results["recommendations"].append("Define Facebook-specific engagement tactics")
|
||||
|
||||
# Validate content formats
|
||||
content_formats = persona_data.get("facebook_content_formats", {})
|
||||
if content_formats:
|
||||
validation_results["content_format_score"] = 80.0
|
||||
validation_results["strengths"].append("Facebook content formats optimized")
|
||||
else:
|
||||
validation_results["quality_issues"].append("Missing Facebook content format optimization")
|
||||
validation_results["recommendations"].append("Add Facebook-specific content format strategies")
|
||||
|
||||
# Validate audience targeting
|
||||
audience_targeting = persona_data.get("facebook_audience_targeting", {})
|
||||
if audience_targeting:
|
||||
validation_results["audience_targeting_score"] = 75.0
|
||||
validation_results["strengths"].append("Facebook audience targeting strategies present")
|
||||
else:
|
||||
validation_results["quality_issues"].append("Missing Facebook audience targeting")
|
||||
validation_results["recommendations"].append("Add Facebook-specific audience targeting strategies")
|
||||
|
||||
# Validate community building
|
||||
community_building = persona_data.get("facebook_community_building", {})
|
||||
if community_building:
|
||||
validation_results["community_building_score"] = 85.0
|
||||
validation_results["strengths"].append("Facebook community building strategies defined")
|
||||
else:
|
||||
validation_results["quality_issues"].append("Missing Facebook community building strategies")
|
||||
validation_results["recommendations"].append("Add Facebook-specific community building tactics")
|
||||
|
||||
# Recalculate overall quality score
|
||||
validation_results["quality_score"] = (
|
||||
validation_results["completeness_score"] * 0.2 +
|
||||
validation_results["facebook_optimization_score"] * 0.25 +
|
||||
validation_results["engagement_strategy_score"] * 0.2 +
|
||||
validation_results["content_format_score"] * 0.15 +
|
||||
validation_results["audience_targeting_score"] * 0.1 +
|
||||
validation_results["community_building_score"] * 0.1
|
||||
)
|
||||
|
||||
# Add validation timestamp
|
||||
validation_results["validation_timestamp"] = datetime.utcnow().isoformat()
|
||||
|
||||
logger.info(f"Facebook persona validation completed: Quality Score: {validation_results['quality_score']:.1f}%")
|
||||
|
||||
return validation_results
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error validating Facebook persona: {str(e)}")
|
||||
return {
|
||||
"is_valid": False,
|
||||
"quality_score": 0.0,
|
||||
"error": f"Validation failed: {str(e)}"
|
||||
}
|
||||
|
||||
def optimize_for_facebook_algorithm(self, persona_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Optimize Facebook persona data for maximum algorithm performance.
|
||||
|
||||
Args:
|
||||
persona_data: Facebook persona data to optimize
|
||||
|
||||
Returns:
|
||||
Optimized Facebook persona data
|
||||
"""
|
||||
try:
|
||||
logger.info("Optimizing Facebook persona for algorithm performance")
|
||||
|
||||
# Get optimization strategies
|
||||
optimization_strategies = self.optimization.get_facebook_optimization_strategies()
|
||||
|
||||
# Apply algorithm optimization
|
||||
optimized_persona = persona_data.copy()
|
||||
|
||||
# Add comprehensive algorithm optimization
|
||||
optimized_persona["algorithm_optimization"] = {
|
||||
"engagement_optimization": optimization_strategies["algorithm_optimization"]["engagement_optimization"],
|
||||
"content_quality_optimization": optimization_strategies["algorithm_optimization"]["content_quality_optimization"],
|
||||
"timing_optimization": optimization_strategies["algorithm_optimization"]["timing_optimization"],
|
||||
"audience_targeting_optimization": optimization_strategies["algorithm_optimization"]["audience_targeting_optimization"]
|
||||
}
|
||||
|
||||
# Add engagement strategies
|
||||
optimized_persona["engagement_strategies"] = {
|
||||
"community_building": optimization_strategies["engagement_strategies"]["community_building"],
|
||||
"content_engagement": optimization_strategies["engagement_strategies"]["content_engagement"],
|
||||
"conversion_optimization": optimization_strategies["engagement_strategies"]["conversion_optimization"]
|
||||
}
|
||||
|
||||
# Add content format optimization
|
||||
optimized_persona["content_formats"] = optimization_strategies["content_formats"]
|
||||
|
||||
# Add audience targeting optimization
|
||||
optimized_persona["audience_targeting"] = optimization_strategies["audience_targeting"]
|
||||
|
||||
# Add community building optimization
|
||||
optimized_persona["community_building"] = optimization_strategies["community_building"]
|
||||
|
||||
# Add optimization metadata
|
||||
total_strategies = 0
|
||||
for category_name, category_data in optimization_strategies.items():
|
||||
if isinstance(category_data, dict):
|
||||
for strategy_name, strategies in category_data.items():
|
||||
if isinstance(strategies, list):
|
||||
total_strategies += len(strategies)
|
||||
elif isinstance(strategies, dict):
|
||||
# Handle nested dictionaries
|
||||
for sub_strategy_name, sub_strategies in strategies.items():
|
||||
if isinstance(sub_strategies, list):
|
||||
total_strategies += len(sub_strategies)
|
||||
else:
|
||||
total_strategies += 1
|
||||
else:
|
||||
total_strategies += 1
|
||||
elif isinstance(category_data, list):
|
||||
total_strategies += len(category_data)
|
||||
else:
|
||||
total_strategies += 1
|
||||
|
||||
optimized_persona["optimization_metadata"] = {
|
||||
"optimization_applied": True,
|
||||
"optimization_timestamp": datetime.utcnow().isoformat(),
|
||||
"optimization_categories": list(optimization_strategies.keys()),
|
||||
"total_optimization_strategies": total_strategies
|
||||
}
|
||||
|
||||
logger.info("✅ Facebook persona algorithm optimization completed successfully")
|
||||
|
||||
return optimized_persona
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error optimizing Facebook persona: {str(e)}")
|
||||
return persona_data # Return original data if optimization fails
|
||||
|
||||
def _get_enhanced_facebook_schema(self) -> Dict[str, Any]:
|
||||
"""Get enhanced Facebook persona schema for Gemini structured response with improved JSON parsing reliability."""
|
||||
return {
|
||||
"type": "object",
|
||||
"description": "Facebook-optimized persona data structure for community engagement and algorithm optimization",
|
||||
"properties": {
|
||||
"persona_name": {
|
||||
"type": "string",
|
||||
"description": "Name of the Facebook-optimized persona (e.g., 'Community Builder', 'Social Connector')",
|
||||
"minLength": 3,
|
||||
"maxLength": 50
|
||||
},
|
||||
"archetype": {
|
||||
"type": "string",
|
||||
"description": "Persona archetype for Facebook (e.g., 'The Community Catalyst', 'The Social Storyteller')",
|
||||
"minLength": 5,
|
||||
"maxLength": 50
|
||||
},
|
||||
"core_belief": {
|
||||
"type": "string",
|
||||
"description": "Core belief driving the Facebook persona (e.g., 'Building authentic connections through shared experiences')",
|
||||
"minLength": 10,
|
||||
"maxLength": 200
|
||||
},
|
||||
"facebook_algorithm_optimization": {
|
||||
"type": "object",
|
||||
"description": "Facebook algorithm optimization strategies",
|
||||
"properties": {
|
||||
"engagement_optimization": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Strategies for optimizing Facebook engagement (3-8 strategies)",
|
||||
"minItems": 3,
|
||||
"maxItems": 8
|
||||
},
|
||||
"content_quality_optimization": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Strategies for optimizing content quality on Facebook (3-8 strategies)",
|
||||
"minItems": 3,
|
||||
"maxItems": 8
|
||||
},
|
||||
"timing_optimization": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Strategies for optimizing posting timing on Facebook (3-8 strategies)",
|
||||
"minItems": 3,
|
||||
"maxItems": 8
|
||||
},
|
||||
"audience_targeting_optimization": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Strategies for optimizing audience targeting on Facebook (3-8 strategies)",
|
||||
"minItems": 3,
|
||||
"maxItems": 8
|
||||
}
|
||||
}
|
||||
},
|
||||
"facebook_engagement_strategies": {
|
||||
"type": "object",
|
||||
"description": "Facebook-specific engagement strategies",
|
||||
"properties": {
|
||||
"community_building": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Community building strategies for Facebook"
|
||||
},
|
||||
"content_engagement": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Content engagement strategies for Facebook"
|
||||
},
|
||||
"conversion_optimization": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Conversion optimization strategies for Facebook"
|
||||
}
|
||||
}
|
||||
},
|
||||
"facebook_content_formats": {
|
||||
"type": "object",
|
||||
"description": "Facebook content format optimizations",
|
||||
"properties": {
|
||||
"text_posts": {
|
||||
"type": "object",
|
||||
"description": "Text post optimization for Facebook",
|
||||
"properties": {
|
||||
"optimal_length": {"type": "string"},
|
||||
"structure_guidelines": {"type": "array", "items": {"type": "string"}},
|
||||
"hook_strategies": {"type": "array", "items": {"type": "string"}}
|
||||
}
|
||||
},
|
||||
"image_posts": {
|
||||
"type": "object",
|
||||
"description": "Image post optimization for Facebook",
|
||||
"properties": {
|
||||
"image_guidelines": {"type": "array", "items": {"type": "string"}},
|
||||
"caption_strategies": {"type": "array", "items": {"type": "string"}}
|
||||
}
|
||||
},
|
||||
"video_posts": {
|
||||
"type": "object",
|
||||
"description": "Video post optimization for Facebook",
|
||||
"properties": {
|
||||
"video_length_guidelines": {"type": "array", "items": {"type": "string"}},
|
||||
"engagement_hooks": {"type": "array", "items": {"type": "string"}}
|
||||
}
|
||||
},
|
||||
"carousel_posts": {
|
||||
"type": "object",
|
||||
"description": "Carousel post optimization for Facebook",
|
||||
"properties": {
|
||||
"slide_structure": {"type": "array", "items": {"type": "string"}},
|
||||
"storytelling_flow": {"type": "array", "items": {"type": "string"}}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"facebook_audience_targeting": {
|
||||
"type": "object",
|
||||
"description": "Facebook audience targeting strategies",
|
||||
"properties": {
|
||||
"demographic_targeting": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Demographic targeting strategies for Facebook"
|
||||
},
|
||||
"interest_targeting": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Interest targeting strategies for Facebook"
|
||||
},
|
||||
"behavioral_targeting": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Behavioral targeting strategies for Facebook"
|
||||
}
|
||||
}
|
||||
},
|
||||
"facebook_community_building": {
|
||||
"type": "object",
|
||||
"description": "Facebook community building strategies",
|
||||
"properties": {
|
||||
"group_management": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Facebook Group management strategies"
|
||||
},
|
||||
"event_management": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Facebook Event management strategies"
|
||||
},
|
||||
"live_streaming": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Facebook Live streaming strategies"
|
||||
}
|
||||
}
|
||||
},
|
||||
"confidence_score": {
|
||||
"type": "number",
|
||||
"description": "Confidence score for the Facebook persona (0-100)",
|
||||
"minimum": 0,
|
||||
"maximum": 100
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"persona_name",
|
||||
"archetype",
|
||||
"core_belief",
|
||||
"facebook_algorithm_optimization",
|
||||
"facebook_engagement_strategies",
|
||||
"confidence_score"
|
||||
],
|
||||
"additionalProperties": False
|
||||
}
|
||||
10
backend/services/persona/linkedin/__init__.py
Normal file
10
backend/services/persona/linkedin/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
||||
"""
|
||||
LinkedIn Persona Services
|
||||
Contains LinkedIn-specific persona generation and optimization services.
|
||||
"""
|
||||
|
||||
from .linkedin_persona_service import LinkedInPersonaService
|
||||
from .linkedin_persona_prompts import LinkedInPersonaPrompts
|
||||
from .linkedin_persona_schemas import LinkedInPersonaSchemas
|
||||
|
||||
__all__ = ['LinkedInPersonaService', 'LinkedInPersonaPrompts', 'LinkedInPersonaSchemas']
|
||||
319
backend/services/persona/linkedin/linkedin_persona_prompts.py
Normal file
319
backend/services/persona/linkedin/linkedin_persona_prompts.py
Normal file
@@ -0,0 +1,319 @@
|
||||
"""
|
||||
LinkedIn Persona Prompts
|
||||
Contains LinkedIn-specific prompt generation for persona analysis.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any
|
||||
import json
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class LinkedInPersonaPrompts:
|
||||
"""Handles LinkedIn-specific persona prompt generation."""
|
||||
|
||||
@staticmethod
|
||||
def build_enhanced_linkedin_prompt(core_persona: Dict[str, Any], onboarding_data: Dict[str, Any]) -> str:
|
||||
"""Build enhanced LinkedIn-specific persona prompt with professional optimization."""
|
||||
|
||||
# Extract comprehensive professional context
|
||||
professional_context = LinkedInPersonaPrompts._extract_professional_context(onboarding_data)
|
||||
|
||||
website_analysis = onboarding_data.get("website_analysis", {}) or {}
|
||||
target_audience = website_analysis.get("target_audience", {})
|
||||
industry_focus = professional_context.get("industry_focus", "general")
|
||||
expertise_level = professional_context.get("expertise_level", "intermediate")
|
||||
|
||||
prompt = f"""
|
||||
LINKEDIN PROFESSIONAL PERSONA OPTIMIZATION TASK: Create a comprehensive LinkedIn-optimized writing persona for professional networking and thought leadership.
|
||||
|
||||
CORE PERSONA FOUNDATION:
|
||||
{json.dumps(core_persona, indent=2)}
|
||||
|
||||
PROFESSIONAL CONTEXT:
|
||||
- Industry: {industry_focus}
|
||||
- Expertise Level: {expertise_level}
|
||||
- Company Size: {professional_context.get('company_size', 'Not specified')}
|
||||
- Business Model: {professional_context.get('business_model', 'Not specified')}
|
||||
- Professional Role: {professional_context.get('professional_role', 'Not specified')}
|
||||
- Demographics: {professional_context.get('target_demographics', [])}
|
||||
- Psychographic: {professional_context.get('psychographic_profile', 'Not specified')}
|
||||
|
||||
LINKEDIN PLATFORM SPECIFICATIONS:
|
||||
- Character Limit: 3,000 characters
|
||||
- Optimal Post Length: 150-300 words for maximum engagement
|
||||
- Professional Network: B2B focused, career-oriented audience
|
||||
- Algorithm Priority: Engagement, relevance, professional value
|
||||
- Content Types: Posts, Articles, Polls, Videos, Carousels, Events
|
||||
- Hashtag Limit: 3-5 hashtags for optimal reach
|
||||
- Link Strategy: Place external links in first comment for algorithm optimization
|
||||
|
||||
LINKEDIN PROFESSIONAL OPTIMIZATION REQUIREMENTS:
|
||||
|
||||
1. PROFESSIONAL TONE & VOICE:
|
||||
- Maintain authoritative yet approachable professional tone
|
||||
- Use industry-specific terminology appropriately
|
||||
- Balance expertise with accessibility for {expertise_level} audience
|
||||
- Incorporate thought leadership elements
|
||||
- Include professional storytelling and case studies
|
||||
|
||||
2. CONTENT STRATEGY FOR {industry_focus.upper()}:
|
||||
- Industry insights for {expertise_level} professionals
|
||||
- Professional development content for {professional_context.get('target_demographics', [])}
|
||||
- Business strategy discussions for {professional_context.get('business_model', 'general business')}
|
||||
- Networking focus for {professional_context.get('company_size', 'all company sizes')}
|
||||
- Thought leadership positioning as {professional_context.get('professional_role', 'professional')}
|
||||
|
||||
3. ENGAGEMENT OPTIMIZATION:
|
||||
- Professional question frameworks for discussion
|
||||
- Industry-relevant polling strategies
|
||||
- Professional networking call-to-actions
|
||||
- Thought leadership positioning
|
||||
- Community building through professional value
|
||||
|
||||
4. LINKEDIN-SPECIFIC FEATURES:
|
||||
- Native video optimization for professional content
|
||||
- LinkedIn Articles for long-form thought leadership
|
||||
- LinkedIn Polls for industry insights and engagement
|
||||
- LinkedIn Events for professional networking
|
||||
- LinkedIn Carousels for educational content
|
||||
- LinkedIn Live for professional discussions
|
||||
|
||||
5. PROFESSIONAL NETWORKING ELEMENTS:
|
||||
- Industry-specific hashtag strategy
|
||||
- Professional mention and tagging etiquette
|
||||
- Thought leadership positioning
|
||||
- Professional relationship building
|
||||
- Career advancement focus
|
||||
|
||||
6. CONTENT FORMAT OPTIMIZATION:
|
||||
- Hook strategies for professional feed
|
||||
- "See More" optimization for longer posts
|
||||
- Professional call-to-action frameworks
|
||||
- Industry-specific content structures
|
||||
- Professional storytelling techniques
|
||||
|
||||
7. LINKEDIN ALGORITHM OPTIMIZATION:
|
||||
- Professional engagement patterns
|
||||
- Industry-relevant content timing
|
||||
- Professional network interaction strategies
|
||||
- Thought leadership content performance
|
||||
- Professional community building
|
||||
|
||||
8. INDUSTRY-SPECIFIC ADAPTATIONS FOR {industry_focus.upper()}:
|
||||
- Terminology appropriate for {expertise_level} level
|
||||
- Professional development for {professional_context.get('target_demographics', [])}
|
||||
- Trend discussions for {professional_context.get('business_model', 'general business')}
|
||||
- Networking strategies for {professional_context.get('company_size', 'all company sizes')}
|
||||
- Thought leadership as {professional_context.get('professional_role', 'professional')}
|
||||
- Content addressing {professional_context.get('psychographic_profile', 'professional needs')}
|
||||
- Business insights for {professional_context.get('conversion_focus', 'business growth')}
|
||||
|
||||
PROFESSIONAL EXCELLENCE STANDARDS:
|
||||
- Maintain high professional standards
|
||||
- Focus on value-driven content
|
||||
- Emphasize thought leadership and expertise
|
||||
- Build professional credibility and authority
|
||||
- Foster meaningful professional relationships
|
||||
- Provide actionable business insights
|
||||
- Support professional development and growth
|
||||
|
||||
Generate a comprehensive LinkedIn-optimized persona that positions the user as a thought leader in {industry_focus} while maintaining professional excellence and maximizing LinkedIn's professional networking potential.
|
||||
"""
|
||||
|
||||
return prompt
|
||||
|
||||
@staticmethod
|
||||
def get_linkedin_platform_constraints() -> Dict[str, Any]:
|
||||
"""Get LinkedIn-specific platform constraints and best practices."""
|
||||
return {
|
||||
"character_limit": 3000,
|
||||
"optimal_length": "150-300 words",
|
||||
"professional_tone": True,
|
||||
"hashtag_limit": 5,
|
||||
"rich_media": True,
|
||||
"long_form": True,
|
||||
"thought_leadership": True,
|
||||
"networking_focus": True,
|
||||
"career_development": True,
|
||||
"industry_insights": True,
|
||||
"professional_storytelling": True,
|
||||
"b2b_optimized": True,
|
||||
"algorithm_engagement": True,
|
||||
"native_video": True,
|
||||
"linkedin_articles": True,
|
||||
"linkedin_polls": True,
|
||||
"linkedin_events": True,
|
||||
"linkedin_carousels": True,
|
||||
"linkedin_live": True
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _extract_professional_context(onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Extract comprehensive professional context from onboarding data."""
|
||||
|
||||
professional_context = {
|
||||
"industry_focus": "general",
|
||||
"expertise_level": "intermediate",
|
||||
"company_size": "Not specified",
|
||||
"business_model": "Not specified",
|
||||
"professional_role": "Not specified",
|
||||
"geographic_focus": "global",
|
||||
"target_demographics": [],
|
||||
"psychographic_profile": "",
|
||||
"content_purpose": "",
|
||||
"conversion_focus": "",
|
||||
"research_depth": "",
|
||||
"content_types": []
|
||||
}
|
||||
|
||||
# Extract from website analysis
|
||||
website_analysis = onboarding_data.get("website_analysis", {}) or {}
|
||||
|
||||
# Target audience information
|
||||
target_audience = website_analysis.get("target_audience", {})
|
||||
if target_audience:
|
||||
professional_context["industry_focus"] = target_audience.get("industry_focus", "general")
|
||||
professional_context["expertise_level"] = target_audience.get("expertise_level", "intermediate")
|
||||
professional_context["geographic_focus"] = target_audience.get("geographic_focus", "global")
|
||||
professional_context["target_demographics"] = target_audience.get("demographics", [])
|
||||
professional_context["psychographic_profile"] = target_audience.get("psychographic_profile", "")
|
||||
|
||||
# Content type and business context
|
||||
content_type = website_analysis.get("content_type", {})
|
||||
if content_type:
|
||||
professional_context["content_purpose"] = content_type.get("purpose", "")
|
||||
professional_context["conversion_focus"] = content_type.get("conversion_focus", "")
|
||||
|
||||
# Company and business information from crawl results
|
||||
crawl_result = website_analysis.get("crawl_result", {})
|
||||
if crawl_result:
|
||||
domain_info = crawl_result.get("domain_info", {})
|
||||
if domain_info:
|
||||
professional_context["company_size"] = domain_info.get("company_size", "Not specified")
|
||||
professional_context["business_model"] = domain_info.get("business_model", "Not specified")
|
||||
|
||||
brand_info = crawl_result.get("brand_info", {})
|
||||
if brand_info:
|
||||
professional_context["professional_role"] = brand_info.get("professional_role", "Not specified")
|
||||
|
||||
# Research preferences
|
||||
research_prefs = onboarding_data.get("research_preferences", {})
|
||||
if research_prefs:
|
||||
professional_context["research_depth"] = research_prefs.get("research_depth", "")
|
||||
professional_context["content_types"] = research_prefs.get("content_types", [])
|
||||
|
||||
# Enhanced analysis data
|
||||
enhanced_analysis = onboarding_data.get("enhanced_analysis", {})
|
||||
if enhanced_analysis:
|
||||
audience_intel = enhanced_analysis.get("audience_intelligence", {})
|
||||
if audience_intel:
|
||||
# Override with more detailed information if available
|
||||
if audience_intel.get("industry_focus"):
|
||||
professional_context["industry_focus"] = audience_intel["industry_focus"]
|
||||
if audience_intel.get("expertise_level"):
|
||||
professional_context["expertise_level"] = audience_intel["expertise_level"]
|
||||
if audience_intel.get("psychographic_profile"):
|
||||
professional_context["psychographic_profile"] = audience_intel["psychographic_profile"]
|
||||
|
||||
brand_voice = enhanced_analysis.get("brand_voice_analysis", {})
|
||||
if brand_voice:
|
||||
if brand_voice.get("primary_content_type"):
|
||||
professional_context["content_purpose"] = brand_voice["primary_content_type"]
|
||||
if brand_voice.get("conversion_focus"):
|
||||
professional_context["conversion_focus"] = brand_voice["conversion_focus"]
|
||||
|
||||
return professional_context
|
||||
|
||||
@staticmethod
|
||||
def build_linkedin_system_prompt(core_persona: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Build system prompt with core persona for LinkedIn generation.
|
||||
This moves the core persona to system prompt to free up context window.
|
||||
"""
|
||||
import json
|
||||
|
||||
return f"""You are an expert LinkedIn content strategist and professional networking specialist.
|
||||
|
||||
CORE PERSONA FOUNDATION:
|
||||
{json.dumps(core_persona, indent=2)}
|
||||
|
||||
Your task is to create LinkedIn-optimized persona adaptations that maintain the core persona's identity while optimizing for professional networking, thought leadership, and B2B engagement on LinkedIn.
|
||||
|
||||
Focus on:
|
||||
- Professional tone and authority
|
||||
- Industry-specific optimization
|
||||
- LinkedIn algorithm best practices
|
||||
- B2B engagement strategies
|
||||
- Professional networking optimization"""
|
||||
|
||||
@staticmethod
|
||||
def build_focused_linkedin_prompt(onboarding_data: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Build focused LinkedIn prompt without core persona JSON to optimize context usage.
|
||||
"""
|
||||
# Extract professional context
|
||||
professional_context = LinkedInPersonaPrompts._extract_professional_context(onboarding_data)
|
||||
|
||||
industry_focus = professional_context.get("industry_focus", "general")
|
||||
expertise_level = professional_context.get("expertise_level", "intermediate")
|
||||
|
||||
return f"""LINKEDIN PROFESSIONAL OPTIMIZATION TASK: Create LinkedIn-specific adaptations for the core persona.
|
||||
|
||||
PROFESSIONAL CONTEXT:
|
||||
- Industry: {industry_focus}
|
||||
- Expertise Level: {expertise_level}
|
||||
- Company Size: {professional_context.get('company_size', 'Not specified')}
|
||||
- Business Model: {professional_context.get('business_model', 'Not specified')}
|
||||
- Professional Role: {professional_context.get('professional_role', 'Not specified')}
|
||||
- Demographics: {professional_context.get('target_demographics', [])}
|
||||
- Psychographic: {professional_context.get('psychographic_profile', 'Not specified')}
|
||||
|
||||
LINKEDIN PLATFORM SPECIFICATIONS:
|
||||
- Character Limit: 3,000 characters
|
||||
- Optimal Post Length: 150-300 words for maximum engagement
|
||||
- Professional Network: B2B focused, career-oriented audience
|
||||
- Algorithm Priority: Engagement, relevance, professional value
|
||||
- Content Types: Posts, Articles, Polls, Videos, Carousels, Events
|
||||
- Hashtag Limit: 3-5 hashtags for optimal reach
|
||||
- Link Strategy: Place external links in first comment for algorithm optimization
|
||||
|
||||
LINKEDIN OPTIMIZATION REQUIREMENTS:
|
||||
|
||||
1. PROFESSIONAL TONE & VOICE:
|
||||
- Maintain authoritative yet approachable professional tone
|
||||
- Use industry-specific terminology appropriately
|
||||
- Balance expertise with accessibility for {expertise_level} audience
|
||||
- Incorporate thought leadership elements
|
||||
- Include professional storytelling and case studies
|
||||
|
||||
2. CONTENT STRATEGY FOR {industry_focus.upper()}:
|
||||
- Industry insights for {expertise_level} professionals
|
||||
- Professional development content for {professional_context.get('target_demographics', [])}
|
||||
- Business strategy discussions for {professional_context.get('business_model', 'general business')}
|
||||
- Networking focus for {professional_context.get('company_size', 'all company sizes')}
|
||||
- Thought leadership positioning as {professional_context.get('professional_role', 'professional')}
|
||||
|
||||
3. LINKEDIN-SPECIFIC ADAPTATIONS:
|
||||
- Optimize sentence structure for professional readability
|
||||
- Create platform-specific vocabulary and terminology
|
||||
- Define engagement patterns for B2B audience
|
||||
- Establish professional networking strategies
|
||||
- Include LinkedIn feature optimization (Articles, Polls, Events, etc.)
|
||||
|
||||
4. ALGORITHM OPTIMIZATION:
|
||||
- Engagement patterns for professional audience
|
||||
- Content timing for maximum reach
|
||||
- Professional value metrics
|
||||
- Network interaction strategies
|
||||
|
||||
5. PROFESSIONAL CONTEXT OPTIMIZATION:
|
||||
- Industry-specific positioning
|
||||
- Expertise level adaptation
|
||||
- Company size considerations
|
||||
- Business model alignment
|
||||
- Professional role authority
|
||||
- Demographic targeting
|
||||
- Psychographic engagement
|
||||
- Conversion optimization
|
||||
|
||||
Generate a comprehensive LinkedIn-optimized persona that maintains the core persona's identity while maximizing professional networking and thought leadership potential on LinkedIn."""
|
||||
115
backend/services/persona/linkedin/linkedin_persona_schemas.py
Normal file
115
backend/services/persona/linkedin/linkedin_persona_schemas.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
LinkedIn Persona Schemas
|
||||
Contains LinkedIn-specific JSON schemas for persona generation.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any
|
||||
|
||||
|
||||
class LinkedInPersonaSchemas:
|
||||
"""Handles LinkedIn-specific persona schema definitions."""
|
||||
|
||||
@staticmethod
|
||||
def get_linkedin_platform_schema() -> Dict[str, Any]:
|
||||
"""Get LinkedIn-specific platform persona schema."""
|
||||
return {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"platform_type": {"type": "string"},
|
||||
"sentence_metrics": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"max_sentence_length": {"type": "number"},
|
||||
"optimal_sentence_length": {"type": "number"},
|
||||
"sentence_variety": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"lexical_adaptations": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"platform_specific_words": {"type": "array", "items": {"type": "string"}},
|
||||
"hashtag_strategy": {"type": "string"},
|
||||
"emoji_usage": {"type": "string"},
|
||||
"mention_strategy": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"content_format_rules": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"character_limit": {"type": "number"},
|
||||
"paragraph_structure": {"type": "string"},
|
||||
"call_to_action_style": {"type": "string"},
|
||||
"link_placement": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"engagement_patterns": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"posting_frequency": {"type": "string"},
|
||||
"optimal_posting_times": {"type": "array", "items": {"type": "string"}},
|
||||
"engagement_tactics": {"type": "array", "items": {"type": "string"}},
|
||||
"community_interaction": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"platform_best_practices": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"required": ["platform_type", "sentence_metrics", "content_format_rules", "engagement_patterns"]
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def get_enhanced_linkedin_schema() -> Dict[str, Any]:
|
||||
"""Get enhanced LinkedIn schema with additional professional fields."""
|
||||
base_schema = LinkedInPersonaSchemas.get_linkedin_platform_schema()
|
||||
|
||||
# Add LinkedIn-specific professional fields
|
||||
base_schema["properties"]["professional_networking"] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"thought_leadership_positioning": {"type": "string"},
|
||||
"industry_authority_building": {"type": "string"},
|
||||
"professional_relationship_strategies": {"type": "array", "items": {"type": "string"}},
|
||||
"career_advancement_focus": {"type": "string"}
|
||||
}
|
||||
}
|
||||
|
||||
base_schema["properties"]["linkedin_features"] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"articles_strategy": {"type": "string"},
|
||||
"polls_optimization": {"type": "string"},
|
||||
"events_networking": {"type": "string"},
|
||||
"carousels_education": {"type": "string"},
|
||||
"live_discussions": {"type": "string"},
|
||||
"native_video": {"type": "string"}
|
||||
}
|
||||
}
|
||||
|
||||
base_schema["properties"]["algorithm_optimization"] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"engagement_patterns": {"type": "array", "items": {"type": "string"}},
|
||||
"content_timing": {"type": "array", "items": {"type": "string"}},
|
||||
"professional_value_metrics": {"type": "array", "items": {"type": "string"}},
|
||||
"network_interaction_strategies": {"type": "array", "items": {"type": "string"}}
|
||||
}
|
||||
}
|
||||
|
||||
# Add professional context optimization
|
||||
base_schema["properties"]["professional_context_optimization"] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"industry_specific_positioning": {"type": "string"},
|
||||
"expertise_level_adaptation": {"type": "string"},
|
||||
"company_size_considerations": {"type": "string"},
|
||||
"business_model_alignment": {"type": "string"},
|
||||
"professional_role_authority": {"type": "string"},
|
||||
"demographic_targeting": {"type": "array", "items": {"type": "string"}},
|
||||
"psychographic_engagement": {"type": "string"},
|
||||
"conversion_optimization": {"type": "string"}
|
||||
}
|
||||
}
|
||||
|
||||
return base_schema
|
||||
550
backend/services/persona/linkedin/linkedin_persona_service.py
Normal file
550
backend/services/persona/linkedin/linkedin_persona_service.py
Normal file
@@ -0,0 +1,550 @@
|
||||
"""
|
||||
LinkedIn Persona Service
|
||||
Handles LinkedIn-specific persona generation and optimization.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional
|
||||
from loguru import logger
|
||||
|
||||
from services.llm_providers.gemini_provider import gemini_structured_json_response
|
||||
from .linkedin_persona_prompts import LinkedInPersonaPrompts
|
||||
from .linkedin_persona_schemas import LinkedInPersonaSchemas
|
||||
|
||||
|
||||
class LinkedInPersonaService:
|
||||
"""Service for generating LinkedIn-specific persona adaptations."""
|
||||
|
||||
_instance = None
|
||||
_initialized = False
|
||||
|
||||
def __new__(cls):
|
||||
"""Implement singleton pattern to prevent multiple initializations."""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(LinkedInPersonaService, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the LinkedIn persona service (only once)."""
|
||||
if not self._initialized:
|
||||
self.prompts = LinkedInPersonaPrompts()
|
||||
self.schemas = LinkedInPersonaSchemas()
|
||||
logger.debug("LinkedInPersonaService initialized")
|
||||
self._initialized = True
|
||||
|
||||
def generate_linkedin_persona(self, core_persona: Dict[str, Any], onboarding_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Generate LinkedIn-specific persona adaptation using optimized chained prompts.
|
||||
|
||||
Args:
|
||||
core_persona: The core writing persona
|
||||
onboarding_data: User's onboarding data
|
||||
|
||||
Returns:
|
||||
LinkedIn-optimized persona data
|
||||
"""
|
||||
try:
|
||||
logger.info("Generating LinkedIn-specific persona with optimized prompts")
|
||||
|
||||
# Build focused LinkedIn prompt (without core persona JSON)
|
||||
prompt = self.prompts.build_focused_linkedin_prompt(onboarding_data)
|
||||
|
||||
# Create system prompt with core persona
|
||||
system_prompt = self.prompts.build_linkedin_system_prompt(core_persona)
|
||||
|
||||
# Get LinkedIn-specific schema
|
||||
schema = self.schemas.get_enhanced_linkedin_schema()
|
||||
|
||||
# Generate structured response using Gemini with optimized prompts
|
||||
response = gemini_structured_json_response(
|
||||
prompt=prompt,
|
||||
schema=schema,
|
||||
temperature=0.2,
|
||||
max_tokens=4096,
|
||||
system_prompt=system_prompt
|
||||
)
|
||||
|
||||
if "error" in response:
|
||||
logger.error(f"LinkedIn persona generation failed: {response['error']}")
|
||||
return {"error": f"LinkedIn persona generation failed: {response['error']}"}
|
||||
|
||||
# Validate the generated persona
|
||||
validation_results = self.validate_linkedin_persona(response)
|
||||
logger.info(f"LinkedIn persona validation: Quality Score: {validation_results['quality_score']:.1f}%, Valid: {validation_results['is_valid']}")
|
||||
|
||||
# Add validation results to persona data
|
||||
response["validation_results"] = validation_results
|
||||
|
||||
# Apply comprehensive algorithm optimization
|
||||
optimized_response = self.optimize_for_linkedin_algorithm(response)
|
||||
logger.info("✅ LinkedIn persona algorithm optimization applied")
|
||||
|
||||
logger.info("✅ LinkedIn persona generated and optimized successfully")
|
||||
return optimized_response
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating LinkedIn persona: {str(e)}")
|
||||
return {"error": f"Failed to generate LinkedIn persona: {str(e)}"}
|
||||
|
||||
def get_linkedin_constraints(self) -> Dict[str, Any]:
|
||||
"""Get LinkedIn platform constraints."""
|
||||
return self.prompts.get_linkedin_platform_constraints()
|
||||
|
||||
def validate_linkedin_persona(self, persona_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Comprehensive validation of LinkedIn persona data for completeness and quality.
|
||||
|
||||
Args:
|
||||
persona_data: LinkedIn persona data to validate
|
||||
|
||||
Returns:
|
||||
Detailed validation results with quality metrics and recommendations
|
||||
"""
|
||||
try:
|
||||
validation_results = {
|
||||
"is_valid": True,
|
||||
"quality_score": 0.0,
|
||||
"completeness_score": 0.0,
|
||||
"professional_context_score": 0.0,
|
||||
"linkedin_optimization_score": 0.0,
|
||||
"missing_fields": [],
|
||||
"incomplete_fields": [],
|
||||
"recommendations": [],
|
||||
"quality_issues": [],
|
||||
"strengths": [],
|
||||
"validation_details": {}
|
||||
}
|
||||
|
||||
# 1. CORE FIELDS VALIDATION (30% of score)
|
||||
core_fields_score = self._validate_core_fields(persona_data, validation_results)
|
||||
|
||||
# 2. LINKEDIN-SPECIFIC FIELDS VALIDATION (40% of score)
|
||||
linkedin_fields_score = self._validate_linkedin_specific_fields(persona_data, validation_results)
|
||||
|
||||
# 3. PROFESSIONAL CONTEXT VALIDATION (20% of score)
|
||||
professional_context_score = self._validate_professional_context(persona_data, validation_results)
|
||||
|
||||
# 4. CONTENT QUALITY VALIDATION (10% of score)
|
||||
content_quality_score = self._validate_content_quality(persona_data, validation_results)
|
||||
|
||||
# Calculate overall quality score
|
||||
validation_results["quality_score"] = (
|
||||
core_fields_score * 0.3 +
|
||||
linkedin_fields_score * 0.4 +
|
||||
professional_context_score * 0.2 +
|
||||
content_quality_score * 0.1
|
||||
)
|
||||
|
||||
# Set completeness score
|
||||
validation_results["completeness_score"] = core_fields_score
|
||||
validation_results["professional_context_score"] = professional_context_score
|
||||
validation_results["linkedin_optimization_score"] = linkedin_fields_score
|
||||
|
||||
# Determine if persona is valid
|
||||
validation_results["is_valid"] = (
|
||||
validation_results["quality_score"] >= 70.0 and
|
||||
len(validation_results["missing_fields"]) == 0
|
||||
)
|
||||
|
||||
# Add quality assessment
|
||||
self._assess_persona_quality(validation_results)
|
||||
|
||||
return validation_results
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error validating LinkedIn persona: {str(e)}")
|
||||
return {
|
||||
"is_valid": False,
|
||||
"quality_score": 0.0,
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
def _validate_core_fields(self, persona_data: Dict[str, Any], validation_results: Dict[str, Any]) -> float:
|
||||
"""Validate core LinkedIn persona fields."""
|
||||
core_fields = {
|
||||
"platform_type": {"required": True, "type": str},
|
||||
"sentence_metrics": {"required": True, "type": dict, "subfields": ["max_sentence_length", "optimal_sentence_length"]},
|
||||
"lexical_adaptations": {"required": True, "type": dict, "subfields": ["platform_specific_words", "hashtag_strategy"]},
|
||||
"content_format_rules": {"required": True, "type": dict, "subfields": ["character_limit", "paragraph_structure"]},
|
||||
"engagement_patterns": {"required": True, "type": dict, "subfields": ["posting_frequency", "optimal_posting_times"]},
|
||||
"platform_best_practices": {"required": True, "type": list}
|
||||
}
|
||||
|
||||
score = 0.0
|
||||
total_fields = len(core_fields)
|
||||
|
||||
for field, config in core_fields.items():
|
||||
if field not in persona_data:
|
||||
validation_results["missing_fields"].append(field)
|
||||
continue
|
||||
|
||||
field_data = persona_data[field]
|
||||
field_score = 0.0
|
||||
|
||||
# Check field type
|
||||
if isinstance(field_data, config["type"]):
|
||||
field_score += 0.5
|
||||
else:
|
||||
validation_results["quality_issues"].append(f"{field} has incorrect type: expected {config['type'].__name__}")
|
||||
|
||||
# Check subfields if specified
|
||||
if "subfields" in config and isinstance(field_data, dict):
|
||||
subfield_score = 0.0
|
||||
for subfield in config["subfields"]:
|
||||
if subfield in field_data and field_data[subfield]:
|
||||
subfield_score += 1.0
|
||||
else:
|
||||
validation_results["incomplete_fields"].append(f"{field}.{subfield}")
|
||||
|
||||
if config["subfields"]:
|
||||
field_score += (subfield_score / len(config["subfields"])) * 0.5
|
||||
|
||||
score += field_score
|
||||
validation_results["validation_details"][field] = {
|
||||
"present": True,
|
||||
"type_correct": isinstance(field_data, config["type"]),
|
||||
"completeness": field_score
|
||||
}
|
||||
|
||||
return (score / total_fields) * 100
|
||||
|
||||
def _validate_linkedin_specific_fields(self, persona_data: Dict[str, Any], validation_results: Dict[str, Any]) -> float:
|
||||
"""Validate LinkedIn-specific optimization fields."""
|
||||
linkedin_fields = {
|
||||
"professional_networking": {
|
||||
"required": True,
|
||||
"subfields": ["thought_leadership_positioning", "industry_authority_building", "professional_relationship_strategies"]
|
||||
},
|
||||
"linkedin_features": {
|
||||
"required": True,
|
||||
"subfields": ["articles_strategy", "polls_optimization", "events_networking", "carousels_education"]
|
||||
},
|
||||
"algorithm_optimization": {
|
||||
"required": True,
|
||||
"subfields": ["engagement_patterns", "content_timing", "professional_value_metrics"]
|
||||
},
|
||||
"professional_context_optimization": {
|
||||
"required": True,
|
||||
"subfields": ["industry_specific_positioning", "expertise_level_adaptation", "demographic_targeting"]
|
||||
}
|
||||
}
|
||||
|
||||
score = 0.0
|
||||
total_fields = len(linkedin_fields)
|
||||
|
||||
for field, config in linkedin_fields.items():
|
||||
if field not in persona_data:
|
||||
validation_results["missing_fields"].append(field)
|
||||
validation_results["recommendations"].append(f"Add {field} for enhanced LinkedIn optimization")
|
||||
continue
|
||||
|
||||
field_data = persona_data[field]
|
||||
if not isinstance(field_data, dict):
|
||||
validation_results["quality_issues"].append(f"{field} should be a dictionary")
|
||||
continue
|
||||
|
||||
field_score = 0.0
|
||||
for subfield in config["subfields"]:
|
||||
if subfield in field_data and field_data[subfield]:
|
||||
field_score += 1.0
|
||||
else:
|
||||
validation_results["incomplete_fields"].append(f"{field}.{subfield}")
|
||||
|
||||
field_score = (field_score / len(config["subfields"])) * 100
|
||||
score += field_score
|
||||
|
||||
validation_results["validation_details"][field] = {
|
||||
"present": True,
|
||||
"completeness": field_score,
|
||||
"subfields_present": len([sf for sf in config["subfields"] if sf in field_data and field_data[sf]])
|
||||
}
|
||||
|
||||
return score / total_fields
|
||||
|
||||
def _validate_professional_context(self, persona_data: Dict[str, Any], validation_results: Dict[str, Any]) -> float:
|
||||
"""Validate professional context optimization."""
|
||||
if "professional_context_optimization" not in persona_data:
|
||||
validation_results["missing_fields"].append("professional_context_optimization")
|
||||
return 0.0
|
||||
|
||||
context_data = persona_data["professional_context_optimization"]
|
||||
if not isinstance(context_data, dict):
|
||||
validation_results["quality_issues"].append("professional_context_optimization should be a dictionary")
|
||||
return 0.0
|
||||
|
||||
professional_fields = [
|
||||
"industry_specific_positioning",
|
||||
"expertise_level_adaptation",
|
||||
"company_size_considerations",
|
||||
"business_model_alignment",
|
||||
"professional_role_authority",
|
||||
"demographic_targeting",
|
||||
"psychographic_engagement",
|
||||
"conversion_optimization"
|
||||
]
|
||||
|
||||
score = 0.0
|
||||
for field in professional_fields:
|
||||
if field in context_data and context_data[field]:
|
||||
score += 1.0
|
||||
# Check for meaningful content (not just placeholder text)
|
||||
if isinstance(context_data[field], str) and len(context_data[field]) > 50:
|
||||
score += 0.5
|
||||
else:
|
||||
validation_results["incomplete_fields"].append(f"professional_context_optimization.{field}")
|
||||
|
||||
return (score / len(professional_fields)) * 100
|
||||
|
||||
def _validate_content_quality(self, persona_data: Dict[str, Any], validation_results: Dict[str, Any]) -> float:
|
||||
"""Validate content quality and depth."""
|
||||
score = 0.0
|
||||
|
||||
# Check for meaningful content in key fields
|
||||
quality_checks = [
|
||||
("sentence_metrics", "optimal_sentence_length"),
|
||||
("lexical_adaptations", "platform_specific_words"),
|
||||
("professional_networking", "thought_leadership_positioning"),
|
||||
("linkedin_features", "articles_strategy")
|
||||
]
|
||||
|
||||
for field, subfield in quality_checks:
|
||||
if field in persona_data and subfield in persona_data[field]:
|
||||
content = persona_data[field][subfield]
|
||||
if isinstance(content, str) and len(content) > 30:
|
||||
score += 1.0
|
||||
elif isinstance(content, list) and len(content) > 3:
|
||||
score += 1.0
|
||||
else:
|
||||
validation_results["quality_issues"].append(f"{field}.{subfield} content too brief")
|
||||
else:
|
||||
validation_results["quality_issues"].append(f"{field}.{subfield} missing or empty")
|
||||
|
||||
return (score / len(quality_checks)) * 100
|
||||
|
||||
def _assess_persona_quality(self, validation_results: Dict[str, Any]) -> None:
|
||||
"""Assess overall persona quality and provide recommendations."""
|
||||
quality_score = validation_results["quality_score"]
|
||||
|
||||
if quality_score >= 90:
|
||||
validation_results["strengths"].append("Excellent LinkedIn persona with comprehensive optimization")
|
||||
elif quality_score >= 80:
|
||||
validation_results["strengths"].append("Strong LinkedIn persona with good optimization")
|
||||
elif quality_score >= 70:
|
||||
validation_results["strengths"].append("Good LinkedIn persona with basic optimization")
|
||||
else:
|
||||
validation_results["quality_issues"].append("LinkedIn persona needs significant improvement")
|
||||
|
||||
# Add specific recommendations based on missing fields
|
||||
if "professional_context_optimization" in validation_results["missing_fields"]:
|
||||
validation_results["recommendations"].append("Add professional context optimization for industry-specific positioning")
|
||||
|
||||
if "algorithm_optimization" in validation_results["missing_fields"]:
|
||||
validation_results["recommendations"].append("Add algorithm optimization for better LinkedIn reach")
|
||||
|
||||
if validation_results["incomplete_fields"]:
|
||||
validation_results["recommendations"].append(f"Complete {len(validation_results['incomplete_fields'])} incomplete fields for better optimization")
|
||||
|
||||
# Add enterprise-grade recommendations
|
||||
if quality_score >= 80:
|
||||
validation_results["recommendations"].append("Persona is enterprise-ready for professional LinkedIn content")
|
||||
else:
|
||||
validation_results["recommendations"].append("Consider regenerating persona with more comprehensive data")
|
||||
|
||||
def optimize_for_linkedin_algorithm(self, persona_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Comprehensive LinkedIn algorithm optimization for maximum reach and engagement.
|
||||
|
||||
Args:
|
||||
persona_data: LinkedIn persona data to optimize
|
||||
|
||||
Returns:
|
||||
Algorithm-optimized persona data with advanced optimization features
|
||||
"""
|
||||
try:
|
||||
optimized_persona = persona_data.copy()
|
||||
|
||||
# Initialize algorithm optimization if not present
|
||||
if "algorithm_optimization" not in optimized_persona:
|
||||
optimized_persona["algorithm_optimization"] = {}
|
||||
|
||||
# 1. CONTENT QUALITY OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["content_quality_optimization"] = {
|
||||
"original_insights_priority": [
|
||||
"Share proprietary industry insights and case studies",
|
||||
"Publish data-driven analyses and research findings",
|
||||
"Create thought leadership content with unique perspectives",
|
||||
"Avoid generic or recycled content that lacks value"
|
||||
],
|
||||
"professional_credibility_boost": [
|
||||
"Include relevant credentials and expertise indicators",
|
||||
"Reference industry experience and achievements",
|
||||
"Use professional language and terminology appropriately",
|
||||
"Maintain consistent brand voice and messaging"
|
||||
],
|
||||
"content_depth_requirements": [
|
||||
"Provide actionable insights and practical advice",
|
||||
"Include specific examples and real-world applications",
|
||||
"Offer comprehensive analysis rather than surface-level content",
|
||||
"Create content that solves professional problems"
|
||||
]
|
||||
}
|
||||
|
||||
# 2. MULTIMEDIA FORMAT OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["multimedia_strategy"] = {
|
||||
"native_video_optimization": [
|
||||
"Upload videos directly to LinkedIn for maximum reach",
|
||||
"Keep videos 1-3 minutes for optimal engagement",
|
||||
"Include captions for accessibility and broader reach",
|
||||
"Start with compelling hooks to retain viewers"
|
||||
],
|
||||
"carousel_document_strategy": [
|
||||
"Create swipeable educational content and tutorials",
|
||||
"Use 5-10 slides for optimal engagement",
|
||||
"Include clear, scannable text and visuals",
|
||||
"End with strong call-to-action"
|
||||
],
|
||||
"visual_content_optimization": [
|
||||
"Use high-quality, professional images and graphics",
|
||||
"Create infographics that convey complex information simply",
|
||||
"Design visually appealing quote cards and statistics",
|
||||
"Ensure all visuals align with professional brand"
|
||||
]
|
||||
}
|
||||
|
||||
# 3. ENGAGEMENT OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["engagement_optimization"] = {
|
||||
"comment_encouragement_strategies": [
|
||||
"Ask thought-provoking questions that invite discussion",
|
||||
"Pose industry-specific challenges or scenarios",
|
||||
"Request personal experiences and insights",
|
||||
"Create polls and surveys for interactive engagement"
|
||||
],
|
||||
"network_interaction_boost": [
|
||||
"Respond to comments within 2-4 hours for maximum visibility",
|
||||
"Engage meaningfully with others' content before posting",
|
||||
"Share and comment on industry leaders' posts",
|
||||
"Participate actively in relevant LinkedIn groups"
|
||||
],
|
||||
"professional_relationship_building": [
|
||||
"Tag relevant connections when appropriate",
|
||||
"Mention industry experts and thought leaders",
|
||||
"Collaborate with peers on joint content",
|
||||
"Build genuine professional relationships"
|
||||
]
|
||||
}
|
||||
|
||||
# 4. TIMING AND FREQUENCY OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["timing_optimization"] = {
|
||||
"optimal_posting_schedule": [
|
||||
"Tuesday-Thursday: 8-11 AM EST for maximum professional engagement",
|
||||
"Wednesday: Peak day for B2B content and thought leadership",
|
||||
"Avoid posting on weekends unless targeting specific audiences",
|
||||
"Maintain consistent posting schedule for algorithm recognition"
|
||||
],
|
||||
"frequency_optimization": [
|
||||
"Post 3-5 times per week for consistent visibility",
|
||||
"Balance original content with curated industry insights",
|
||||
"Space posts 4-6 hours apart to avoid audience fatigue",
|
||||
"Monitor engagement rates to adjust frequency"
|
||||
],
|
||||
"timezone_considerations": [
|
||||
"Consider global audience time zones for international reach",
|
||||
"Adjust posting times based on target audience location",
|
||||
"Use LinkedIn Analytics to identify peak engagement times",
|
||||
"Test different time slots to optimize reach"
|
||||
]
|
||||
}
|
||||
|
||||
# 5. HASHTAG AND DISCOVERABILITY OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["discoverability_optimization"] = {
|
||||
"strategic_hashtag_usage": [
|
||||
"Use 3-5 relevant hashtags for optimal reach",
|
||||
"Mix broad industry hashtags with niche-specific tags",
|
||||
"Include trending hashtags when relevant to content",
|
||||
"Create branded hashtags for consistent brand recognition"
|
||||
],
|
||||
"keyword_optimization": [
|
||||
"Include industry-specific keywords naturally in content",
|
||||
"Use professional terminology that resonates with target audience",
|
||||
"Optimize for LinkedIn's search algorithm",
|
||||
"Include location-based keywords for local reach"
|
||||
],
|
||||
"content_categorization": [
|
||||
"Tag content appropriately for LinkedIn's content categorization",
|
||||
"Use consistent themes and topics for algorithm recognition",
|
||||
"Create content series for sustained engagement",
|
||||
"Leverage LinkedIn's content suggestions and trending topics"
|
||||
]
|
||||
}
|
||||
|
||||
# 6. LINKEDIN FEATURES OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["linkedin_features_optimization"] = {
|
||||
"articles_strategy": [
|
||||
"Publish long-form articles for thought leadership positioning",
|
||||
"Use compelling headlines that encourage clicks",
|
||||
"Include relevant images and formatting for readability",
|
||||
"Cross-promote articles in regular posts"
|
||||
],
|
||||
"polls_and_surveys": [
|
||||
"Create engaging polls to drive interaction",
|
||||
"Ask industry-relevant questions that spark discussion",
|
||||
"Use poll results to create follow-up content",
|
||||
"Share poll insights to provide value to audience"
|
||||
],
|
||||
"events_and_networking": [
|
||||
"Host or participate in LinkedIn events and webinars",
|
||||
"Use LinkedIn's event features for promotion and networking",
|
||||
"Create virtual networking opportunities",
|
||||
"Leverage LinkedIn Live for real-time engagement"
|
||||
]
|
||||
}
|
||||
|
||||
# 7. PERFORMANCE MONITORING AND OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["performance_monitoring"] = {
|
||||
"key_metrics_tracking": [
|
||||
"Monitor engagement rate (likes, comments, shares, saves)",
|
||||
"Track reach and impression metrics",
|
||||
"Analyze click-through rates on links and CTAs",
|
||||
"Measure follower growth and network expansion"
|
||||
],
|
||||
"content_performance_analysis": [
|
||||
"Identify top-performing content types and topics",
|
||||
"Analyze posting times for optimal engagement",
|
||||
"Track hashtag performance and reach",
|
||||
"Monitor audience demographics and interests"
|
||||
],
|
||||
"optimization_recommendations": [
|
||||
"A/B test different content formats and styles",
|
||||
"Experiment with posting frequencies and timing",
|
||||
"Test various hashtag combinations and strategies",
|
||||
"Continuously refine content based on performance data"
|
||||
]
|
||||
}
|
||||
|
||||
# 8. PROFESSIONAL CONTEXT OPTIMIZATION
|
||||
optimized_persona["algorithm_optimization"]["professional_context_optimization"] = {
|
||||
"industry_specific_optimization": [
|
||||
"Tailor content to industry-specific trends and challenges",
|
||||
"Use industry terminology and references appropriately",
|
||||
"Address current industry issues and developments",
|
||||
"Position as thought leader within specific industry"
|
||||
],
|
||||
"career_stage_targeting": [
|
||||
"Create content relevant to different career stages",
|
||||
"Address professional development and growth topics",
|
||||
"Share career insights and advancement strategies",
|
||||
"Provide value to both junior and senior professionals"
|
||||
],
|
||||
"company_size_considerations": [
|
||||
"Adapt content for different company sizes and structures",
|
||||
"Address challenges specific to startups, SMBs, and enterprises",
|
||||
"Provide relevant insights for different organizational contexts",
|
||||
"Consider decision-making processes and hierarchies"
|
||||
]
|
||||
}
|
||||
|
||||
logger.info("✅ LinkedIn persona comprehensively optimized for 2024 algorithm performance")
|
||||
return optimized_persona
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error optimizing LinkedIn persona for algorithm: {str(e)}")
|
||||
return persona_data
|
||||
1082
backend/services/persona/persona_quality_improver.py
Normal file
1082
backend/services/persona/persona_quality_improver.py
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user