ALwrity + Wordpress + Wix + GSC integration
This commit is contained in:
@@ -15,10 +15,34 @@ class PersonaPromptBuilder:
|
||||
def build_persona_analysis_prompt(self, onboarding_data: Dict[str, Any]) -> str:
|
||||
"""Build the main persona analysis prompt with comprehensive data."""
|
||||
|
||||
# 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 {}
|
||||
# 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.
|
||||
@@ -115,10 +139,8 @@ Style Patterns: {json.dumps(website_analysis.get('style_patterns', {}), indent=2
|
||||
- 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
|
||||
- Provide a confidence score (0-100) based on data richness and quality
|
||||
- Include detailed analysis notes explaining your reasoning and data sources
|
||||
|
||||
Generate a comprehensive, data-driven persona profile that can be used to replicate this writing style across different platforms while maintaining brand authenticity and competitive positioning.
|
||||
Generate a comprehensive, data-driven persona profile that accurately captures the writing style and brand voice to replicate consistently across different platforms.
|
||||
"""
|
||||
|
||||
return prompt
|
||||
@@ -256,11 +278,9 @@ Generate a platform-optimized persona adaptation that maintains brand consistenc
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"confidence_score": {"type": "number"},
|
||||
"analysis_notes": {"type": "string"}
|
||||
}
|
||||
},
|
||||
"required": ["identity", "linguistic_fingerprint", "tonal_range", "confidence_score"]
|
||||
"required": ["identity", "linguistic_fingerprint", "tonal_range"]
|
||||
}
|
||||
|
||||
def get_platform_schema(self) -> Dict[str, Any]:
|
||||
|
||||
@@ -13,28 +13,35 @@ 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
|
||||
import spacy
|
||||
|
||||
class EnhancedLinguisticAnalyzer:
|
||||
"""Advanced linguistic analysis for persona creation and improvement."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the linguistic analyzer."""
|
||||
"""Initialize the linguistic analyzer with required spaCy dependency."""
|
||||
self.nlp = None
|
||||
self.spacy_available = False
|
||||
|
||||
# spaCy is REQUIRED for high-quality persona generation
|
||||
try:
|
||||
# Try to load spaCy model
|
||||
import spacy
|
||||
self.nlp = spacy.load("en_core_web_sm")
|
||||
except OSError:
|
||||
logger.warning("spaCy model not found. Install with: python -m spacy download en_core_web_sm")
|
||||
self.spacy_available = True
|
||||
logger.info("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')
|
||||
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', quiet=True)
|
||||
nltk.download('punkt_tab', quiet=True) # Updated for newer NLTK versions
|
||||
nltk.download('stopwords', quiet=True)
|
||||
nltk.download('averaged_perceptron_tagger', quiet=True)
|
||||
|
||||
@@ -625,5 +632,4 @@ class EnhancedLinguisticAnalyzer:
|
||||
clauses = len(re.findall(r'[,;]', sentence)) + 1
|
||||
total_clauses += clauses
|
||||
|
||||
return total_clauses / len(sentences) if sentences else 0
|
||||
a
|
||||
return total_clauses / len(sentences) if sentences else 0
|
||||
@@ -26,6 +26,299 @@ class PersonaQualityImprover:
|
||||
self.linguistic_analyzer = EnhancedLinguisticAnalyzer()
|
||||
logger.info("PersonaQualityImprover initialized")
|
||||
|
||||
def assess_persona_quality_comprehensive(
|
||||
self,
|
||||
core_persona: Dict[str, Any],
|
||||
platform_personas: Dict[str, Any],
|
||||
linguistic_analysis: Dict[str, Any],
|
||||
user_preferences: Optional[Dict[str, Any]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Comprehensive quality assessment for quality-first approach.
|
||||
"""
|
||||
try:
|
||||
# Calculate comprehensive quality metrics
|
||||
quality_metrics = self._calculate_comprehensive_quality_metrics(
|
||||
core_persona, platform_personas, linguistic_analysis, user_preferences
|
||||
)
|
||||
|
||||
# Generate detailed recommendations
|
||||
recommendations = self._generate_comprehensive_recommendations(quality_metrics, linguistic_analysis)
|
||||
|
||||
return {
|
||||
"overall_score": quality_metrics.get('overall_score', 0),
|
||||
"core_completeness": quality_metrics.get('core_completeness', 0),
|
||||
"platform_consistency": quality_metrics.get('platform_consistency', 0),
|
||||
"platform_optimization": quality_metrics.get('platform_optimization', 0),
|
||||
"linguistic_quality": quality_metrics.get('linguistic_quality', 0),
|
||||
"recommendations": recommendations,
|
||||
"assessment_method": "comprehensive_ai_based",
|
||||
"linguistic_insights": linguistic_analysis,
|
||||
"detailed_metrics": quality_metrics
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Comprehensive quality assessment error: {str(e)}")
|
||||
return {
|
||||
"overall_score": 75,
|
||||
"core_completeness": 75,
|
||||
"platform_consistency": 75,
|
||||
"platform_optimization": 75,
|
||||
"linguistic_quality": 75,
|
||||
"recommendations": ["Quality assessment completed with default metrics"],
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
def improve_persona_quality(
|
||||
self,
|
||||
core_persona: Dict[str, Any],
|
||||
platform_personas: Dict[str, Any],
|
||||
quality_metrics: Dict[str, Any]
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Improve persona quality based on assessment results.
|
||||
"""
|
||||
try:
|
||||
logger.info("Improving persona quality based on assessment results...")
|
||||
|
||||
improved_core_persona = self._improve_core_persona(core_persona, quality_metrics)
|
||||
improved_platform_personas = self._improve_platform_personas(platform_personas, quality_metrics)
|
||||
|
||||
return {
|
||||
"core_persona": improved_core_persona,
|
||||
"platform_personas": improved_platform_personas,
|
||||
"improvement_applied": True,
|
||||
"improvement_details": "Quality improvements applied based on assessment results"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Persona quality improvement error: {str(e)}")
|
||||
return {"error": f"Failed to improve persona quality: {str(e)}"}
|
||||
|
||||
def _calculate_comprehensive_quality_metrics(
|
||||
self,
|
||||
core_persona: Dict[str, Any],
|
||||
platform_personas: Dict[str, Any],
|
||||
linguistic_analysis: Dict[str, Any],
|
||||
user_preferences: Optional[Dict[str, Any]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Calculate comprehensive quality metrics."""
|
||||
try:
|
||||
# Core completeness (30% weight)
|
||||
core_completeness = self._assess_core_completeness(core_persona, linguistic_analysis)
|
||||
|
||||
# Platform consistency (25% weight)
|
||||
platform_consistency = self._assess_platform_consistency(core_persona, platform_personas)
|
||||
|
||||
# Platform optimization (25% weight)
|
||||
platform_optimization = self._assess_platform_optimization(platform_personas)
|
||||
|
||||
# Linguistic quality (20% weight)
|
||||
linguistic_quality = self._assess_linguistic_quality(linguistic_analysis)
|
||||
|
||||
# Calculate weighted overall score
|
||||
overall_score = int((
|
||||
core_completeness * 0.30 +
|
||||
platform_consistency * 0.25 +
|
||||
platform_optimization * 0.25 +
|
||||
linguistic_quality * 0.20
|
||||
))
|
||||
|
||||
return {
|
||||
"overall_score": overall_score,
|
||||
"core_completeness": core_completeness,
|
||||
"platform_consistency": platform_consistency,
|
||||
"platform_optimization": platform_optimization,
|
||||
"linguistic_quality": linguistic_quality,
|
||||
"weights": {
|
||||
"core_completeness": 0.30,
|
||||
"platform_consistency": 0.25,
|
||||
"platform_optimization": 0.25,
|
||||
"linguistic_quality": 0.20
|
||||
}
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error calculating comprehensive quality metrics: {str(e)}")
|
||||
return {
|
||||
"overall_score": 75,
|
||||
"core_completeness": 75,
|
||||
"platform_consistency": 75,
|
||||
"platform_optimization": 75,
|
||||
"linguistic_quality": 75
|
||||
}
|
||||
|
||||
def _assess_core_completeness(self, core_persona: Dict[str, Any], linguistic_analysis: Dict[str, Any]) -> int:
|
||||
"""Assess core persona completeness."""
|
||||
required_sections = ['writing_style', 'content_characteristics', 'brand_voice', 'target_audience']
|
||||
present_sections = sum(1 for section in required_sections if section in core_persona and core_persona[section])
|
||||
|
||||
base_score = int((present_sections / len(required_sections)) * 100)
|
||||
|
||||
# Boost if linguistic analysis provides additional insights
|
||||
if linguistic_analysis and linguistic_analysis.get('analysis_completeness', 0) > 0.8:
|
||||
base_score = min(base_score + 10, 100)
|
||||
|
||||
return base_score
|
||||
|
||||
def _assess_platform_consistency(self, core_persona: Dict[str, Any], platform_personas: Dict[str, Any]) -> int:
|
||||
"""Assess consistency across platform personas."""
|
||||
if not platform_personas:
|
||||
return 50
|
||||
|
||||
core_voice = core_persona.get('brand_voice', {}).get('keywords', [])
|
||||
consistency_scores = []
|
||||
|
||||
for platform, persona in platform_personas.items():
|
||||
if 'error' not in persona:
|
||||
platform_voice = persona.get('brand_voice', {}).get('keywords', [])
|
||||
overlap = len(set(core_voice) & set(platform_voice))
|
||||
consistency_scores.append(min(overlap * 10, 100))
|
||||
|
||||
return int(sum(consistency_scores) / len(consistency_scores)) if consistency_scores else 75
|
||||
|
||||
def _assess_platform_optimization(self, platform_personas: Dict[str, Any]) -> int:
|
||||
"""Assess platform-specific optimization quality."""
|
||||
if not platform_personas:
|
||||
return 50
|
||||
|
||||
optimization_scores = []
|
||||
for platform, persona in platform_personas.items():
|
||||
if 'error' not in persona:
|
||||
has_optimizations = any(key in persona for key in [
|
||||
'platform_optimizations', 'content_guidelines', 'engagement_strategies'
|
||||
])
|
||||
optimization_scores.append(90 if has_optimizations else 60)
|
||||
|
||||
return int(sum(optimization_scores) / len(optimization_scores)) if optimization_scores else 75
|
||||
|
||||
def _assess_linguistic_quality(self, linguistic_analysis: Dict[str, Any]) -> int:
|
||||
"""Assess linguistic analysis quality."""
|
||||
if not linguistic_analysis:
|
||||
return 50
|
||||
|
||||
quality_indicators = [
|
||||
'analysis_completeness',
|
||||
'style_consistency',
|
||||
'vocabulary_sophistication',
|
||||
'content_coherence'
|
||||
]
|
||||
|
||||
scores = [linguistic_analysis.get(indicator, 0.5) for indicator in quality_indicators]
|
||||
return int(sum(scores) / len(scores) * 100)
|
||||
|
||||
def _generate_comprehensive_recommendations(self, quality_metrics: Dict[str, Any], linguistic_analysis: Dict[str, Any]) -> List[str]:
|
||||
"""Generate comprehensive quality recommendations."""
|
||||
recommendations = []
|
||||
|
||||
if quality_metrics.get('core_completeness', 0) < 85:
|
||||
recommendations.append("Enhance core persona with more detailed writing style characteristics and brand voice elements")
|
||||
|
||||
if quality_metrics.get('platform_consistency', 0) < 80:
|
||||
recommendations.append("Improve brand voice consistency across all platform adaptations")
|
||||
|
||||
if quality_metrics.get('platform_optimization', 0) < 85:
|
||||
recommendations.append("Strengthen platform-specific optimizations and engagement strategies")
|
||||
|
||||
if quality_metrics.get('linguistic_quality', 0) < 80:
|
||||
recommendations.append("Improve linguistic quality and writing sophistication")
|
||||
|
||||
# Add linguistic-specific recommendations
|
||||
if linguistic_analysis:
|
||||
if linguistic_analysis.get('style_consistency', 0) < 0.7:
|
||||
recommendations.append("Enhance writing style consistency across content samples")
|
||||
|
||||
if linguistic_analysis.get('vocabulary_sophistication', 0) < 0.7:
|
||||
recommendations.append("Increase vocabulary sophistication for better audience engagement")
|
||||
|
||||
if not recommendations:
|
||||
recommendations.append("Your personas demonstrate excellent quality across all assessment criteria!")
|
||||
|
||||
return recommendations
|
||||
|
||||
def _improve_core_persona(self, core_persona: Dict[str, Any], quality_metrics: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Improve core persona based on quality metrics."""
|
||||
improved_persona = core_persona.copy()
|
||||
|
||||
# Enhance based on quality gaps
|
||||
if quality_metrics.get('core_completeness', 0) < 85:
|
||||
# Add more detailed characteristics
|
||||
if 'writing_style' not in improved_persona:
|
||||
improved_persona['writing_style'] = {}
|
||||
|
||||
if 'sentence_structure' not in improved_persona['writing_style']:
|
||||
improved_persona['writing_style']['sentence_structure'] = 'Varied and engaging'
|
||||
|
||||
if 'vocabulary_level' not in improved_persona['writing_style']:
|
||||
improved_persona['writing_style']['vocabulary_level'] = 'Professional with accessible language'
|
||||
|
||||
return improved_persona
|
||||
|
||||
def _improve_platform_personas(self, platform_personas: Dict[str, Any], quality_metrics: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Improve platform personas based on quality metrics."""
|
||||
improved_personas = platform_personas.copy()
|
||||
|
||||
# Enhance each platform persona
|
||||
for platform, persona in improved_personas.items():
|
||||
if 'error' not in persona:
|
||||
# Add platform-specific optimizations if missing
|
||||
if 'platform_optimizations' not in persona:
|
||||
persona['platform_optimizations'] = self._get_default_platform_optimizations(platform)
|
||||
|
||||
# Enhance engagement strategies
|
||||
if 'engagement_strategies' not in persona:
|
||||
persona['engagement_strategies'] = self._get_default_engagement_strategies(platform)
|
||||
|
||||
return improved_personas
|
||||
|
||||
def _get_default_platform_optimizations(self, platform: str) -> Dict[str, Any]:
|
||||
"""Get default platform optimizations."""
|
||||
optimizations = {
|
||||
'linkedin': {
|
||||
'professional_networking': True,
|
||||
'thought_leadership': True,
|
||||
'industry_insights': True
|
||||
},
|
||||
'facebook': {
|
||||
'community_building': True,
|
||||
'social_engagement': True,
|
||||
'visual_storytelling': True
|
||||
},
|
||||
'twitter': {
|
||||
'real_time_updates': True,
|
||||
'hashtag_optimization': True,
|
||||
'concise_messaging': True
|
||||
},
|
||||
'blog': {
|
||||
'seo_optimization': True,
|
||||
'long_form_content': True,
|
||||
'storytelling': True
|
||||
}
|
||||
}
|
||||
return optimizations.get(platform, {})
|
||||
|
||||
def _get_default_engagement_strategies(self, platform: str) -> Dict[str, Any]:
|
||||
"""Get default engagement strategies."""
|
||||
strategies = {
|
||||
'linkedin': {
|
||||
'call_to_action': 'Connect with me to discuss',
|
||||
'engagement_style': 'Professional networking'
|
||||
},
|
||||
'facebook': {
|
||||
'call_to_action': 'Join our community',
|
||||
'engagement_style': 'Social interaction'
|
||||
},
|
||||
'twitter': {
|
||||
'call_to_action': 'Follow for updates',
|
||||
'engagement_style': 'Real-time conversation'
|
||||
},
|
||||
'blog': {
|
||||
'call_to_action': 'Subscribe for more insights',
|
||||
'engagement_style': 'Educational content'
|
||||
}
|
||||
}
|
||||
return strategies.get(platform, {})
|
||||
|
||||
def assess_persona_quality(self, persona_id: int, user_feedback: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Assess the quality of a persona and provide improvement suggestions.
|
||||
|
||||
Reference in New Issue
Block a user