Member-only story
Detect the language using Python
2 min readAug 28, 2024
pip install langdetect
from langdetect import detect, DetectorFactory
DetectorFactory.seed = 0
text = input("Enter a senetence to detect: ")
detected_language = detect(text)
print(f"The detected language is: {detected_language}")
The detected language is: ur
Let’s break down the code:
1. Importing Modules
from langdetect import detect, DetectorFactory
detect
: This function from thelangdetect
library is used to detect the language of a given text. It returns a language code (e.g.,'en'
for English,'fr'
for French).DetectorFactory
: This class is used to control certain aspects of the language detection process, such as setting a seed for consistent results.
2. Setting a Seed
DetectorFactory.seed = 0
DetectorFactory.seed = 0
: This line sets a seed value for the random number generator used by the language detection algorithm. By setting the seed, you ensure that the language detection results are consistent every time you run the code, even with the same input. This is useful for reproducibility, especially when you run the code multiple times and want to get the same result each time.