A byte-level language detection model supporting 102 languages
Project description
Lark - Byte-Level Language Detection
Lark is a byte-level language detection model that supports 102 languages with high accuracy and efficiency.
🚀 Features
- 102 Languages: Supports a wide range of languages including English, Chinese, Japanese, Spanish, French, etc.
- Byte-Level Processing: No vocabulary limitations, handles any Unicode text
- High Accuracy: State-of-the-art performance on language detection tasks
- Fast Inference: Optimized for both CPU and GPU
- Easy Integration: Simple API for both batch and single text processing
📦 Installation
From PyPI (Recommended)
pip install lark-language-detector
From Source
git clone https://github.com/jiangchengchengNLP/Lark.git
cd Lark
pip install -e .
🎯 Quick Start
Basic Usage
from lark import LarkDetector
# Initialize detector
detector = LarkDetector()
# Detect language for single text
text = "Hello, how are you today?"
language, confidence = detector.detect(text)
print(f"Language: {language}, Confidence: {confidence:.4f}")
# Batch detection
texts = [
"Hello world!",
"今天天气真好",
"こんにちは、元気ですか?"
]
results = detector.detect_batch(texts)
for text, (lang, conf) in zip(texts, results):
print(f"'{text}' -> {lang} ({conf:.4f})")
Advanced Usage
from lark import LarkDetector
detector = LarkDetector()
# Get top-k predictions
text = "This is a sample text"
prediction, confidence, top_k = detector.detect_with_topk(text, k=5)
print(f"Prediction: {prediction} (Confidence: {confidence:.4f})")
print("Top 5 predictions:")
for i, item in enumerate(top_k):
print(f" {i+1}. {item['language']:8} - {item['probability']:.4f}")
# Confidence threshold
language, confidence, top_k = detector.detect_with_confidence(
text,
confidence_threshold=0.7
)
if language == "unknown":
print(f"Low confidence: {confidence:.4f}")
else:
print(f"Detected: {language} (Confidence: {confidence:.4f})")
📊 Supported Languages
Lark supports 102 languages including:
- European: English, Spanish, French, German, Italian, Russian, etc.
- Asian: Chinese, Japanese, Korean, Hindi, Arabic, Thai, etc.
- African: Swahili, Yoruba, Zulu, etc.
- Others: And many more...
See the full list in all_dataset_labels.json.
🏗️ Model Architecture
Lark uses a novel byte-level architecture:
- Byte Encoder: Converts raw bytes to contextual representations
- Boundary Predictor: Identifies segment boundaries using Gumbel-Sigmoid
- Segment Decoder: Processes segments for language classification
This architecture enables:
- No vocabulary limitations
- Robust handling of mixed-language text
- Efficient processing of long documents
📈 Performance
| Metric | Value |
|---|---|
| Accuracy | >95% on test set |
| Inference Speed | ~1ms per text (CPU) |
| Model Size | ~15MB |
| Supported Languages | 102 |
🔧 API Reference
LarkDetector Class
class LarkDetector:
def __init__(self, model_path: str = None, labels_path: str = None):
"""Initialize the language detector"""
def detect(self, text: str) -> Tuple[str, float]:
"""Detect language for single text"""
def detect_batch(self, texts: List[str]) -> List[Tuple[str, float]]:
"""Batch language detection"""
def detect_with_topk(self, text: str, k: int = 5) -> Tuple[str, float, List[Dict]]:
"""Get top-k predictions with probabilities"""
def detect_with_confidence(self, text: str, confidence_threshold: float = 0.5) -> Tuple[str, float, List[Dict]]:
"""Detection with confidence threshold"""
🛠️ Development
Setup Development Environment
git clone https://github.com/jiangchengchengNLP/Lark.git
cd Lark
pip install -e ".[dev]"
Running Tests
python -m pytest tests/
Building from Source
python setup.py sdist bdist_wheel
📝 Citation
If you use Lark in your research, please cite:
@software{lark2024,
title={Lark: Byte-Level Language Detection},
author={Jiang Chengcheng},
year={2024},
url={https://github.com/jiangchengchengNLP/Lark}
}
🤝 Contributing
We welcome contributions! Please see CONTRIBUTING.md for details.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Thanks to the open-source community for datasets and tools
- Inspired by modern language detection approaches
- Built with PyTorch and Hugging Face ecosystem
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lark_ld-1.0.0.tar.gz.
File metadata
- Download URL: lark_ld-1.0.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d7d988b73b79eb6d2228d0a802ddf0b81bafc7f345867c70d0b0669d93d741c
|
|
| MD5 |
6e782d8aa568a111532e479be24f24b0
|
|
| BLAKE2b-256 |
9ee5384e7b18eac60305051199c41acce98f12a35d28343ae3168e173d9724fb
|
File details
Details for the file lark_ld-1.0.0-py3-none-any.whl.
File metadata
- Download URL: lark_ld-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a946889c9c217cda14043ae2938b0a97d58d3056de499f893eb3412533ddb270
|
|
| MD5 |
0238e0e095d5ed83b61f426dc3f1b596
|
|
| BLAKE2b-256 |
af5519788fa6f87eb111698c4d03922312086b3a96edbdb2e43a0e314e1c59fa
|