Registry / ai-ml / spacy-language-detection

spacy-language-detection

JSON →
library0.2.1pypypi✓ verified 22d ago

spacy-language-detection is a fully customizable language detection component for spaCy pipelines, designed for spaCy 3.0 and later. It was forked from `spacy-langdetect` to address issues and ensure compatibility with modern spaCy versions. The library enables detection of language at both document and sentence levels. The current version is 0.2.1, with releases typically focused on bug fixes and ongoing spaCy compatibility.

pip install spacy-language-detection
INSTALL
IMPORT
SIG · SPACY-LANGUAGE-DET
S
spacy-language-detection
ai-mlpythonv0.2.1
Install
13.7s avg
Import
2925ms
Disk
309MB
Pass rate
8/ 10
Env Coverage8 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 14.9s
py 3.11
✓ —
✓ 13.4s
py 3.12
✓ —
✓ 13.2s
py 3.13
✓ —
✓ 13.1s
py 3.9
✕ build_error
✕ build_error
309MB installed
● package 309MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LanguageDetector
from spacy_language_detection import LanguageDetector
from spacy_language_detection import LanguageDetector

This quickstart demonstrates how to add the `spacy-language-detection` component to a spaCy 3.x pipeline. It registers a custom language detector factory and adds it as the last component in the pipeline. It then processes a multilingual text and prints the detected language for the entire document and each individual sentence. Ensure you have a spaCy model (e.g., `en_core_web_sm`) downloaded before running.

import spacy from spacy.language import Language from spacy_language_detection import LanguageDetector def get_lang_detector(nlp, name): return LanguageDetector(seed=42) # Using a seed for reproducibility nlp_model = spacy.load("en_core_web_sm") Language.factory("language_detector", func=get_lang_detector) nlp_model.add_pipe('language_detector', last=True) text = "This is English text. Er lebt mit seinen Eltern und seiner Schwester in Berlin. Yo me divierto todos los días en el parque." doc = nlp_model(text) print(f"Document language: {doc._.language}") for i, sent in enumerate(doc.sents): print(f"Sentence {i+1}: {sent} -> {sent._.language}")
Debug
Known issues
breakingFor spaCy 3.x, adding custom pipeline components requires using `Language.factory` to register a component factory, then `nlp.add_pipe` with the factory name. Direct instantiation like `nlp.add_pipe(LanguageDetector())` (common in spaCy 2.x and older `spacy-langdetect`) will not work.
fix
Use `Language.factory('component_name', func=your_factory_function)` to register the component, then `nlp.add_pipe('component_name', ...)` to add it to the pipeline. Refer to the quickstart example.
affects: >=0.2.0 (for spacy-language-detection); spaCy >= 3.0
gotchaThe underlying `langdetect` library (used by default) is non-deterministic without a seed. For reproducible results, pass a `seed` argument to the `LanguageDetector` constructor.
fix
Initialize `LanguageDetector` with a `seed` parameter, e.g., `LanguageDetector(seed=42)`.
affects: <0.2.0 (for missing seed arg), all versions (for langdetect non-determinism)
breakingToken-level language detection was removed in version 0.2 of `spacy-language-detection` to simplify the component and focus on Doc and Span level detection.
fix
If token-level detection is required, consider using an older version of `spacy-langdetect` (the predecessor project) or implementing custom token-level logic.
affects: >=0.2.0
deprecatedThis library (`spacy-language-detection`) is a fork of the original `spacy-langdetect` project, created to address compatibility issues with spaCy 3.x and add features like the `seed` argument. The original `spacy-langdetect` is less actively maintained and may not work correctly with newer spaCy versions.
fix
Migrate to `spacy-language-detection` for spaCy 3.x and later compatibility.
affects: All versions of `spacy-langdetect` when used with spaCy 3.x
Upgrade
Version history
0.2.1latest on PyPI · released Sep 8, 2021
Audit
Dependencies
spacyrequiredCore dependency for NLP pipeline integration; requires spaCy >= 3.0.
langdetectrequiredDefault underlying language detection library; installed automatically.
Agent activity
27 hits · last 30 days
node
18
OpenAI (training)
2
Amazon
1
Bingbot
1
Resources