Made changes to Getting started with ALwrity and added lot of details on API keys
This commit is contained in:
88
lib/personalization/README.md
Normal file
88
lib/personalization/README.md
Normal file
@@ -0,0 +1,88 @@
|
||||
# Content Style Analyzer Guide
|
||||
|
||||
## What is the Content Style Analyzer?
|
||||
|
||||
The Content Style Analyzer is an AI-powered tool that helps you understand and improve your writing style. It analyzes your content to provide detailed insights about your writing approach, helping you create more consistent and engaging content.
|
||||
|
||||
## What Can It Do?
|
||||
|
||||
### 1. Writing Style Analysis
|
||||
The analyzer examines your content to identify:
|
||||
- **Tone**: Whether your writing is formal, casual, technical, or conversational
|
||||
- **Voice**: If you're using active or passive voice
|
||||
- **Complexity**: How complex your writing is (simple, moderate, or complex)
|
||||
- **Engagement Level**: How engaging your content is (low, medium, or high)
|
||||
|
||||
### 2. Content Characteristics
|
||||
It provides insights about:
|
||||
- **Sentence Structure**: How your sentences are organized
|
||||
- **Vocabulary Level**: Whether you're using basic, intermediate, or advanced vocabulary
|
||||
- **Paragraph Organization**: How your paragraphs flow together
|
||||
- **Content Flow**: How well your ideas progress throughout the content
|
||||
|
||||
### 3. Target Audience Analysis
|
||||
The tool helps you understand:
|
||||
- **Demographics**: Who your content appeals to
|
||||
- **Expertise Level**: Whether it's suitable for beginners, intermediate, or advanced readers
|
||||
- **Industry Focus**: Which industry your content is targeting
|
||||
- **Geographic Focus**: Which regions your content is most relevant for
|
||||
|
||||
### 4. Content Type Assessment
|
||||
It identifies:
|
||||
- **Primary Type**: Whether it's a blog post, article, product description, etc.
|
||||
- **Secondary Types**: Other content categories it might fit into
|
||||
- **Purpose**: Whether it's meant to inform, entertain, persuade, etc.
|
||||
- **Call to Action**: How effectively you're guiding readers to take action
|
||||
|
||||
### 5. Style Pattern Analysis
|
||||
The analyzer also looks for specific patterns in your writing:
|
||||
- **Sentence Patterns**: How your sentences are structured
|
||||
- **Word Patterns**: Your vocabulary choices and frequency
|
||||
- **Rhetorical Devices**: Literary techniques you're using
|
||||
|
||||
## How to Use It
|
||||
|
||||
1. **Input Your Content**: Provide your content, including:
|
||||
- Main content text
|
||||
- Title
|
||||
- Description
|
||||
|
||||
2. **Get Analysis**: The tool will analyze your content and provide detailed insights
|
||||
|
||||
3. **Review Recommendations**: Receive suggestions for:
|
||||
- Writing tone
|
||||
- Target audience
|
||||
- Content type
|
||||
- Creativity level
|
||||
- Geographic focus
|
||||
|
||||
## Benefits for Content Creators
|
||||
|
||||
1. **Consistency**: Maintain a consistent writing style across your content
|
||||
2. **Audience Alignment**: Ensure your content matches your target audience's expectations
|
||||
3. **Quality Improvement**: Identify areas where your writing can be enhanced
|
||||
4. **Style Optimization**: Get recommendations for better engagement
|
||||
5. **Content Strategy**: Make data-driven decisions about your content approach
|
||||
|
||||
## Tips for Best Results
|
||||
|
||||
1. **Provide Complete Content**: Include all relevant sections (title, description, main content)
|
||||
2. **Keep Content Length Reasonable**: The analyzer works best with content up to 4000 characters
|
||||
3. **Review All Sections**: Pay attention to all aspects of the analysis for comprehensive insights
|
||||
4. **Use Recommendations**: Apply the suggested improvements to enhance your content
|
||||
|
||||
## Understanding the Results
|
||||
|
||||
The analysis results are presented in a clear, structured format that helps you:
|
||||
- Identify your current writing style
|
||||
- Understand your content's strengths
|
||||
- Spot areas for improvement
|
||||
- Make informed decisions about future content
|
||||
|
||||
## Need Help?
|
||||
|
||||
If you encounter any issues or have questions about the analysis results, please refer to your content team or technical support for assistance.
|
||||
|
||||
---
|
||||
|
||||
*Note: This tool is designed to help content creators improve their writing style and content quality. It uses advanced AI technology to provide detailed insights and recommendations.*
|
||||
203
lib/personalization/style_analyzer.py
Normal file
203
lib/personalization/style_analyzer.py
Normal file
@@ -0,0 +1,203 @@
|
||||
"""Style analyzer module for analyzing content style using LLM."""
|
||||
|
||||
from typing import Dict, List, Optional
|
||||
from loguru import logger
|
||||
from ..gpt_providers.text_generation.main_text_generation import llm_text_gen
|
||||
import json
|
||||
import re
|
||||
|
||||
class StyleAnalyzer:
|
||||
"""Analyzer for content style using LLM."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the style analyzer."""
|
||||
logger.info("[StyleAnalyzer.__init__] Initializing style analyzer")
|
||||
|
||||
def _clean_json_response(self, text: str) -> str:
|
||||
"""
|
||||
Clean the LLM response to extract valid JSON.
|
||||
|
||||
Args:
|
||||
text (str): Raw response from LLM
|
||||
|
||||
Returns:
|
||||
str: Cleaned JSON string
|
||||
"""
|
||||
try:
|
||||
# Remove markdown code block markers
|
||||
cleaned_string = text.replace("```json", "").replace("```", "").strip()
|
||||
|
||||
# Log the cleaned JSON for debugging
|
||||
logger.debug(f"[StyleAnalyzer._clean_json_response] Cleaned JSON: {cleaned_string}")
|
||||
|
||||
return cleaned_string
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[StyleAnalyzer._clean_json_response] Error cleaning response: {str(e)}")
|
||||
return ""
|
||||
|
||||
def analyze_content_style(self, content: Dict) -> Dict:
|
||||
"""
|
||||
Analyze the style of the provided content.
|
||||
|
||||
Args:
|
||||
content (Dict): Content to analyze, containing main_content, title, etc.
|
||||
|
||||
Returns:
|
||||
Dict: Analysis results
|
||||
"""
|
||||
try:
|
||||
logger.info("[StyleAnalyzer.analyze_content_style] Starting content style analysis")
|
||||
|
||||
# Prepare content for analysis
|
||||
main_content = content.get("main_content", "")
|
||||
title = content.get("title", "")
|
||||
description = content.get("description", "")
|
||||
|
||||
# Construct the analysis prompt
|
||||
prompt = f"""Analyze the following content and provide a comprehensive writing style analysis.
|
||||
Focus on identifying the writing style, tone, and characteristics that make this content unique.
|
||||
|
||||
Title: {title}
|
||||
Description: {description}
|
||||
Content: {main_content[:4000]} # Limit content length for API
|
||||
|
||||
IMPORTANT: Respond ONLY with a JSON object in the following format. Do not include any additional text, explanations, or markdown formatting:
|
||||
{{
|
||||
"writing_style": {{
|
||||
"tone": "formal/casual/technical/etc",
|
||||
"voice": "active/passive",
|
||||
"complexity": "simple/moderate/complex",
|
||||
"engagement_level": "low/medium/high"
|
||||
}},
|
||||
"content_characteristics": {{
|
||||
"sentence_structure": "description",
|
||||
"vocabulary_level": "basic/intermediate/advanced",
|
||||
"paragraph_organization": "description",
|
||||
"content_flow": "description"
|
||||
}},
|
||||
"target_audience": {{
|
||||
"demographics": ["list"],
|
||||
"expertise_level": "beginner/intermediate/advanced",
|
||||
"industry_focus": "primary industry",
|
||||
"geographic_focus": "primary region"
|
||||
}},
|
||||
"content_type": {{
|
||||
"primary_type": "blog/article/product/etc",
|
||||
"secondary_types": ["list"],
|
||||
"purpose": "inform/entertain/persuade/etc",
|
||||
"call_to_action": "type and frequency"
|
||||
}},
|
||||
"recommended_settings": {{
|
||||
"writing_tone": "recommended tone",
|
||||
"target_audience": "recommended audience",
|
||||
"content_type": "recommended type",
|
||||
"creativity_level": "low/medium/high",
|
||||
"geographic_location": "recommended location"
|
||||
}}
|
||||
}}"""
|
||||
|
||||
# Get analysis from LLM
|
||||
logger.debug("[StyleAnalyzer.analyze_content_style] Sending prompt to LLM")
|
||||
analysis_text = llm_text_gen(prompt)
|
||||
|
||||
try:
|
||||
# Clean and parse the JSON response
|
||||
cleaned_json = self._clean_json_response(analysis_text)
|
||||
if not cleaned_json:
|
||||
raise ValueError("No valid JSON found in response")
|
||||
|
||||
# Log the cleaned JSON for debugging
|
||||
logger.debug(f"[StyleAnalyzer.analyze_content_style] Cleaned JSON: {cleaned_json}")
|
||||
|
||||
# Try to parse the cleaned JSON
|
||||
try:
|
||||
analysis = json.loads(cleaned_json)
|
||||
except json.JSONDecodeError as e:
|
||||
# If parsing fails, try to fix common JSON issues
|
||||
logger.warning(f"[StyleAnalyzer.analyze_content_style] Initial JSON parsing failed: {e}")
|
||||
|
||||
# Fix any remaining issues
|
||||
cleaned_json = re.sub(r'([^"\\])\n', r'\1 ', cleaned_json)
|
||||
cleaned_json = re.sub(r'\\n', ' ', cleaned_json)
|
||||
|
||||
# Try parsing again
|
||||
analysis = json.loads(cleaned_json)
|
||||
|
||||
logger.info("[StyleAnalyzer.analyze_content_style] Successfully parsed analysis results")
|
||||
return analysis
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"[StyleAnalyzer.analyze_content_style] Failed to parse JSON response: {e}")
|
||||
logger.debug(f"[StyleAnalyzer.analyze_content_style] Raw response: {analysis_text}")
|
||||
return {
|
||||
"error": "Failed to parse analysis results",
|
||||
"raw_response": analysis_text
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[StyleAnalyzer.analyze_content_style] Error during analysis: {str(e)}")
|
||||
return {
|
||||
"error": str(e),
|
||||
"success": False
|
||||
}
|
||||
|
||||
def analyze_style_patterns(self, content: Dict) -> Dict:
|
||||
"""
|
||||
Analyze specific writing style patterns in the content.
|
||||
|
||||
Args:
|
||||
content (Dict): Content to analyze
|
||||
|
||||
Returns:
|
||||
Dict: Pattern analysis results
|
||||
"""
|
||||
try:
|
||||
main_content = content.get("main_content", "")
|
||||
|
||||
prompt = f"""Analyze the following content for specific writing style patterns.
|
||||
Focus on identifying recurring patterns in sentence structure, word choice, and rhetorical devices.
|
||||
|
||||
Content: {main_content[:4000]}
|
||||
|
||||
IMPORTANT: Respond ONLY with a JSON object in the following format. Do not include any additional text, explanations, or markdown formatting:
|
||||
{{
|
||||
"sentence_patterns": {{
|
||||
"structure": ["list of patterns"],
|
||||
"length": "short/medium/long",
|
||||
"complexity": "simple/moderate/complex"
|
||||
}},
|
||||
"word_patterns": {{
|
||||
"vocabulary": ["list of patterns"],
|
||||
"frequency": "low/medium/high",
|
||||
"diversity": "low/medium/high"
|
||||
}},
|
||||
"rhetorical_devices": {{
|
||||
"types": ["list of devices"],
|
||||
"frequency": "low/medium/high",
|
||||
"effectiveness": "low/medium/high"
|
||||
}}
|
||||
}}"""
|
||||
|
||||
analysis_text = llm_text_gen(prompt)
|
||||
|
||||
try:
|
||||
cleaned_json = self._clean_json_response(analysis_text)
|
||||
if not cleaned_json:
|
||||
raise ValueError("No valid JSON found in response")
|
||||
|
||||
analysis = json.loads(cleaned_json)
|
||||
return analysis
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"[StyleAnalyzer.analyze_style_patterns] Failed to parse JSON response: {e}")
|
||||
return {
|
||||
"error": "Failed to parse pattern analysis results",
|
||||
"raw_response": analysis_text
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[StyleAnalyzer.analyze_style_patterns] Error during analysis: {str(e)}")
|
||||
return {
|
||||
"error": str(e),
|
||||
"success": False
|
||||
}
|
||||
Reference in New Issue
Block a user