@@ -28,6 +28,7 @@ BNLP is a natural language processing toolkit for Bengali Language. This tool wi
2828- [ Batch Processing] ( #batch-processing )
2929- [ Async Model Loading] ( #async-model-loading )
3030- [ Spell Checking] ( #spell-checking )
31+ - [ Language Detection] ( #language-detection )
3132
3233## Installation
3334
@@ -327,6 +328,104 @@ prob = checker.word_probability("আমি")
327328print (prob) # Higher = more common word
328329```
329330
331+ ## Language Detection
332+
333+ Fast and accurate language detection using FastText's language identification model. Supports 176 languages including Bengali.
334+
335+ ``` bash
336+ # Install with language detection support
337+ pip install bnlp_toolkit[langdetect]
338+ ```
339+
340+ ``` python
341+ from bnlp import LanguageDetector, detect_language, is_bengali
342+
343+ # Create detector (downloads model automatically on first use)
344+ detector = LanguageDetector()
345+
346+ # Detect language
347+ result = detector.detect(" আমি বাংলায় গান গাই" )
348+ print (result.language) # 'bn'
349+ print (result.confidence) # ~0.99
350+ print (result.is_bengali) # True
351+
352+ # Get multiple predictions
353+ result = detector.detect(" আমি বাংলায় গান গাই" , top_k = 3 )
354+ print (result.all_predictions)
355+ # Output: [('bn', 0.99), ('hi', 0.005), ...]
356+
357+ # Check if text is Bengali
358+ print (detector.is_bengali(" আমি বাংলায় গান গাই" )) # True
359+ print (detector.is_bengali(" Hello world" )) # False
360+
361+ # Detect English
362+ result = detector.detect(" Hello, this is English text" )
363+ print (result.language) # 'en'
364+ print (result.is_bengali) # False
365+ ```
366+
367+ ### Convenience Functions
368+
369+ ``` python
370+ from bnlp import detect_language, is_bengali
371+
372+ # Quick language detection
373+ result = detect_language(" আমি বাংলায় গান গাই" )
374+ print (result.language) # 'bn'
375+
376+ # Quick Bengali check
377+ print (is_bengali(" আমি বাংলায় গান গাই" )) # True
378+ print (is_bengali(" Hello world" )) # False
379+ ```
380+
381+ ### Batch Detection
382+
383+ ``` python
384+ from bnlp import LanguageDetector
385+
386+ detector = LanguageDetector()
387+
388+ texts = [" আমি বাংলায় গান গাই" , " Hello world" , " Bonjour le monde" ]
389+ results = detector.detect_batch(texts)
390+
391+ for text, result in zip (texts, results):
392+ print (f " { text[:20 ]} ... -> { result.language} ( { result.confidence:.2f } ) " )
393+ ```
394+
395+ ### Mixed Language Detection
396+
397+ Detect code-mixed text (e.g., Bengali-English):
398+
399+ ``` python
400+ from bnlp import LanguageDetector
401+
402+ detector = LanguageDetector()
403+
404+ mixed_text = " আমি today বাংলায় গান গাই। This is mixed text।"
405+ languages = detector.detect_mixed(mixed_text)
406+ print (languages)
407+ # Output: {'bn': 0.5, 'en': 0.5}
408+ ```
409+
410+ ### Language Detection Options
411+
412+ ``` python
413+ from bnlp import LanguageDetector
414+
415+ # Custom confidence threshold
416+ detector = LanguageDetector(threshold = 0.7 )
417+
418+ # Use your own model file
419+ detector = LanguageDetector(model_path = " /path/to/lid.176.ftz" )
420+
421+ # Disable auto-download
422+ detector = LanguageDetector(auto_download = False )
423+
424+ # Get language name
425+ print (detector.get_language_name(' bn' )) # 'Bengali'
426+ print (detector.get_language_name(' en' )) # 'English'
427+ ```
428+
330429## Documentation
331430Full documentation are available [ here] ( https://sagorbrur.github.io/bnlp/ )
332431
0 commit comments