AI Analyzer Tool
Develop AI-powered tools for comprehensive data analysis, text mining, sentiment analysis, pattern recognition, and automated report generation.
Introduction
AI transforms data analysis from a manual, time-consuming process into an automated, insight-generating pipeline. Instead of writing complex queries or building statistical models, you can describe what you want to know in plain language.
In this guide, you will build a complete AI analyzer tool that can process structured data, analyze text, detect sentiments and patterns, and generate professional reports automatically.
Data Analysis with AI
AI can analyze structured data (JSON, CSV, databases) and generate insights that would take a human analyst hours to produce:
from openai import OpenAI
import json
client = OpenAI()
def analyze_dataset(data, context=""):
"""Perform AI-powered analysis on structured data."""
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """You are a senior data analyst.
Analyze the provided data and return insights in JSON:
{
"summary": "Brief overview",
"key_metrics": [{"name": "", "value": "", "trend": ""}],
"patterns": ["pattern 1", "pattern 2"],
"anomalies": ["anomaly 1"],
"recommendations": ["action 1", "action 2"],
"risk_factors": ["risk 1"]
}"""},
{"role": "user", "content": f"Context: {context}\n\nData:\n{json.dumps(data, indent=2)}"}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Example usage
sales_data = {
"q1": {"revenue": 150000, "customers": 45, "churn": 3},
"q2": {"revenue": 180000, "customers": 52, "churn": 2},
"q3": {"revenue": 165000, "customers": 48, "churn": 7},
"q4": {"revenue": 210000, "customers": 61, "churn": 4}
}
insights = analyze_dataset(sales_data, "Annual B2B SaaS performance")
print(json.dumps(insights, indent=2))Text Analysis
Text analysis extracts structured information from unstructured text — articles, emails, reports, social media posts:
def analyze_text(text, analysis_type="comprehensive"):
"""Analyze text for various linguistic properties."""
prompts = {
"comprehensive": """Analyze this text comprehensively:
1. Main topics and themes
2. Key entities (people, companies, locations)
3. Sentiment (positive/negative/neutral + confidence)
4. Writing style and tone
5. Summary in 2-3 sentences
Return as structured JSON.""",
"entities": """Extract all named entities from this text:
- People, Organizations, Locations, Dates, Products
Return as JSON with categories.""",
"keywords": """Extract the top 10 most important keywords/phrases.
For each, provide relevance score (0-1) and context.
Return as JSON array."""
}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": prompts.get(analysis_type, prompts["comprehensive"])},
{"role": "user", "content": text}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Usage
article = """Artificial intelligence is transforming healthcare.
New diagnostic tools powered by machine learning can detect
diseases earlier and more accurately than traditional methods.
Hospitals in Germany and the US are adopting these systems rapidly."""
result = analyze_text(article, "comprehensive")
print(json.dumps(result, indent=2))Sentiment Analysis
Sentiment analysis determines the emotional tone of text. It is essential for monitoring customer feedback, social media, and brand perception:
def batch_sentiment_analysis(texts):
"""Analyze sentiment for multiple texts at once."""
formatted = "\n".join([f"{i+1}. {t}" for i, t in enumerate(texts)])
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Analyze the sentiment
of each numbered text. Return JSON:
{"results": [
{"id": 1, "text_preview": "first 30 chars...",
"sentiment": "positive/negative/neutral/mixed",
"confidence": 0.0-1.0,
"emotions": ["joy", "trust"],
"key_phrases": ["positive phrase"]}
]}"""},
{"role": "user", "content": formatted}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Analyze customer reviews
reviews = [
"Absolutely love this product! Best purchase ever.",
"Terrible experience. The item arrived broken.",
"It's okay. Nothing special but does the job.",
"Great quality but shipping was very slow.",
"Customer service was outstanding, they fixed everything!"
]
results = batch_sentiment_analysis(reviews)
for r in results["results"]:
print(f"Review {r['id']}: {r['sentiment']} ({r['confidence']})")✅ Beyond Positive/Negative
Modern AI can detect nuanced emotions (frustration, excitement, confusion) and mixed sentiments within the same text. Ask the model to identify specific emotions rather than just positive/negative for richer analysis.
Pattern Recognition
AI is excellent at identifying patterns in data that humans might miss — trends, anomalies, cyclical behaviors, and correlations:
def detect_patterns(data, data_type="time_series"):
"""Detect patterns in data using AI analysis."""
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""You are an expert data scientist.
Analyze this {data_type} data for patterns:
1. Trends (upward, downward, cyclical)
2. Seasonality or periodic patterns
3. Outliers or anomalies
4. Correlations between variables
5. Predicted next values (if time series)
Return JSON with detailed findings and confidence levels."""},
{"role": "user", "content": json.dumps(data)}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
# Example: Detect patterns in website traffic
traffic_data = {
"daily_visitors": [
{"date": "2025-01-06", "visitors": 1200, "day": "Mon"},
{"date": "2025-01-07", "visitors": 1350, "day": "Tue"},
{"date": "2025-01-08", "visitors": 1400, "day": "Wed"},
{"date": "2025-01-09", "visitors": 1280, "day": "Thu"},
{"date": "2025-01-10", "visitors": 980, "day": "Fri"},
{"date": "2025-01-11", "visitors": 650, "day": "Sat"},
{"date": "2025-01-12", "visitors": 580, "day": "Sun"}
]
}
patterns = detect_patterns(traffic_data, "time_series")Report Generation
The final step is turning analysis results into human-readable reports. AI can generate different styles depending on the audience:
def generate_report(analysis_results, report_type="executive"):
"""Generate a human-readable report from analysis results."""
styles = {
"executive": "Write a concise executive summary (max 300 words). Focus on key findings, business impact, and recommended actions.",
"technical": "Write a detailed technical report with methodology, statistical details, and data quality notes.",
"presentation": "Create presentation-ready bullet points with key metrics highlighted. Include talking points for each slide."
}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""Generate a {report_type}
report based on these analysis results.
Style: {styles.get(report_type, styles['executive'])}
Structure the report with clear sections and headers.
Use specific numbers from the data."""},
{"role": "user", "content": json.dumps(analysis_results, indent=2)}
]
)
return response.choices[0].message.content
# Generate different report types
report = generate_report(insights, "executive")
print(report)Building an Analysis Pipeline
Combine all analysis steps into a single reusable pipeline:
class AIAnalyzer:
"""Complete AI analysis pipeline."""
def __init__(self):
self.client = OpenAI()
self.results = {}
def ingest(self, data, data_type="auto"):
"""Load and prepare data for analysis."""
self.raw_data = data
self.data_type = data_type
return self
def analyze(self, analyses=["summary", "patterns", "sentiment"]):
"""Run multiple analyses on the data."""
for analysis in analyses:
if analysis == "summary":
self.results["summary"] = analyze_dataset(self.raw_data)
elif analysis == "patterns":
self.results["patterns"] = detect_patterns(self.raw_data)
elif analysis == "sentiment":
if isinstance(self.raw_data, list):
self.results["sentiment"] = batch_sentiment_analysis(
self.raw_data
)
return self
def report(self, report_type="executive"):
"""Generate a final report."""
self.results["report"] = generate_report(
self.results, report_type
)
return self.results
# Usage — chain the pipeline
pipeline = AIAnalyzer()
results = (
pipeline
.ingest(sales_data)
.analyze(["summary", "patterns"])
.report("executive")
)
print(results["report"])⚠️ Validate AI Analysis
AI analysis is powerful but not infallible. Always spot-check numerical claims against the raw data. For critical business decisions, use AI analysis as a starting point, then verify with traditional statistical methods.
Summary
You have built a complete AI analysis pipeline. Key takeaways:
- AI can analyze structured data, text, and sentiment in seconds with simple prompts.
- Pattern recognition helps identify trends and anomalies humans might miss.
- Automated report generation saves hours and can produce multiple formats.
- Always validate AI-generated analysis against raw data for accuracy.