Sentence Count API: The Complete Guide

Need to count sentences in text programmatically? This guide covers everything you need to know about sentence counting via API, including handling abbreviations, edge cases, and multiple languages.

What is Sentence Counting?

Sentence counting determines the number of sentences in a text. Unlike simple character or word counting, sentence detection requires understanding of language rules, punctuation, and context. A sentence typically ends with a period, question mark, or exclamation point.

Example: "Hello! How are you? I'm fine." contains 3 sentences.

Challenges in Sentence Detection

Accurate sentence counting must handle various edge cases:

  • Abbreviations: "Dr. Smith" should not count as 2 sentences
  • Decimal numbers: "The price is $9.99" is one sentence
  • Ellipsis: "Wait for it..." is one sentence
  • URLs and emails: "Visit example.com today" is one sentence
Note: A good sentence count API uses NLP techniques to accurately detect sentence boundaries, not just simple punctuation splitting.

Using the Sentence Count API

TinyFn provides a simple endpoint to count sentences:

API Request
POST https://api.tinyfn.io/v1/text/sentence-count
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "text": "Hello! How are you? I'm doing great."
}
Response
{
  "sentence_count": 3,
  "sentences": [
    "Hello!",
    "How are you?",
    "I'm doing great."
  ]
}

Parameters

Parameter Type Description
text string The text to analyze (required)
return_sentences boolean Return array of individual sentences (default: false)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/text/sentence-count',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'your-api-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ text: 'Hello! How are you? I\'m doing great.' })
  }
);
const result = await response.json();
console.log(result.sentence_count); // 3

Python

import requests

response = requests.post(
    'https://api.tinyfn.io/v1/text/sentence-count',
    headers={'X-API-Key': 'your-api-key'},
    json={'text': "Hello! How are you? I'm doing great."}
)
result = response.json()
print(result['sentence_count'])  # 3

cURL

curl -X POST "https://api.tinyfn.io/v1/text/sentence-count" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello! How are you? I'\''m doing great."}'

Common Use Cases

  • Readability Analysis: Calculate average sentence length for readability scores
  • Content Quality: Check for appropriate sentence variety
  • Writing Tools: Help writers improve sentence structure
  • Text Summarization: Extract key sentences from documents
  • Translation Services: Segment text for translation

Best Practices

  1. Use NLP-based APIs: Simple regex won't handle edge cases
  2. Consider language: Different languages have different rules
  3. Validate input: Ensure text is properly encoded
  4. Handle edge cases: Test with abbreviations, URLs, numbers

Try the Sentence Count API

Get your free API key and start counting sentences in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key