Tiny examples and utilities for language detection
Project description
language-detection-nlp
Small examples and utilities for simple language detection tasks. The repository contains:
- A tiny wrapper using
langdetectinsrc/langdetector/. - An example sklearn-based detector in
examples/language_detection.py(toy dataset). - Tests in
tests/that exercise thelangdetectorpackage.
Quick start
- Create and activate a virtual environment (optional):
python3 -m venv .venv
source .venv/bin/activate
- Install dependencies:
python3 -m pip install -r requirements.txt
- Install the package (editable) so
langdetectorimports work:
python3 -m pip install -e .
Run tests
python3 -m pytest -q
Run the sklearn example
python3 examples/language_detection.py
Run the langdetect CLI
python3 -m langdetector.cli --text "This is a test"
Notes
- The sklearn example uses a toy dataset for demonstration. For production tasks, use larger labeled corpora and proper preprocessing.
- Consider using
pycountryor similar to map language codes to full names if needed.
Sample data - sentences with their language labels
texts = [ "Hello, how are you?", # English "What is your name?", # English "Bonjour, comment ça va?", # French "Quel est ton nom?", # French "Hola, ¿cómo estás?", # Spanish "¿Cuál es tu nombre?", # Spanish "Hallo, wie geht's dir?", # German "Wie heißt du?", # German "Ciao, come stai?", # Italian "Come ti chiami?", # Italian ] labels = [ "English", "English", "French", "French", "Spanish", "Spanish", "German", "German", "Italian", "Italian" ]
Split data
X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.3, random_state=42)
Feature extraction
vectorizer = TfidfVectorizer() X_train_vec = vectorizer.fit_transform(X_train) X_test_vec = vectorizer.transform(X_test)
Train classifier
model = MultinomialNB() model.fit(X_train_vec, y_train)
Test accuracy
y_pred = model.predict(X_test_vec) accuracy = accuracy_score(y_test, y_pred) print(f"Language detection accuracy: {accuracy:.2f}")
Optional: Predict language for new sentences
sample_sentences = ["Wie geht es Ihnen?", "Good morning!", "¿Dónde está la estación?"] sample_vec = vectorizer.transform(sample_sentences) predictions = model.predict(sample_vec) for i, sentence in enumerate(sample_sentences): print(f"'{sentence}' predicted as: {predictions[i]}")
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 language_detection_nlp-0.0.0.tar.gz.
File metadata
- Download URL: language_detection_nlp-0.0.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c9c17d2dfc4e5e617f26f52a078131b3e24b1e7976afb16825dfa5188ef0f61
|
|
| MD5 |
535f87812b675ff30f8b6c21d47be401
|
|
| BLAKE2b-256 |
800cc3802a1541744bbab0a98a56ad99442e64a69c1546272779f3dd2e7a6baf
|
Provenance
The following attestation bundles were made for language_detection_nlp-0.0.0.tar.gz:
Publisher:
publish.yml on swaranika27nath-spec/language-detection-nlp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
language_detection_nlp-0.0.0.tar.gz -
Subject digest:
2c9c17d2dfc4e5e617f26f52a078131b3e24b1e7976afb16825dfa5188ef0f61 - Sigstore transparency entry: 775134435
- Sigstore integration time:
-
Permalink:
swaranika27nath-spec/language-detection-nlp@6e15f5ae634d8ce0b636464eb3b90a36936d5e7c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/swaranika27nath-spec
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6e15f5ae634d8ce0b636464eb3b90a36936d5e7c -
Trigger Event:
release
-
Statement type:
File details
Details for the file language_detection_nlp-0.0.0-py3-none-any.whl.
File metadata
- Download URL: language_detection_nlp-0.0.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32b8670be4b2ba6baebd95028a992145f3f2f275e70cfa7a6129b370e94a2eb6
|
|
| MD5 |
e52c4f4f144f33703db32885b52a69e6
|
|
| BLAKE2b-256 |
53aa759a4c151cd3669bce4ea7ce479051c79bfd2d4fc7ac5b018dc2a7d6afd0
|
Provenance
The following attestation bundles were made for language_detection_nlp-0.0.0-py3-none-any.whl:
Publisher:
publish.yml on swaranika27nath-spec/language-detection-nlp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
language_detection_nlp-0.0.0-py3-none-any.whl -
Subject digest:
32b8670be4b2ba6baebd95028a992145f3f2f275e70cfa7a6129b370e94a2eb6 - Sigstore transparency entry: 775134438
- Sigstore integration time:
-
Permalink:
swaranika27nath-spec/language-detection-nlp@6e15f5ae634d8ce0b636464eb3b90a36936d5e7c -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/swaranika27nath-spec
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6e15f5ae634d8ce0b636464eb3b90a36936d5e7c -
Trigger Event:
release
-
Statement type: