Skip to main content

spacy-ngram

MIT License PyPI Version Python Versions Contributors Issues

spacy-ngram is a flexible SpaCy pipeline component for adding document- or sentence-level ngrams to your NLP pipeline. It extracts ngrams from lemmas (default) or tokens, handling stop words, punctuation, and digits automatically.

Table of Contents

About the Project

This component provides an easy way to enrich your Doc or Span objects with n-character or n-word sequences ( ngrams). It's designed to be lightweight and highly configurable.

Getting Started

Prerequisites

  • Python 3.10+
  • SpaCy 3.5.0+

Installation

  1. Install from PyPI:
pip install spacy-ngram
  1. Download a SpaCy model:
python -m spacy download en_core_web_sm

Usage

Quick Start

By default, the component adds unigrams and bigrams at the document level, filtering out stop words, punctuation, and digits.

import spacy
# the component is registered automatically on import
import spacy_ngram

nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacy-ngram')

text = 'Quark soup is an interacting localized assembly of quarks and gluons.'
doc = nlp(text)

print(doc._.ngram_1)
# ['quark', 'soup', 'interact', 'localize', 'assembly', 'quark', 'gluon']

print(doc._.ngram_2)
# ['quark_soup', 'soup_interact', 'interact_localize', 'localize_assembly', 'assembly_quark', 'quark_gluon']

Custom Configuration

You can customize the extension name, ngram sizes, and whether to include Boundary-of-Sentence (<BOS>) / End-of-Sentence (<EOS>) tags.

nlp.add_pipe('spacy-ngram', config={
    'extension_name': 'my_ngrams',
    'ngrams': (2, 3),  # Extract bi- and trigrams
    'sentence_level': True,  # Process each sentence individually
    'doc_level': True,  # Also process the entire document
    'include_bos': True,  # Include <BOS> tags
    'include_eos': True,  # Include <EOS> tags
})

doc = nlp("This is a test. This is only a test.")

# access document-level trigrams
print(doc._.my_ngrams_3)

# access sentence-level bigrams
for sent in doc.sents:
    print(sent._.my_ngrams_2)

Pipeline Parameters

Parameter Type Default Description
extension_name str 'ngram' Base name for the Doc/Span extensions
ngrams tuple[int] (1, 2) List of ngram sizes to extract
include_bos bool False include BOS tags at end of sentence/document
include_eos bool False include EOS tags at end of sentence/document
sentence_level bool False perform ngram-extraction at sentence-level
doc_level bool True perform ngram-extraction at document-level

Versions

Uses SEMVER. See Releases.

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are welcome!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Please use the issue tracker.

Metadata

Release files for spacy-ngram 1.0.1

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

Source distribution (sdist)

Source distribution for spacy-ngram 1.0.1
File Size Uploaded
spacy_ngram-1.0.1.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spacy-ngram 1.0.1
File Interpreter ABI Platform
spacy_ngram-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / spacy_ngram-1.0.1.tar.gz

Download URL spacy_ngram-1.0.1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6e9f0f8cd41e01c8843cff847eb3ac40812c4b14bb92fbf4cd575d0a3cd38d74
BLAKE2b-256 checksum
How to use checksums
f5f936322ad78df510d94e88e04998b058920af275047ee809007a0b97e7f454
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / spacy_ngram-1.0.1-py3-none-any.whl

Download URL spacy_ngram-1.0.1-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
031277f7f9afd10727d4d907cc6c004b27b6e55cc91820fbabf208a91a097d2e
BLAKE2b-256 checksum
How to use checksums
2fba806515aec0929dea3ef807f5693a8bad85c7f573a2a82f9197531ac93610
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.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