Language detection using python

Introduction

Language detection using python We will show you how to use Python to identify the language in this project. Using Tkinter, langdetect, langcodes, and language_data, this project was made. The Tkinter package makes it possible to create GUI programs quickly and simply. A library for language detection is called Langdetect. To work with language codes, use the langcodes library. Langcodes utilize the language_data package to obtain language information. This software features a simple button and label design.

Explanation

A basic Tkinter-based GUI with buttons, labels, and an entry are produced by this code. Users may enter text into the application’s straightforward graphical user interface (GUI), click a button to identify the text’s language, and then see the result presented in the GUI. Language identification is accomplished with the langdetect library, while display name extraction is accomplished with the langcodes library. To handle errors that can arise during the language detection process, the program has error handling. In the event of an error, a message alerting the user to the problem is shown in the result label.

Google is Offering Free AI and Cyber Security certification: 2024! Click here

Source Code:

import tkinter as tk
from tkinter import Label, Entry, Button
from langdetect import detect
from langcodes import Language
import language_data

class LanguageDetectorApp:
    def __init__(self, master):
        self.master = master
        master.title("Language Detector")

        self.label = Label(master, text="Enter text:")
        self.label.pack()

        self.text_entry = Entry(master, width=40)
        self.text_entry.pack()

        self.detect_button = Button(master, text="Detect Language", command=self.detect_language)
        self.detect_button.pack()

        self.result_label = Label(master, text="")
        self.result_label.pack()

    def detect_language(self):
        input_text = self.text_entry.get()

        if input_text.strip():  # Check if the input text is not empty or just whitespace
            try:
                language_code = detect(input_text)
                language_name = Language.get(language_code).display_name('en')  # Specify the language for display name
                self.result_label.config(text=f"The detected language is: {language_name}")
            except Exception as e:
                self.result_label.config(text=f"Error during language detection: {e}")
        else:
            self.result_label.config(text="Please enter some text for language detection.")

if __name__ == "__main__":
    root = tk.Tk()
    app = LanguageDetectorApp(root)
    root.mainloop()

Leave a Comment