Natural Language Processing

Libraries for working with human languages.

Whether you're building an app or learning the field, there's a Python NLP library for it: spaCy for apps, NLTK for learning. Stanza covers many languages.

How to choose:

  • Production NLP pipelines: spaCy
  • Learning NLP, or lexical resources like WordNet: NLTK
  • Many languages, or CoreNLP from Python: Stanza
  • Topic models and word embeddings: gensim
  • Chinese word segmentation: jieba
  • Chinese characters to pinyin: pypinyin
  • Spaces between CJK text and letters or digits: pangu.py

Listed in editorial order, grouped by use case. Click a column to re-sort the whole list.

Press / to search. Tap a tag to filter. Click any row for details.

Search and filter

Results

Row number Tags

General 4 projects

A leading platform for building Python programs to work with human language data.
nltk/github.com/nltk/nltk / /43,656,887 downloads/month
A library for industrial-strength natural language processing in Python and Cython.
explosion/github.com/explosion/spaCy / /21,750,309 downloads/month
Topic Modeling for Humans.
piskvorky/github.com/piskvorky/gensim / /3,044,981 downloads/month
The Stanford NLP Group's official Python library, supporting 60+ languages.
stanfordnlp/github.com/stanfordnlp/stanza / /800,396 downloads/month

Chinese 3 projects

The most popular Chinese text segmentation library.
fxsjy/github.com/fxsjy/jieba / /4,829,766 downloads/month
Convert Chinese hanzi (漢字) to pinyin (拼音).
mozillazg/github.com/mozillazg/python-pinyin / /1,687,864 downloads/month
Paranoid text spacing.
vinta/github.com/vinta/pangu.py / /9,786 downloads/month

Natural Language Processing guide

spaCy is designed for production use: you build and train NLP pipelines, then package them to deploy. Its trained pipelines are Python packages. In an automated build, install them with pip from a direct link instead of spaCy's download command, and put that link in your requirements.txt. In a larger code base, import the pipeline as a module, so a missing one raises an ImportError right away. Run your texts through nlp.pipe in batches, and disable the components you don't need. To train your own pipeline, run spacy train with one config file.

NLTK comes with corpora and lexical resources such as WordNet, plus libraries to tokenize, tag, parse, and classify text. Its creators wrote a book that teaches NLP with it, and you can read it online. The book also says NLTK isn't highly optimized for runtime performance, so use it to learn and experiment. The data ships separately: get it with NLTK's data downloader, and set NLTK_DATA when you install it somewhere other than the standard locations.

Stanza is designed to work across many languages, using the Universal Dependencies formalism. It's also the official Python interface to Stanford's Java CoreNLP, though its own pipeline doesn't need CoreNLP. You can list the processors to load with processors=. Pass all your documents to the pipeline at once, since a for loop over one sentence at a time is very slow. For a lot of text, run it on a GPU. To keep the pipeline from downloading anything at runtime, download the models ahead of time.

jieba segments Chinese text into words: accurate mode suits text analysis, and search engine mode cuts long words into short ones for better recall. Add your own words with jieba.load_userdict() to get higher accuracy, and for Traditional Chinese, switch to its bigger dictionary with jieba.set_dictionary(). The other picks work with it: spaCy can use jieba as its Chinese segmenter, and Stanza supports it as a tokenizer.

pypinyin matches pinyin by whole words, so it handles characters with more than one reading. It also writes zhuyin (Bopomofo) and Wade-Giles. When a wrong word split gives a wrong reading, segment the text with jieba first and pass in the list of words. For readings that are still wrong, add your own with load_phrases_dict() or load_single_dict().

pangu.py inserts spaces between CJK characters and letters, digits, and symbols. Call pangu.space_text() on a string or pangu.space_file() on a file. From the command line, pangu-py -c prints the corrected text and exits with 1 when the spacing needed fixing.

Check the license of the models and data, not only the library. spaCy is MIT, but its Spanish pipelines are GPL and its Italian ones are for non-commercial use only. NLTK is Apache, and its corpora come under various licenses, each listed in its own README.

Know a project that belongs here?

Tell us what it does and why it stands out.