Python langdetect: choose between one language or the other only

坚强是说给别人听的谎言 提交于 2019-12-05 05:45:07

Option 1

One option would be using the package langid instead. Then you can simply restrict the languages with a method call:

import langid
langid.set_languages(['fr', 'en'])  # ISO 639-1 codes
lang, score = langid.classify('This is a french or english text')
print(lang) # en

Option 2

If you really want to use the langdetect package, you can copy the package folder (if you're not sure where it is, use python -m site --user-site) and remove the profiles you don't need from the folder langdetect\profiles.

This is not a very dynamic solution though.

The way I'd do this is to use detect_langs, which returns a list of Language objects with probabilities, and then iterate through this list, returning the language if one of the options is English or French, or None if this isn't the case. This function works well for this purpose:

from langdetect import detect_langs

def englishOrFrench(string):
    res = detect_langs(string)
    for item in res:
        if item.lang == "fr" or item.lang == "en":
            return item.lang
    return None

print(englishOrFrench("Bonjour"))              # fr
print(englishOrFrench("The quick brown fox"))  # en
print(englishOrFrench("Hallo, mein Freund"))   # None
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!