Code for How to Make a Language Detector in Python Tutorial


View on Github

language_detector_cli_1.py

""""
    THIS SCRIPT IS USING langdetect
"""

# import the detect function from langdetect
from langdetect import detect

# openning the txt file in read mode
sentences_file = open('sentences.txt', 'r')

# creating a list of sentences using the readlines() function
sentences = sentences_file.readlines()

# a function for detection language
def detect_langauage(sentence, n):
    """try and except block for catching exception errors"""
    # the try will run when everything is ok
    try:
        # checking if the sentence[n] exists
        if sentences[n]:
            # creating a new variable, the strip() function removes newlines
            new_sentence = sentences[n].strip('\n')
            print(f'The language for the sentence "{new_sentence}" is {detect(new_sentence)}')
    # this will catch all the errors that occur  
    except:
        print(f'Sentence does not exist')
           
# printing the the number of sentences in the sentences.txt   
print(f'You have {len(sentences)} sentences')

# this will prompt the user to enter an integer
number_of_sentence = int(input('Which sentence do you want to detect?(Provide an integer please):'))

# calling the detect_langauage function
detect_langauage(sentences_file, number_of_sentence)

language_detector_cli_2.py

""""
    THIS SCRIPT IS USING langid
"""
import langid

# openning the txt file in read mode
sentences_file = open('sentences.txt', 'r')

# creating a list of sentences using the readlines() function
sentences = sentences_file.readlines()

# looping through all the sentences in thesentences.txt file
for sentence in sentences:
    # detecting the languages for the sentences
    lang = langid.classify(sentence)
    # formatting the sentence by removing the newline characters
    formatted_sentence = sentence.strip('\n')
   
    print(f'The sentence "{formatted_sentence}" is in {lang[0]}')

language_detector_cli_3.py

""""
    THIS SCRIPT IS USING googletrans
"""

# importing the Translator function from googletrans
from googletrans import Translator

# initializing the translator object
translator = Translator()

# openning the txt file in read mode
sentences_file = open('sentences.txt', 'r')

# creating a list of sentences using the readlines() function
sentences = sentences_file.readlines()

# a function for detection language
def detect_langauage(sentence, n):
    """try and except block for catching exception errors"""
    # the try will run when everything is ok
    try:
        # checking if the sentence[n] exists
        if sentences[n]:
            # creating a new variable, the strip() function removes newlines
            new_sentence = sentences[n].strip('\n')
            # detecting the sentence language using the translator.detect()
            # .lang extract the language code
            detected_sentence_lang = translator.detect(new_sentence).lang 
            print(f'The language for the sentence "{new_sentence}" is {detected_sentence_lang}')
    # this will catch all the errors that occur  
    except:
        print(f'Make sure the sentence exists or you have internet connection')
           
       
print(f'You have {len(sentences)} sentences')
# this will prompt the user to enter an integer
number_of_sentence = int(input('Which sentence do you want to detect?(Provide an integer please):'))
# calling the detect_langauage function
detect_langauage(sentences_file, number_of_sentence)

language_detector_cli_4.py

""""
    THIS SCRIPT IS USING language_detector
"""
# importing the language detector
from language_detector import detect_language

# definig the function for detecting the language
# the function takes text as an argument
def detectLanguage(text):
    # detecting the language using the detect_language function
    language = detect_language(text)
    print(f'"{text}" is written in {language}')

# an infinite while while loop
while True:
    
    # this will prompt the user to enter options
    option = input('Enter 1 to detect language or 0 to exit:')
    
    if option == '1':
        # this will prompt the user to enter the text
        data = input('Enter your sentence or word here:')
        # calling the detectLanguage function
        detectLanguage(data)
    
    # if option is 0 break the loop   
    elif option == '0':
        print('Quitting........\nByee!!!')
        break
    # if option isnt 1 or 0 then its invalid 
    else:
        print('Wrong input, try again!!!')