Language Detection API
The language detection endpoint identifies the language of a given text and returns a confidence score. This is useful for routing text into language-specific processing pipelines before calling other Sapling APIs.
Sample Code
- cURL
- JavaScript
- Python
curl -X POST https://api.sapling.ai/api/v1/langdetect \
-H "Content-Type: application/json" \
-d '{"key":"<api-key>", "text":"This is an English sentence."}'
import axios from 'axios';
async function run(text) {
try {
const response = await axios.post(
'https://api.sapling.ai/api/v1/langdetect',
{
key: '<api-key>',
text,
},
);
const {status, data} = response;
console.log({status});
console.log(JSON.stringify(data, null, 4));
} catch (err) {
const { msg } = err.response.data;
console.log({err: msg});
}
}
run('This is an English sentence.');
import requests
from pprint import pprint
response = requests.post(
"https://api.sapling.ai/api/v1/langdetect",
json={
"key": "<api-key>",
"text": "This is an English sentence."
}
)
if 200 <= response.status_code < 300:
pprint(response.json())
else:
print('Error: ', response.status_code, response.text)
Sample Response
{
"lang": "en",
"conf": 0.99
}
Request Parameters
POST to https://api.sapling.ai/api/v1/langdetect
key: String
32-character API key. Can also be supplied via the Authorization header as a bearer token; if both are provided, the key parameter takes precedence.
text: String
Text to identify the language for.
Response Parameters
The language detection response contains two fields:
lang: The detected language code.conf: Confidence score for the detection result.