Registry /
ai-ml / spacy-language-detection
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
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.fixUse `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.fixInitialize `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.fixIf 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.fixMigrate 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.