The way I do this is to use detect_langsthat returns a list of objects Languagewith probabilities, and then iterates over this list, returning the language if one of the options is English or French, or Noneif it is not. 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"))
print(englishOrFrench("The quick brown fox"))
print(englishOrFrench("Hallo, mein Freund"))
source
share