Skip to main content

spaCy + Trankit

This package wraps the Trankit library, so you can use trankit models in a spaCy pipeline.

CI PyPi GitHub Code style: black

Using this wrapper, you'll be able to use the following annotations, computed by your pretrained trankit pipeline/model:

  • Statistical tokenization (reflected in the Doc and its tokens)
  • Lemmatization (token.lemma and token.lemma_)
  • Part-of-speech tagging (token.tag, token.tag_, token.pos, token.pos_)
  • Morphological analysis (token.morph)
  • Dependency parsing (token.dep, token.dep_, token.head)
  • Named entity recognition (doc.ents, token.ent_type, token.ent_type_, token.ent_iob, token.ent_iob_)
  • Sentence segmentation (doc.sents)
  • Multiword token preservation for languages such as Arabic and Hebrew via token._.trankit_expanded

️️️⌛️ Installation

As of v0.2.1 spacy-trankit is only compatible with spaCy v3.x. On Python 3.12, spacy-trankit applies a runtime compatibility patch for the current trankit dataclass issue in adapter_transformers before creating the pipeline. To install the most recent version:

pip install git+https://github.com/imvladikon/spacy-trankit

or from pypi:

pip install spacy-trankit

📖 Usage & Examples

Load pre-trained trankit model into a spaCy pipeline:

import spacy_trankit

# Initialize the pipeline
nlp = spacy_trankit.load("en")

doc = nlp("Barack Obama was born in Hawaii. He was elected president in 2008.")
for token in doc:
    print(token.text, token.lemma_, token.pos_, token.dep_, token.ent_type_)
print(doc.ents)

By default, mwt_strategy="auto" expands multiword tokens when the expanded tokens can be aligned back to the original text without changing doc.text. Expansions that cannot be represented as substrings of the original text are kept non-destructive. For example, Arabic and Hebrew clitic expansions can differ from the surface token, so the spaCy token keeps the original surface form and stores Trankit's expansion under token._.trankit_expanded.

doc = nlp("ذهبت للبيت اليوم")
for token in doc:
    print(token.text, token._.trankit_expanded)

If you always want surface tokens, pass mwt_strategy="preserve". If you need the previous expanded-token behavior and accept that spaCy may have to replace the original text with space-separated expanded tokens for unalignable cases, pass mwt_strategy="expand":

nlp = spacy_trankit.load("ar", mwt_strategy="preserve")
nlp = spacy_trankit.load("ar", mwt_strategy="expand")

Load it from the path:

import spacy_trankit

# Initialize the pipeline
nlp = spacy_trankit.load_from_path(name="en", path="./cache") 

doc = nlp("Barack Obama was born in Hawaii. He was elected president in 2008.")
for token in doc:
    print(token.text, token.lemma_, token.pos_, token.dep_, token.ent_type_)
print(doc.ents)

📦 Model downloads

The Trankit release on PyPI fetches its pretrained models from nlp.uoregon.edu, which is currently unavailable. spacy-trankit bypasses that broken download path and pulls the same artifacts from Trankit's HuggingFace mirror (https://huggingface.co/uonlp/trankit) into the local cache before instantiating the Trankit pipeline. The behaviour is automatic; no extra setup is needed.

If you mirror the artifacts elsewhere (e.g. for offline / air-gapped use), point spacy-trankit at it via the SPACY_TRANKIT_MODEL_URL environment variable. The template understands {version}, {embedding} and {lang}:

export SPACY_TRANKIT_MODEL_URL="https://my-mirror.example.com/trankit/{version}/{embedding}/{lang}.zip"

Metadata

Release files for spacy-trankit 0.2.3

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-trankit 0.2.3
File Size Uploaded Provenance Actions
spacy_trankit-0.2.3.tar.gz 15.2 kB With provenance Details for spacy_trankit-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spacy-trankit 0.2.3
File Interpreter ABI Platform Actions
spacy_trankit-0.2.3-py3-none-any.whl Python 3 none any Details for spacy_trankit-0.2.3-py3-none-any.whl

Total release size: 26.4 kB

Release files / spacy_trankit-0.2.3.tar.gz

Download URL spacy_trankit-0.2.3.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fd45e0c70a51b1c242671bc431ba5645941043fb3d697e65a7a25ec6bcf24fc4
BLAKE2b-256 checksum
How to use checksums
5aac84701a3c4a28ced7997829f48dd079f792f2fb14e85c4325f90bd2e4b604
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 5, 2026.

Transparency log

Release files / spacy_trankit-0.2.3-py3-none-any.whl

Download URL spacy_trankit-0.2.3-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f9c375a9d6dd661d38ebc038b99cacafb5665fe36ea5ea7ec9803673a1f33d8
BLAKE2b-256 checksum
How to use checksums
80450022467dd41b5a12e929618b35a27c5f2ca736eedd72d999851728014640
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 5, 2026.

Transparency log

Security for spacy-trankit 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for spacy_trankit-0.2.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files Full provenance

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