Skip to main content

spacy-langdetect

Fully customizable language detection pipeline for spaCy

Installation

pip install spacy-langdetect

NOTE:

Requires spaCy >= 2.0. This dependency is removed in pip install spacy-langdetect so that it can be used with nightly versions also

Basic usage

Out of the box, under the hood it uses langdetect to detect languages on spaCy's Doc and Span objects.

import spacy
from spacy_langdetect import LanguageDetector
nlp = spacy.load("en")
nlp.add_pipe(LanguageDetector(), name="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. Je m'appelle Angélica Summer, j'ai 12 ans et je suis canadienne."
doc = nlp(text)
# document level language detection. Think of it like average language of document!
print(doc._.language)
# sentence level language detection
for i, sent in enumerate(doc.sents):
    print(sent, sent._.language)

Using your own language detector

Suppose you are not happy with the accuracy of the out of the box language detector or you have your own language detector which you want to use with spaCy pipeline. How do you do it? That's where the language_detection_function argument comes in. The function takes in a Spacy Doc or Span object and can return any python object which is stored in doc._.language and span._.language. For example, let's say you want to use googletrans as your language detection module:

import spacy
from spacy.tokens import Doc, Span
from spacy_langdetect import LanguageDetector
# install using pip install googletrans
from googletrans import Translator
nlp = spacy.load("en")

def custom_detection_function(spacy_object):
    # custom detection function should take a Spacy Doc or a
    assert isinstance(spacy_object, Doc) or isinstance(
        spacy_object, Span), "spacy_object must be a spacy Doc or Span object but it is a {}".format(type(spacy_object))
    detection = Translator().detect(spacy_object.text)
    return {'language':detection.lang, 'score':detection.confidence}

nlp.add_pipe(LanguageDetector(language_detection_function=custom_detection_function), name="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. Je m'appelle Angélica Summer, j'ai 12 ans et je suis canadienne."
doc = nlp(text)
# document level language detection. Think of it like average language of document!
print(doc._.language)
# sentence level language detection
for i, sent in enumerate(doc.sents):
    print(sent, sent._.language)

Similarly you can also use pycld2 and other language detectors with spaCy

Metadata

Release files for spacy-langdetect 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for spacy-langdetect 0.1.2
File Interpreter ABI Platform
spacy_langdetect-0.1.2-py3-none-any.whl Python 3 none any Details

Release files / spacy_langdetect-0.1.2-py3-none-any.whl

Download URL spacy_langdetect-0.1.2-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb77878fb2445933cb6db836fc20a0d712fa685e1bd7e3b6a447d0556e54ebde
BLAKE2b-256 checksum
How to use checksums
297072dad19abe81ca8e85ff951da170915211d42d705a001d7e353af349a704
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.25.0 CPython/3.6.8

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page