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
Using the Sentence Count API
TinyFn provides a simple endpoint to count sentences:
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."
}
{
"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
- Use NLP-based APIs: Simple regex won't handle edge cases
- Consider language: Different languages have different rules
- Validate input: Ensure text is properly encoded
- 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