The task of language detection comes into use when you are working on a very large dataset that contains data in different languages. Suppose you want to create an even driven program where the final output depends on the language which the user provides as an input. So it is important to first detect the language of the text provided by the user before taking any action. So, in this article, I will take you through the task of language detection with Python.
Language Detection with Python
As an open-source programming language, Python provides libraries and packages for almost every possible task, as the Python programming community continues to contribute to Python with new libraries, packages, and modules. You can build your machine learning model for the language detection task, but for this article, I will be using the langdetect package in Python which can detect over 55 different languages within a few lines of code.
If you have never used this package before then you can easily install it by using the pip command; pip install langdetect. Now let’s see how to use this package for the task of language detection with Python:
from langdetect import detect text = input("Enter any text in any language: ") print(detect(text))
Output: Enter any text in any language: Salut, je suis Aman Kharwal, j'espère que vous allez très bien. fr
In the above code, I started by importing the detect method from the langdetect package. Then, I am simply asking for user input where the user can enter text in any language. Then I am simply printing the language of the text entered by the user by detecting it using the detect method.
The output received is an abbreviated form of the language (fr means French). So the user can enter text in whatever language the user likes, your program will detect that language but it will give the short form of that language as output. Hope you liked this article on the language detection task with Python. Please feel free to ask your valuable questions in the comments section below.