Emir Hıfsı Öztoprak 3rd Party API Documentation - bounswe/2021SpringGroup1 GitHub Wiki
3rd party API documentation (Detect Language API)
I researched online about 3rd party APIs that would be helpful in our community based project, and I stumbled upon Detect Language API. I think it will be quite useful in later parts of our project when our app gets a lot of users. Moderators of a community may want to keep their community exclusively in one language, for example the moderator of a community that is about the events in England may want to make sure that the posts in that community are exclusively English. So an automatic language detection tool may be valuable in these kind of scenarios.
Now I will talk about the documentation and usage of this API. Detect Language API requires authentication with an API key, which you can get by registering to their website. It takes 1 parameter 'q', which is the string you want to detect languages in. Thankfully, the authentication and the request can be done in a single line using requests.post() function in Python. You can see below the code I wrote where I sent a POST request to the third party API:
myResponse = requests.post(str(DETECT_LANGUAGE_BASE_URL), auth=(str(DETECT_LANGUAGE_KEY), '12345'), data={'q': 'Hello world'})
NOTE: The password is trivial, it is not checked so I simply wrote '12345' as a placeholder. You can leave it blank as well using an empty string ("").
To give more detail about the above code, DETECT_LANGUAGE_BASE_URL = "https://ws.detectlanguage.com/0.2/detect/" and DETECT_LANGUAGE_KEY is the key you get by registering to the website. And I also wrote 'Hello world' as parameter in the POST request in the example. The output of that request will be in JSON format which you can see below:
{
"data": {
"detections": [
{
"language": "en",
"isReliable": true,
"confidence": 11.94
}
]
}
}
While writing my own API functions, I ignored the 'isReliable' and 'confidence' properties, I only used the 'language' property for each language detection. There can be multiple language detections, you can see an example JSON response below:
{
"data": {
"detections": [
{
"language": "pt",
"isReliable": true,
"confidence": 2.82
},
{
"language": "tr",
"isReliable": false,
"confidence": 2.82
},
{
"language": "it",
"isReliable": false,
"confidence": 2.82
}
]
}
}