Python langdetect: select only one language or another

I use langdetectto determine the language of the set of strings that I know, either in English or in French.

Sometimes he langdetecttells me that the language is Romanian for the string that I know in French.

How can I make a langdetectchoice only in English or French, and not in all other languages?

Thank!

+4
source share
1 answer

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"))              # fr
print(englishOrFrench("The quick brown fox"))  # en
print(englishOrFrench("Hallo, mein Freund"))   # None
+3
source

All Articles