# hankcs/hanlp architecture

> Architecture diagram of the GitHub repository hankcs/hanlp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hankcs/hanlp
- Repository: https://github.com/hankcs/hanlp

## Overview

HanLP is a multilingual NLP toolkit with PyTorch and TensorFlow implementations. Users load pretrained components or compose components into pipelines; task models then produce structured linguistic results such as tokenization, tags, parses, semantic roles, AMR, and similarity scores. The graph preserves those documented task families and highlights the shared component, document, and embedding layers. Some task implementations are represented by their exact source directories because their individual files were not sampled; links between those branches are omitted where the excerpts do not establish calls.

Main entry points: [`hanlp/components/pipeline.py`](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/components/pipeline.py) (Pipeline), [`hanlp/components/mtl/multi_task_learning.py`](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/components/mtl/multi_task_learning.py) (Multitask learning), [`hanlp/__init__.py`](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/__init__.py) (HanLP API), [`hanlp/components/tokenizers`](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/tokenizers) (Tokenization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry and composition"]
  node_api["HanLP API<br/>[__init__.py]"]
  node_registry["Pretrained models"]
  node_pipeline["Pipeline<br/>[pipeline.py]"]
  node_multitask["Multitask learning"]
end

subgraph group_linguistic["Linguistic analysis"]
  node_tokenization["Tokenization"]
  node_tagging["POS tagging"]
  node_ner["Named entities"]
  node_dependency["Dependency parsing"]
  node_constituency["Constituency parsing"]
end

subgraph group_semantic["Semantic tasks"]
  node_sdp["Semantic parsing"]
  node_srl["Semantic roles"]
  node_amr["AMR parsing"]
  node_sts["Text similarity"]
  node_classification["Classification"]
end

subgraph group_runtime["Shared runtime"]
  node_component["Component base<br/>[component.py]"]
  node_document["Document model<br/>[document.py]"]
  node_transform["Data transforms<br/>[transform.py]"]
  node_dataset["Dataset batching<br/>[dataset.py]"]
  node_embedding["Embeddings"]
end

node_user(("NLP user"))

node_user -->|"loads or composes"| node_api
node_api -->|"resolves models"| node_registry
node_api -->|"creates pipeline"| node_pipeline
node_pipeline -->|"wraps components"| node_component
node_pipeline -->|"reads and updates"| node_document
node_pipeline -.->|"dispatches task"| node_tokenization
node_pipeline -.->|"dispatches task"| node_tagging
node_pipeline -.->|"dispatches task"| node_ner
node_pipeline -.->|"dispatches task"| node_dependency
node_pipeline -.->|"dispatches task"| node_constituency
node_pipeline -.->|"dispatches task"| node_sdp
node_pipeline -.->|"dispatches task"| node_srl
node_pipeline -.->|"dispatches task"| node_amr
node_pipeline -.->|"dispatches task"| node_sts
node_pipeline -.->|"dispatches task"| node_classification
node_multitask -.->|"supports joint tasks"| node_tokenization
node_multitask -.->|"supports joint tasks"| node_tagging
node_multitask -.->|"supports joint tasks"| node_ner
node_multitask -.->|"supports joint tasks"| node_dependency
node_dataset -->|"applies transforms"| node_transform
node_embedding -->|"consumes transformed inputs"| node_transform

click node_api "https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/__init__.py"
click node_registry "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/pretrained"
click node_pipeline "https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/components/pipeline.py"
click node_component "https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/common/component.py"
click node_document "https://github.com/hankcs/hanlp/blob/doc-zh/plugins/hanlp_common/hanlp_common/document.py"
click node_tokenization "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/tokenizers"
click node_tagging "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/taggers"
click node_ner "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/ner"
click node_dependency "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/parsers"
click node_constituency "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/parsers/constituency"
click node_sdp "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/parsers/biaffine"
click node_srl "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/srl"
click node_amr "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/amr"
click node_sts "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/sts"
click node_classification "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/classifiers"
click node_multitask "https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/components/mtl/multi_task_learning.py"
click node_transform "https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/common/transform.py"
click node_dataset "https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/common/dataset.py"
click node_embedding "https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/layers/embeddings"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_registry,node_pipeline,node_multitask,node_user toneBlue
class node_tokenization,node_tagging,node_ner,node_dependency,node_constituency toneAmber
class node_sdp,node_srl,node_amr,node_sts,node_classification toneMint
class node_component,node_document,node_transform,node_dataset,node_embedding toneRose
```

## Components

### Entry and composition

- [HanLP API](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/__init__.py): `hanlp/__init__.py`
- [Pretrained models](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/pretrained): `hanlp/pretrained`
- [Pipeline](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/components/pipeline.py): `hanlp/components/pipeline.py`
- [Multitask learning](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/components/mtl/multi_task_learning.py): `hanlp/components/mtl/multi_task_learning.py`

### Linguistic analysis

- [Tokenization](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/tokenizers): `hanlp/components/tokenizers`
- [POS tagging](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/taggers): `hanlp/components/taggers`
- [Named entities](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/ner): `hanlp/components/ner`
- [Dependency parsing](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/parsers): `hanlp/components/parsers`
- [Constituency parsing](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/parsers/constituency): `hanlp/components/parsers/constituency`

### Semantic tasks

- [Semantic parsing](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/parsers/biaffine): `hanlp/components/parsers/biaffine`
- [Semantic roles](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/srl): `hanlp/components/srl`
- [AMR parsing](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/amr): `hanlp/components/amr`
- [Text similarity](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/sts): `hanlp/components/sts`
- [Classification](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/components/classifiers): `hanlp/components/classifiers`

### Shared runtime

- [Component base](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/common/component.py): `hanlp/common/component.py`
- [Document model](https://github.com/hankcs/hanlp/blob/doc-zh/plugins/hanlp_common/hanlp_common/document.py): `plugins/hanlp_common/hanlp_common/document.py`
- [Data transforms](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/common/transform.py): `hanlp/common/transform.py`
- [Dataset batching](https://github.com/hankcs/hanlp/blob/doc-zh/hanlp/common/dataset.py): `hanlp/common/dataset.py`
- [Embeddings](https://github.com/hankcs/hanlp/tree/doc-zh/hanlp/layers/embeddings): `hanlp/layers/embeddings`

### Other components

- **NLP user**

## Connections

- NLP user → HanLP API: loads or composes (evidence: `hanlp/__init__.py`)
- HanLP API → Pretrained models: resolves models (evidence: `hanlp/__init__.py`)
- HanLP API → Pipeline: creates pipeline (evidence: `hanlp/__init__.py`)
- Pipeline → Component base: wraps components (evidence: `hanlp/components/pipeline.py`)
- Pipeline → Document model: reads and updates (evidence: `hanlp/components/pipeline.py`)
- Pipeline → Tokenization: dispatches task
- Pipeline → POS tagging: dispatches task
- Pipeline → Named entities: dispatches task
- Pipeline → Dependency parsing: dispatches task
- Pipeline → Constituency parsing: dispatches task
- Pipeline → Semantic parsing: dispatches task
- Pipeline → Semantic roles: dispatches task
- Pipeline → AMR parsing: dispatches task
- Pipeline → Text similarity: dispatches task
- Pipeline → Classification: dispatches task
- Multitask learning → Tokenization: supports joint tasks
- Multitask learning → POS tagging: supports joint tasks
- Multitask learning → Named entities: supports joint tasks
- Multitask learning → Dependency parsing: supports joint tasks
- Dataset batching → Data transforms: applies transforms (evidence: `hanlp/common/dataset.py`)
- Embeddings → Data transforms: consumes transformed inputs (evidence: `hanlp/layers/embeddings/contextual_word_embedding.py`)
