Language Detection with Polyglot

  • Share this:

Code introduction


This function is used to detect the language of a given text. It first processes the text using Polyglot's Text object and then uses Polyglot's Detector to detect the language of the text and return the language code.


Technology Stack : Polyglot, Text, Detector

Code Type : Function

Code Difficulty : Intermediate


                
                    
from polyglot.text import Text
from polyglot.detect import Detector

def detect_language(text):
    # Initialize the Text object
    polyglot_text = Text(text)
    
    # Get the detector for the text
    detector = Detector(polyglot_text)
    
    # Return the detected language code
    return detector.language.code