On this article, we’ll see how we will detect the language of an entered textual content in Python utilizing Flask. To detect language utilizing Python we have to set up an exterior library referred to as “langdetect” and “pycountry“. Let’s see tips on how to implement Flask Language Detector App, earlier than implementation we’ll attempt to perceive the fundamental moudules that we’ll want in our Flask Challenge .
pip set up langdetect pip set up pycountry
What’s langdetect in Python?
A langdetect is a Python module for language detection. It’s a port of the language-detection library for Java, which was developed by Nakatani Shuyo. The langdetect module gives a easy interface for detecting the language of a given textual content. It makes use of a set of language profiles to determine the language of the enter textual content.
langdetect Instance
This app makes use of the langdetect library to detect the language of a given sentence. The detect() operate from this library takes a string as enter and returns the detected language as a string. Right here is an instance of tips on how to use the langdetect module to detect the language of a given textual content:
Python3
|
Output:
en
What’s Pycountry in Python?
The pycountry is a Python library for working with ISO nation and language codes. It gives entry to a variety of knowledge, together with details about international locations, languages, currencies, and time zones. The library consists of quite a lot of helpful capabilities and information constructions for working with nation and language codes. For instance, you should utilize the international locations attribute to get a listing of all international locations, and the languages attribute to get a listing of all languages.
Pycountry Instance
Now you possibly can see that your detect languages are displayed in codes (eg: ‘en’, ‘hello’, ‘es’, and so forth). Let’s convert these codes to language names.
Python3
|
Output:
The detected language is: English
Implementation of Flask Language Detector App
To make use of this app, you possibly can ship a GET request to the /detect-language route with a question parameter referred to as sentence containing the sentence to be detected. For instance, in the event you ship a request to http://instance.com/detect-language?sentence=Hellopercent20world, the app will return The detected language is: en (assuming the sentence is in English). You too can use this app in a kind on an internet web page.
index.html
Right here’s an instance of how you can create a easy HTML kind that submits a sentence to the Flask app and shows the detected language:
HTML
|
app.py
Now you possibly can see that your detect languages are displayed in codes (eg: ‘en’, ‘hello’, ‘es’, and so forth). Let’s convert these codes to language names. For doing so, we shall be utilizing `pycountry`. When the consumer submits the shape, the app will obtain a GET request with the sentence question parameter containing the entered sentence. The app will then detect the language of the sentence and return it as a response.
Python3
|
Output:

Enter 1

Enter 2

Enter 3