DEV Community

2x lazymac
2x lazymac

Posted on

Text Analysis API: Sentiment, Keywords, Readability in One Call

Sentiment analysis, keyword extraction, and readability scoring are three separate libraries you don't need to install. Here's how to get all three in one API call.

The Single-Call Approach

import requests

text = """
Cloudflare Workers is an incredible platform for building APIs.
The free tier is genuinely useful for production workloads,
though the 10ms CPU limit can be frustrating for compute-heavy tasks.
"""

resp = requests.post("https://api.lazy-mac.com/text-analysis/analyze", json={
    "text": text,
    "features": ["sentiment", "keywords", "readability", "language"]
})

analysis = resp.json()
Enter fullscreen mode Exit fullscreen mode

Response:

{
  "sentiment": {
    "label": "mixed",
    "score": 0.62,
    "positive": 0.71,
    "negative": 0.29
  },
  "keywords": [
    {"word": "Cloudflare Workers", "score": 0.94},
    {"word": "free tier", "score": 0.87},
    {"word": "10ms CPU limit", "score": 0.71}
  ],
  "readability": {
    "flesch_score": 58.3,
    "grade_level": "10th grade",
    "avg_sentence_length": 18.2,
    "complex_word_ratio": 0.14
  },
  "language": "en",
  "word_count": 42
}
Enter fullscreen mode Exit fullscreen mode

Use Cases

Content moderation

def is_safe_content(text: str) -> bool:
    analysis = analyze(text)
    return analysis['sentiment']['negative'] < 0.7
Enter fullscreen mode Exit fullscreen mode

SEO optimization

def optimize_for_seo(text: str) -> list:
    analysis = analyze(text)
    keywords = [k['word'] for k in analysis['keywords'][:5]]
    if analysis['readability']['grade_level'] > '12th grade':
        return {'action': 'simplify', 'target_keywords': keywords}
    return {'action': 'publish', 'keywords': keywords}
Enter fullscreen mode Exit fullscreen mode

Customer feedback triage

feedback_items = get_support_tickets()
for ticket in feedback_items:
    analysis = analyze(ticket['text'])
    if analysis['sentiment']['negative'] > 0.8:
        escalate_to_human(ticket)
    else:
        send_auto_reply(ticket, analysis['keywords'])
Enter fullscreen mode Exit fullscreen mode

Text Analysis API | Full API store

Top comments (0)