Language Detection of a Sentence using Python With Source Code

Language Detection using Python

Introduction

Hello Curious Coders,

In this project, we are going to learn how to detect the language in text using Python with the help of libraries like long detect. This parses through the basic functions of langdetect library and is going to detect the sentence of a language.

Source Code

Get Discount on Top Educational Courses

Brand NameDiscount InformationCoupon Codes Link
Educative.io20% discount on Educative courses and plans
W3Schools20% discount on W3Schools courses
KodeKloud10% discount on KodeKloud courses and plans
GeeksforGeeks30% discount on GeeksforGeeks courses
Target Test Prep20% discount on Target Test Prep
Coding Ninjas₹5000 discount on Coding Ninjas courses
Skillshare40% discount on Skillshare
DataCamp50% discount on DataCamp
365 Data Science57% discount on 365 Data Science Plans
Get SmarterFlat 20% discount on Get Smarter courses
SmartKeedaFlat 40% discount on SmartKeeda courses
StackSocial20% discount on StackSocial courses
				
					# Importing langdetect library
from langdetect import detect

# Function to detect the language of a sentence
def detect_language(text):
    return detect(text)

# The Sentence
text = "வணக்கம்"

# Dictionary of some famous Languages and their respective codes
codes={
 'ar': 'Arabic', 'et': 'Armenian', 'art': 'Artificial Langauge',
 'sq': 'Albanian','bn': 'Bangla', 'bh': 'Bhojpuri',
'bul': 'Bulgarian', 'cai': 'Central American Indian Language',
 'cze': 'Caech', 'dan': 'Danish', 'ger': 'German', 'eg': 'Egyptian', 'en': 'English',
 'fre': 'french', 'gon': 'Gondi', 'grc': 'Greek', 'gsw': 'Swiss German', 'hi': 'Hindi',
 'ind': 'Indonesian', 'ita': 'Italian', 'ja': 'Japanese', 'kn': 'Kannada',
 'kas': 'Kashmiri', 'geo': 'Georgian', 'kor': 'Korean', 'lat': 'Latin',
 'mar': 'Marathi', 'mni': 'Manipuri', 'mul': 'Multiple Languages', 'dut': 'Dutch',
'te': 'Telugu', 'ta': 'Tamil','cy':'Welsh'}

print("The language of the text is:", codes[detect_language(text)])
				
			

Code Explanation

  • First we imported the langdetect package
  • Next we created a function which takes the sentence as an argument and detects the language of the sentence using detect() function of langdetect package.
  • Later we created aa dictionary which consists of langauges and their respective codes to make sure the output looks good as the detect() function is going to return the lang codes.

Output

The language of the text is: Tamil

Find More Projects

URL Shortener Using Python Django Introduction: Long URLs can be shortened into short, shareable links with the help of the URL Shortener …

User Authentication System Using Python Django Introduction: The implementation of safe and adaptable user authentication in Django is the main goal of …

The E-Learning System using Java with a Graphical User Interface (GUI) Introduction The E-Learning System is developed using Java (with a Graphical …

Weather App Using Python Django Introduction: When a user enters the name of a city, the Weather App retrieves current weather information. …

Quiz App Using Python Django Introduction: Users can take quizzes in a variety of subjects, see their results, and monitor their progress …

resume screener in python using python introduction The hiring process often begins with reviewing numerous resumes to filter out the most suitable …

Get Huge Discounts
More Python Projects