Skip to main content

Tiny examples and utilities for language detection

Project description

CI PyPI Lint

language-detection-nlp

Small examples and utilities for simple language detection tasks. The repository contains:

  • A tiny wrapper using langdetect in src/langdetector/.
  • An example sklearn-based detector in examples/language_detection.py (toy dataset).
  • Tests in tests/ that exercise the langdetector package.

Quick start

  1. Create and activate a virtual environment (optional):
python3 -m venv .venv
source .venv/bin/activate
  1. Install dependencies:
python3 -m pip install -r requirements.txt
  1. Install the package (editable) so langdetector imports 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 pycountry or 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

language_detection_nlp-0.0.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

language_detection_nlp-0.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

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

Hashes for language_detection_nlp-0.0.0.tar.gz
Algorithm Hash digest
SHA256 2c9c17d2dfc4e5e617f26f52a078131b3e24b1e7976afb16825dfa5188ef0f61
MD5 535f87812b675ff30f8b6c21d47be401
BLAKE2b-256 800cc3802a1541744bbab0a98a56ad99442e64a69c1546272779f3dd2e7a6baf

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file language_detection_nlp-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for language_detection_nlp-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32b8670be4b2ba6baebd95028a992145f3f2f275e70cfa7a6129b370e94a2eb6
MD5 e52c4f4f144f33703db32885b52a69e6
BLAKE2b-256 53aa759a4c151cd3669bce4ea7ce479051c79bfd2d4fc7ac5b018dc2a7d6afd0

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page