Python language detection and translation program
In this article, you will learn about language detection and translation program using the Python programming language. Python provides various modules for detecting languages, like langdetect, langrid, textblob, and spacy-langdetect.
The thought behind language identification depends on the recognition of the character among the expressions and words in the content. The primary guideline is to distinguish regularly utilised words like "to", "of" in English. It is also very important when your website serves in multiple countries.
TextBlob Language Detection and Translation
The TextBlob is a natural language processing Python (2 and 3) library. It performs different operations on textual data, such as noun phrase extraction, sentiment analysis, classification, translation, etc. Here is the command to install the TextBlob library using the pip tool -
pip install textblob
TextBlob Program to Detect Language
The detect_language() of TextBlob library detects the language using the Google Translate API. It means an active internet connection is required while performing detection and also translation.
from textblob import TextBlob
L = ["Many many thanks to you",
"Merci beaucoup à vous",
"Muchas muchas gracias a ti",
"ありがとうございます",
"आपको बहुत बहुत धन्यवाद",
"많은 감사합니다",
]
for i in L:
# Language Detection
lang = TextBlob(i)
print(lang.detect_language())
TextBlob Program to Translate Language
The translate() method translates the blob language to another language using Google Translate API.
from textblob import TextBlob
L = ["Merci beaucoup à vous",
"Muchas muchas gracias a ti",
"ありがとうございます",
"आपको बहुत बहुत धन्यवाद",
"많은 감사합니다",
]
for i in L:
# Language Translation
lang = TextBlob(i)
print(lang.translate(to='en'))
Detect Language using langdetect
The langdetect library is a port of Google’s language-detection library and is supported in Python versions 2.7, 3.4+. It supports 55 languages. This module doesn't come with Python's standard utility modules. So, it needs to be installed externally. Here is the command to install the langdetect module-
pip install langdetect
The detect() method of this module detects the language.
from langdetect import detect
L = ["Laisse moi aussi t’aider",
"Ben de sana yardım edeyim",
"Позвольте мне также помочь вам",
"Lass mich dir auch helfen",
"나도 도와 줄게",
]
for i in L:
# Language Translation
print(detect(i))
Detect Language using spacy-langdetect
The spacy-langdetect library uses langdetect to detect languages on spaCy's Doc and Span objects. spaCy is a free, open-source library for Natural Language Processing in Python. It overcomes some issues of the langdetect module. To use the Spacy library to detect the language of a string of text, we need to install spacy and spacy-langdetect modules. These are the commands to install -
pip install spacy
pip install spacy_langdetect
spacy-langdetect program to Detect language
Here, we have loaded the model by providing full package name 'en_core_web_sm'.
# spacy library
import spacy
from spacy_langdetect import LanguageDetector
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe(LanguageDetector(), name='language_detector', last=True)
text = 'Laisse moi aussi t’aider'
doc = nlp(text)
# document level language detection.
print(doc._.language)
Related Articles
Remove last element from list PythonAdaptive Thresholding in Python OpenCV
How to find the stop words in nltk Python
Python OpenCV Overlaying or Blending Two Images
Python Spell Checker Program
Python remove punctuation from string
How to convert Excel to CSV Python Pandas
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml