Random Language Information Retrieval

  • Share this:

Code introduction


This function randomly selects a language from the list of languages supported by the Lingua library and returns the name of the language, its native name, the total number of words, and the number of unique words.


Technology Stack : Lingua library

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import random
from lingua import get_languages, count_words

def random_language_info(language_code):
    """
    This function returns information about a random language from the list of languages supported by the Lingua library.
    It provides the name of the language, its native name, the number of words in the language, and the number of unique words.
    """
    languages = get_languages()
    selected_language = random.choice(languages)
    language_name = selected_language['name']
    native_name = selected_language['native']
    word_count = count_words(language_code)
    unique_word_count = len(word_count)

    return {
        "language_name": language_name,
        "native_name": native_name,
        "word_count": word_count,
        "unique_word_count": unique_word_count
    }                
              
Tags: