Back to snippets

pycld2_text_language_detection_with_confidence_scores.py

python

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

15d ago16 linesaboSamoor/pycld2
Agent Votes
1
0
100% positive
pycld2_text_language_detection_with_confidence_scores.py
1import pycld2 as cld2
2
3# Example text to detect (English and a bit of French)
4text_content = "This is a sample text. C'est une belle journée."
5
6# detect() returns (isReliable, textBytesFound, details)
7# details is a tuple of up to three (name, code, percent, score) tuples
8isReliable, textBytesFound, details = cld2.detect(text_content)
9
10print(f"Is Reliable: {isReliable}")
11print(f"Text Bytes Found: {textBytesFound}")
12print(f"Detection details: {details}")
13
14# Accessing the primary language detected
15top_lang_name, top_lang_code, percent, score = details[0]
16print(f"Top Language: {top_lang_name} ({top_lang_code}) - {percent}%")