Monday, February 6, 2023
HomeSoftware DevelopmentFlask - Language Detector App

Flask – Language Detector App


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

from langdetect import detect

  

textual content = "This can be a pattern textual content in English."

  

language = detect(textual content)

print(language)

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

from langdetect import detect

import pycountry

  

def get_language_name(language_code):

  language = pycountry.languages.get(alpha_2=language_code)

  return language.title

  

  

textual content = "This can be a pattern textual content in English."

  

language = detect(textual content)

print('The detected language is: {}'.format(get_language_name(language)))

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

<!DOCTYPE html>

<html>

<head>

<title>Web page Title</title>

</head>

<physique>

<h2>Welcome To GFG</h2>

<p>Detect your Langauge</p>

  <kind motion="/detect-language" methodology="GET">

    <label for="sentence">Enter a sentence:</label>

    <enter sort="textual content" id="sentence" title="sentence">

    <enter sort="submit" worth="Detect language">

</kind>

  

</physique>

</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

from flask import Flask, request, render_template

from langdetect import detect

  

import pycountry

  

  

def get_language_name(language_code):

    language = pycountry.languages.get

            (alpha_2=language_code)

    return language.title

  

  

app = Flask(__name__)

  

  

@app.route('/')

def house():

    return render_template("house.html")

  

  

@app.route('/detect-language')

def detect_language():

    sentence = request.args.get('sentence')

    

    language = detect(sentence)  

    return 'The detected language is: {}'

          .format(get_language_name(language))

  

  

if __name__ == '__main__':

    app.run(debug=True)

Output:

Enter 1

Enter 2

Enter 3

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments