spaCy NLP Library

repository·master·Indexed 12 days ago

https://github.com/explosion/spaCy

An industrial-strength Natural Language Processing (NLP) library for Python and Cython designed for production use. It provides state-of-the-art neural network models and transformer support for tasks including NER, parsing, and text classification across 70+ languages. Features include pretrained pipelines, a production-ready training system, and support for Large Language Models (LLMs).

Tokens
288.4K
Snippets
1.1K
Records
1.3K
Agent score
99%

What's inside spaCy

  1. Overview of spaCy v3.0 features

    master

    spaCy v3.0 introduces several major architectural shifts designed to bring industrial-strength NLP up to state-of-the-art accuracy and production readiness:

    • Transformer-based pipelines: Support for pretrained transformers to achieve high accuracy, including the ability to use multi-task learning by sharing a single transformer across multiple components.
    • Configurable Training & Custom Models: A new training workflow and configuration system that allows defining custom models using frameworks like PyTorch or TensorFlow.
    • Project Workflows: The projects system allows describing entire end-to-end workflows (from data preparation to production) in a single file.
    • Distributed Training: Support for parallel and distributed training using Ray.
    • Extensible Components: New built-in pipeline components and an improved API for creating custom pipeline components.
    • Dependency Matching: Enhanced capabilities for matching linguistic dependencies.
    • Python Type Hints: Improved developer experience through native Python type hints.
  2. Overview of spaCy NLP capabilities

    master

    spaCy is an industrial-strength library for advanced Natural Language Processing (NLP) in Python and Cython. It is designed for production use and supports tokenization and training for over 70 languages.

    Key features include:

    • Pretrained pipelines: Ready-to-use models for various NLP tasks.
    • Neural network models: State-of-the-art models for tagging, parsing, named entity recognition (NER), and text classification.
    • Transformer support: Multi-task learning with pretrained transformers like BERT.
    • Production-ready training system: Tools for training, model packaging, deployment, and workflow management.
  3. What is spaCy?

    master

    spaCy is a free, open-source Python library designed for advanced Natural Language Processing (NLP) in production environments. It is built to help developers build applications that process and understand large volumes of text, such as information extraction, natural language understanding, or pre-processing for deep learning.

    Key distinctions:

    • Not a platform/API: It is a library you use to build applications, not a SaaS or web service.
    • Not a chatbot engine: It provides text processing capabilities that can power chatbots, but is not a chatbot itself.
    • Not research software: Unlike NLTK or CoreNLP, spaCy is designed to be integrated and opinionated, offering specific, high-performance algorithms rather than a menu of many equivalent choices to simplify the developer experience.
  4. Overview of spaCy v2.0 features

    master

    spaCy v2.0 introduced deep learning-powered models for the tagger, parser, and entity recognizer. These models are designed to be 10× smaller and 20% more accurate than v1.x models.

    Key improvements include:

    • Deep Learning Models: Fully differentiable pipelines that support advanced training techniques like adversarial training, noise contrastive estimation, or reinforcement learning.
    • Stateless String Mapping: The string-to-integer mapping is no longer stateful, allowing for easier reconciliation of annotations across different processes.
    • Consistent Serialization: Improved APIs for saving and loading models, with full support for the Pickle protocol (useful for Apache Spark deployments).
    • Pipeline Extensibility: Custom pipeline components can modify the Doc at any stage, and users can add custom attributes, properties, and methods to Doc, Token, and Span objects.
    • Fixed Model Size: Due to the use of hashing, statistical models do not change size even when learning new vocabulary.
  5. Access spaCy documentation and resources

    master

    The spaCy documentation provides various resources depending on your needs:

    • Getting Started: Use [spaCy 101] for foundational knowledge.
    • Learning & Guides: Consult [Usage Guides] for feature implementation, [Project Templates] for end-to-end workflows, or the [Online Course] for interactive learning.
    • Technical Reference: Use the [API Reference] for detailed method signatures and the [Changelog] for version history.
    • Advanced Features: Explore [GPU Processing] for CUDA support, [Models] for pre-trained pipelines, and [Large Language Models] for LLM integration.
    • Ecosystem: Visit [Universe] for plugins and extensions, or use the [spaCy VS Code Extension] for working with configuration files.
    • Community & Updates: Follow the [Blog], [Videos], or [Live Stream] for news and tutorials.
  6. Core spaCy features

    master

    spaCy provides a wide range of NLP capabilities, including:

    FeatureDescription
    TokenizationSegmenting text into words, punctuation marks, etc.
    Part-of-speech (POS) TaggingAssigning word types to tokens (e.g., verb, noun).
    Dependency ParsingAssigning syntactic dependency labels to describe relations between tokens (e.g., subject, object).
    LemmatizationAssigning the base forms of words (e.g., "was" $\rightarrow$ "be").
    Sentence Boundary Detection (SBD)Finding and segmenting individual sentences.
    Named Entity Recognition (NER)Labeling real-world objects like persons, companies, or locations.
    Entity Linking (EL)Disambiguating entities to unique identifiers in a knowledge base.
    SimilarityComparing words, text spans, and documents.
    Text ClassificationAssigning categories to whole documents or parts of them.
    Rule-based MatchingFinding token sequences based on text and linguistic annotations.
    TrainingUpdating and improving statistical model predictions.
    SerializationSaving objects to files or byte strings.
  7. What is the Language class?

    master

    The Language class is the central object in spaCy, representing a text-processing pipeline. It is typically loaded once per process (often named nlp) and contains:

    • A shared vocabulary (Vocab).
    • Language data (linguistic features).
    • Optional binary weights (from trained models).
    • A processing pipeline consisting of components (like a tagger or parser) that are called sequentially on a Doc object.

    You can also add custom components to this pipeline that take a Doc, modify it, and return it.

  8. What is the EntityRuler and how does it work?

    master

    The EntityRuler is a pipeline component for rule-based named entity recognition (NER). It allows you to add spans to Doc.ents using token-based rules or exact phrase matches.

    You can use it in two ways:

    1. Combined with a statistical EntityRecognizer: To boost accuracy by combining machine learning with specific rules.
    2. Standalone: To implement a purely rule-based entity recognition system.

    When the EntityRuler makes predictions, they are accessible via Doc.ents. The labels are also stored in the underlying tokens in Token.ent_type and Token.ent_iob fields. Note that each token can only have one label.

    import spacy
    
    nlp = spacy.blank("en")
    ruler = nlp.add_pipe("entity_ruler")
    ruler.add_patterns([{"label": "ORG", "pattern": "Apple"}])
    
    doc = nlp("Apple is a company.")
    for ent in doc.ents:
        print(ent.text, ent.label_)
  9. What is the Vocab object?

    master

    The Vocab object is a storage class that provides a lookup table for accessing Lexeme objects and the StringStore. It owns the underlying C-data that is shared between Doc objects in a language model.

    Important Note: A Vocab instance is not static; it increases in size as new tokens are processed in texts. Some models may start with an empty vocabulary at initialization.

  10. What is SpanRuler?

    master

    The SpanRuler is a pipeline component used for rule-based span and named entity recognition (NER). It allows you to add spans to Doc.spans[spans_key] (as a SpanGroup) and/or to Doc.ents using token-based rules or exact phrase matches.

    When matches are assigned to Doc.ents, the annotations are stored in the Token.ent_type and Token.ent_iob fields.

  11. What is the Sentencizer and how does it work?

    master

    The Sentencizer is a rule-based pipeline component used for sentence boundary detection. Unlike the DependencyParser, which uses a statistical model, the Sentencizer uses a simpler strategy based on punctuation. This makes it a lightweight alternative when you don't need the full power of a dependency parser but still need to segment text into sentences.

    When applied to a Doc, it sets the following attributes:

    • Token.is_sent_start: A boolean indicating if a token starts a new sentence.
    • Doc.sents: An iterator over the detected sentences (Spans).
    from spacy.lang.en import English
    
    nlp = English()
    nlp.add_pipe("sentencizer")
    doc = nlp("This is a sentence. This is another sentence.")
    assert len(list(doc.sents)) == 2
  12. What is the AttributeRuler and when to use it

    master

    The AttributeRuler is a pipeline component used for rule-based token attribute assignment. It uses Matcher patterns to identify specific tokens and then sets their attributes (like POS, TAG, LEMMA, or MORPH).

    Common use cases include:

    • Handling exceptions: Overriding incorrect attributes for specific tokens.
    • Attribute mapping: Mapping fine-grained tags to coarse-grained tags (e.g., mapping specific POS tags to a broader category).
    • Morphological mapping: Mapping text and fine-grained tags to coarse-grained morphological features.

    It is a non-trainable component (api_trainable: false).