spaCy: Industrial-strength NLP
spaCy is a library for advanced natural language processing in Python and Cython. spaCy is built on the very latest research, but it isn't researchware. It was designed from day one to be used in real products. spaCy currently supports English and German, as well as tokenization for Chinese, Spanish, Italian, French, Portuguese, Dutch, Swedish, Finnish, Hungarian and Bengali. It's commercial open-source software, released under the MIT license.
|Usage Workflows||How to use spaCy and its features.|
|API Reference||The detailed reference for spaCy's API.|
|Tutorials||End-to-end examples, with code you can modify and run.|
|Showcase & Demos||Demos, libraries and products from the spaCy community.|
|Contribute||How to contribute to the spaCy project and code base.|
💬 Where to ask questions
|Bug reports||GitHub Issue tracker|
|Usage questions||StackOverflow, Reddit usergroup, Gitter chat|
|General discussion||Reddit usergroup, Gitter chat|
- Non-destructive tokenization
- Syntax-driven sentence segmentation
- Pre-trained word vectors
- Part-of-speech tagging
- Named entity recognition
- Labelled dependency parsing
- Convenient string-to-int mapping
- Export to numpy data arrays
- GIL-free multi-threading
- Efficient binary serialization
- Easy deep learning integration
- Statistical models for English and German
- State-of-the-art speed
- Robust, rigorously evaluated accuracy
- Fastest in the world: <50ms per document. No faster system has ever been announced.
- Accuracy within 1% of the current state of the art on all tasks performed (parsing, named entity recognition, part-of-speech tagging). The only more accurate systems are an order of magnitude slower or more.
- CPython 2.6, 2.7, 3.3, 3.4, 3.5 (only 64 bit)
- macOS / OS X
- Windows (Cygwin, MinGW, Visual Studio)
spaCy is compatible with 64-bit CPython 2.6+/3.3+ and runs on Unix/Linux, OS X and Windows. Source packages are available via pip. Please make sure that you have a working build enviroment set up. See notes on Ubuntu, macOS/OS X and Windows for details.
When using pip it is generally recommended to install packages in a virtualenv to avoid modifying system state:
pip install spacy
Python packaging is awkward at the best of times, and it's particularly tricky with C extensions, built via Cython, requiring large data files. So, please report issues as you encounter them.
After installation you need to download a language model. Currently only models for
English and German, named
de, are available.
python -m spacy.en.download all python -m spacy.de.download all
The download command fetches about 1 GB of data which it installs
spacy package directory.
To upgrade spaCy to the latest release:
pip install -U spacy
Sometimes new releases require a new language model. Then you will have to upgrade to a new model, too. You can also force re-downloading and installing a new language model:
python -m spacy.en.download --force
Compile from source
The other way to install spaCy is to clone its GitHub repository and build it from source. That is the common way if you want to make changes to the code base.
You'll need to make sure that you have a development enviroment consisting of a Python distribution including header files, a compiler, pip, virtualenv and git installed. The compiler part is the trickiest. How to do that depends on your system. See notes on Ubuntu, OS X and Windows for details.
# make sure you are using recent pip/virtualenv versions python -m pip install -U pip virtualenv # find git install instructions at https://git-scm.com/downloads git clone https://github.com/explosion/spaCy.git cd spaCy virtualenv .env && source .env/bin/activate pip install -r requirements.txt pip install -e .
Compared to regular install via pip requirements.txt additionally installs developer dependencies such as cython.
Install system-level dependencies via
sudo apt-get install build-essential python-dev git
macOS / OS X
Install a recent version of XCode, including the so-called "Command Line Tools". macOS and OS X ship with Python and git preinstalled.
Install a version of Visual Studio Express or higher that matches the version that was used to compile your Python interpreter. For official distributions these are VS 2008 (Python 2.7), VS 2010 (Python 3.4) and VS 2015 (Python 3.5).
spaCy comes with an extensive test suite. First, find out where spaCy is installed:
python -c "import os; import spacy; print(os.path.dirname(spacy.__file__))"
pytest on that directory. The flags
--model are optional and enable additional tests:
# make sure you are using recent pytest version python -m pip install -U pytest python -m pytest <spacy-directory> --vectors --model --slow
Download model to custom location
You can specify where
spacy.de.download download the language model
to using the
python -m spacy.en.download all --data-path /some/dir
If you choose to download to a custom location, you will need to tell spaCy where to load the model
from in order to use it. You can do this either by calling
spacy.load(), or by passing a
path argument to the
v1.6.0: Improvements to tokenizer and tests2017-01-16
- Updated token exception handling mechanism to allow the usage of arbitrary functions as token exception matchers.
- Improve how tokenizer exceptions for English contractions and punctuations are generated.
- Update language data for Hungarian and Swedish tokenization.
- Update to use Thinc v6 to prepare for spaCy v2.0.
- Fix issue #326: Tokenizer is now more consistent and handles abbreviations correctly.
- Fix issue #344: Tokenizer now handles URLs correctly.
- Fix issue #483: Period after two or more uppercase letters is split off in tokenizer exceptions.
- Fix issue #631: Add
- Fix issue #718: Contractions with
Sheare now handled correctly.
- Fix issue #736: Times are now tokenized with correct string values.
- Fix issue #743:
Tokenis now hashable.
- Fix issue #744:
Wereare now excluded correctly from contractions.
- Modernise and reorganise all tests and remove model dependencies where possible.
- Improve test speed to ~20s for basic tests (from previously >80s) and ~100s including models (from previously >200s).
- Add fixtures for spaCy components and test utilities, e.g. to create
- Add documentation for tests to explain conventions and organisation.
v1.5.0: Alpha support for Swedish and Hungarian2016-12-27
- NEW: Alpha support for Swedish tokenization.
- NEW: Alpha support for Hungarian tokenization.
- Update language data for Spanish tokenization.
- Speed up tokenization when no data is preloaded by caching the first 10,000 vocabulary items seen.
- List the
language_datapackage in the
- Fix missing
vec_pathdeclaration that was failing if
Vocabto load without
- NEW: spaCy Jupyter notebooks repo: ongoing collection of easy-to-run spaCy examples and tutorials.
- Fix issue #657: Generalise dependency parsing annotation specs beyond English.
- Fix various typos and inconsistencies.
v1.4.0: Improved language data and alpha Dutch support2016-12-18
- NEW: Alpha support for Dutch tokenization.
- Reorganise and improve format for language data.
- Add shared tag map, entity rules, emoticons and punctuation to language data.
- Convert entity rules, morphological rules and lemmatization rules from JSON to Python.
- Update language data for English, German, Spanish, French, Italian and Portuguese.
- Fix issue #649: Update and reorganise stop lists.
- Fix issue #672: Make
- Fix issue #674: Add missing lemmas for contracted forms of "be" to
- Fix issue #683
Morphologyclass now supplies tag map value for the special space tag if it's missing.
- Fix issue #684: Ensure
spacy.en.English()loads the Glove vector data if available. Previously was inconsistent with behaviour of
- Fix issue #685: Expand
TOKENIZER_EXCEPTIONSwith unicode apostrophe (
- Fix issue #689: Correct typo in
- Fix issue #691: Add tokenizer exceptions for "gonna" and "Gonna".
No changes to the public, documented API, but the previously undocumented language data and model initialisation processes have been refactored and reorganised. If you were relying on the
bin/init_model.py script, see the new spaCy Developer Resources repo. Code that references internals of the
spacy.de packages should also be reviewed before updating to this version.
- NEW: "Adding languages" workflow.
- NEW: "Part-of-speech tagging" workflow.
- NEW: spaCy Developer Resources repo – scripts, tools and resources for developing spaCy.
- Fix various typos and inconsistencies.
v1.3.0: Improve API consistency2016-12-03
- #658: Add
Span.noun_chunksiterator (thanks @pokey).
- #642: Let
--data-pathbe specified when running download.py scripts (thanks @ExplodingCabbage).
- #638: Add German stopwords (thanks @souravsingh).
- #614: Fix
PhraseMatcherto work with new
- Fix issue #605:
Matchernow rejects matches as expected.
- Fix issue #617:
Vocab.load()now works with string paths, as well as
- Fix issue #639: Stop words in
Languageclass now used as expected.
- Fix issues #656, #624:
Tokenizerspecial-case rules now support arbitrary token attributes.
- Add "Customizing the tokenizer" workflow.
- Add "Training the tagger, parser and entity recognizer" workflow.
- Add "Entity recognition" workflow.
- Fix various typos and inconsistencies.
Thanks to @pokey, @ExplodingCabbage, @souravsingh, @sadovnychyi, @manojsakhwar, @TiagoMRodrigues, @savkov, @pspiegelhalter, @chenb67, @kylepjohnson, @YanhaoYang, @tjrileywisc, @dechov, @wjt, @jsmootiv and @blarghmatey for the pull requests!
v1.2.0: Alpha tokenizers for Chinese, French, Spanish, Italian and Portuguese2016-11-04
- NEW: Support Chinese tokenization, via Jieba.
- NEW: Alpha support for French, Spanish, Italian and Portuguese tokenization.
- Fix issue #376: POS tags for "and/or" are now correct.
- Fix issue #578:
--forceargument on download command now operates correctly.
- Fix issue #595: Lemmatization corrected for some base forms.
- Fix issue #588: Matcher now rejects empty patterns.
- Fix issue #592: Added exception rule for tokenization of "Ph.D."
- Fix issue #599: Empty documents now considered tagged and parsed.
- Fix issue #600: Add missing
- Fix issue #596: Added missing unicode import when compiling regexes that led to incorrect tokenization.
- Fix issue #587: Resolved bug that caused
Matcherto sometimes segfault.
- Fix issue #429: Ensure missing entity types are added to the entity recognizer.
v1.1.0: Bug fixes and adjustments2016-10-23
- Rename new
pipelinekeyword argument of
- Rename new
vectorskeyword argument of
- Fix issue #544: Add
vocab.resize_vectors()method, to support changing to vectors of different dimensionality.
- Fix issue #536: Default probability was incorrect for OOV words.
- Fix issue #539: Unspecified encoding when opening some JSON files.
- Fix issue #541: GloVe vectors were being loaded incorrectly.
- Fix issue #522: Similarities and vector norms were calculated incorrectly.
- Fix issue #461:
ent_iobattribute was incorrect after setting entities via
- Fix issue #459: Deserialiser failed on empty doc
- Fix issue #514: Serialization failed after adding a new entity label.
v1.0.0: Support for deep learning workflows and entity-aware rule matcher2016-10-18
- NEW: custom processing pipelines, to support deep learning workflows
- NEW: Rule matcher now supports entity IDs and attributes
- NEW: Official/documented training APIs and GoldParse class
- Download and use GloVe vectors by default
- Make it easier to load and unload word vectors
- Improved rule matching functionality
- Move basic data into the code, rather than the json files. This makes it simpler to use the tokenizer without the models installed, and makes adding new languages much easier.
- Replace file-system strings with
Pathobjects. You can now load resources over your network, or do similar trickery, by passing any object that supports the
- The data_dir keyword argument of
Language.__init__(and its subclasses
German.__init__) has been renamed to
- Details of how the Language base-class and its sub-classes are loaded, and how defaults are accessed, have been heavily changed. If you have your own subclasses, you should review the changes.
- The deprecated
token.repvecname has been removed.
.train()method of Tagger and Parser has been renamed to
- The previously undocumented
GoldParseclass has a new
__init__()method. The old method has been preserved in
- Previously undocumented details of the
Parserclass have changed.
- The previously undocumented
get_package_by_namehelper functions have been moved into a new module,
spacy.deprecated, in case you still need them while you update.
get_lang_classbug when GloVe vectors are used.
- Fix Issue #411:
doc.sentsraised IndexError on empty string.
- Fix Issue #455: Correct lemmatization logic
- Fix Issue #371: Make
- Fix Issue #469: Make
noun_chunksdetect root NPs
v0.101.0: Fixed German model2016-05-10
- Fixed bug that prevented German parses from being deprojectivised.
- Bug fixes to sentence boundary detection.
- Add rich comparison methods to the Lexeme class.
- Add missing
- Add missing
spaCy finally supports another language, in addition to English. We're lucky
to have Wolfgang Seeker on the team, and the new German model is just the
beginning. Now that there are multiple languages, you should consider loading
spaCy via the
load() function. This function also makes it easier to load extra
word vector data for English:
import spacy en_nlp = spacy.load('en', vectors='en_glove_cc_300_1m_vectors') de_nlp = spacy.load('de')
To support use of the load function, there are also two new helper functions:
spacy.set_lang_class. Once the German model is
loaded, you can use it just like the English model:
doc = nlp(u'''Wikipedia ist ein Projekt zum Aufbau einer Enzyklopädie aus freien Inhalten, zu dem du mit deinem Wissen beitragen kannst. Seit Mai 2001 sind 1.936.257 Artikel in deutscher Sprache entstanden.''') for sent in doc.sents: print(sent.root.text, sent.root.n_lefts, sent.root.n_rights) # (u'ist', 1, 2) # (u'sind', 1, 3)
The German model provides tokenization, POS tagging, sentence boundary detection, syntactic dependency parsing, recognition of organisation, location and person entities, and word vector representations trained on a mix of open subtitles and Wikipedia data. It doesn't yet provide lemmatisation or morphological analysis, and it doesn't yet recognise numeric entities such as numbers and dates.
- spaCy < 0.100.7 had a bug in the semantics of the
Token.__unicode__built-ins: they included a trailing space.
- Improve handling of "infixed" hyphens. Previously the tokenizer struggled with multiple hyphens, such as "well-to-do".
- Improve handling of periods after mixed-case tokens
- Improve lemmatization for English special-case tokens
- Fix bug that allowed spaces to be treated as heads in the syntactic parse
- Fix bug that led to inconsistent sentence boundaries before and after serialisation.
- Fix bug from deserialising untagged documents.
v0.100.6: Add support for GloVe vectors2016-03-08
This release offers improved support for replacing the word vectors used by spaCy. To install Stanford's GloVe vectors, trained on the Common Crawl, just run:
sputnik --name spacy install en_glove_cc_300_1m_vectors
To reduce memory usage and loading time, we've trimmed the vocabulary down to 1m entries.
This release also integrates all the code necessary for German parsing. A German model
will be released shortly. To assist in multi-lingual processing, we've added a
function. To load the English model with the GloVe vectors:
Fix incorrect use of header file, caused from problem with thinc
v0.100.4: Fix OSX problem introduced in 0.100.32016-02-07
Small correction to right_edge calculation
Support multi-threading, via the
.pipe method. spaCy now releases the GIL around the
parser and entity recognizer, so systems that support OpenMP should be able to do
shared memory parallelism at close to full efficiency.
We've also greatly reduced loading time, and fixed a number of bugs.
Fix data version lock that affected v0.100.1
v0.100.1: Fix install for OSX2016-01-21
v0.100 included header files built on Linux that caused installation to fail on OSX. This should now be corrected. We also update the default data distribution, to include a small fix to the tokenizer.
v0.100: Revise setup.py, better model downloads, bug fixes2016-01-19
- Redo setup.py, and remove ugly headers_workaround hack. Should result in fewer install problems.
- Update data downloading and installation functionality, by migrating to the Sputnik data-package manager. This will allow us to offer finer grained control of data installation in future.
- Fix bug when using custom entity types in
Matcher. This should work by default when using the
English.__call__method of running the pipeline. If invoking
Parser.__call__directly to do NER, you should call the
Parser.add_label()method to register your entity type.
- Fix head-finding rules in
- Fix problem that caused
doc.merge()to sometimes hang
- Fix problems in handling of whitespace
v0.99: Improve span merging, internal refactoring2015-11-08
- Merging multi-word tokens into one, via the
span.merge()methods, no longer invalidates existing
Spanobjects. This makes it much easier to merge multiple spans, e.g. to merge all named entities, or all base noun phrases. Thanks to @andreasgrv for help on this patch.
- Lots of internal refactoring, especially around the machine learning module, thinc. The thinc API has now been improved, and the spacy._ml wrapper module is no longer necessary.
- The lemmatizer now lower-cases non-noun, noun-verb and non-adjective words.
- A new attribute,
.rank, is added to Token and Lexeme objects, giving the frequency rank of the word.
v0.98: Smaller package, bug fixes2015-11-03
- Remove binary data from PyPi package.
- Delete archive after downloading data
- Use updated cymem, preshed and thinc packages
- Fix information loss in deserialize
__str__methods for Python2
v0.97: Load the StringStore from a json list, instead of a text file2015-10-23
- Fix bugs in download.py
--forceto over-write the data directory in download.py
- Fix bugs in
v0.96: Hotfix to .merge method2015-10-19
- Fix bug that caused text to be lost after
- Fix bug in Matcher when matched entities overlapped
- Reform encoding of symbols
- Fix bugs in
- Fix bugs in
- Add tokenizer rule to fix numeric range tokenization
- Add specific string-length cap in Tokenizer
- Fix memory error that caused crashes on 32bit platforms
- Fix parse errors caused by smart quotes and em-dashes
Bug fixes to word vectors