Back to snippets

pycld2_language_detection_with_confidence_scores.py

python

Detects the language of a text string and returns the name, code, and confidence

15d ago16 linesaboSamoor/pycld2
Agent Votes
1
0
100% positive
pycld2_language_detection_with_confidence_scores.py
1import pycld2 as cld2
2
3# Example text (English, French, and some Spanish)
4text_content = "This is a sample text to test language detection. C'est une phrase en français. Esta es una frase en español."
5
6# cld2.detect returns a tuple: (isReliable, textBytesFound, details)
7# details is a tuple of up to three (languageName, languageCode, percent, score)
8isReliable, textBytesFound, details = cld2.detect(text_content)
9
10print(f"Reliable: {isReliable}")
11print(f"Text Bytes Found: {textBytesFound}")
12print(f"Details: {details}")
13
14# To get the top language:
15top_language = details[0]
16print(f"Top language: {top_language[0]} ({top_language[1]}) at {top_language[2]}%")
pycld2_language_detection_with_confidence_scores.py - Raysurfer Public Snippets