Polyglot Language Detection Function

  • Share this:

Code introduction


This function uses the language model from the Polyglot library to determine which language a given word belongs to.


Technology Stack : Polyglot, Language Model

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_word_language(word, language_model):
    """
    使用Polyglot库中的语言模型来判断单词属于哪种语言。

    :param word: str, 单词字符串
    :param language_model: polyglot.downloader.Downloader, Polyglot的语言模型
    :return: str, 单词所属的语言
    """
    language_model.download('language_model')
    detect = language_model.language_model(word)
    return detect