# kmike/pymorphy2 architecture

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

- Interactive diagram: https://gitdiagram.com/kmike/pymorphy2
- Repository: https://github.com/kmike/pymorphy2

## Overview

pymorphy2 is a Russian and Ukrainian morphological analyzer and inflection engine. A caller creates an analyzer, which loads a language dictionary and applies configured analysis units to words; results support grammatical-tag handling, normalization, lexeme retrieval, and inflection. A separate dictionary-preparation branch parses and preprocesses OpenCorpora data, compiles it into compact structures, and saves it for later loading. The map reflects README capabilities and indexed references; some runtime dispatch details are unsampled, so those links are omitted or marked unverified.

Main entry points: [`pymorphy2/analyzer.py`](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/analyzer.py) (Morphology API), [`pymorphy2/units`](https://github.com/kmike/pymorphy2/tree/master/pymorphy2/units) (Analysis units), [`pymorphy2/opencorpora_dict/compile.py`](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/compile.py) (Dictionary compiler), [`pymorphy2/lang/uk/config.py`](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/lang/uk/config.py) (Ukrainian config)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_analysis["Word analysis"]
  node_analyzer["Morphology API<br/>[analyzer.py]"]
  node_units["Analysis units"]
  node_base["Unit base behavior<br/>[base.py]"]
  node_tagset["Grammatical tags<br/>[tagset.py]"]
  node_shapes["Word shapes<br/>[shapes.py]"]
  node_cache["Result cache<br/>[cache.py]"]
end

subgraph group_language["Language behavior"]
  node_ru["Russian config<br/>[config.py]"]
  node_uk["Ukrainian config<br/>[config.py]"]
  node_ukprefix["Ukrainian prefixes<br/>[_prefixes.py]"]
end

subgraph group_dictionary["Dictionary data"]
  node_dictapi["Dictionary wrapper<br/>[wrapper.py]"]
  node_storage[("Dictionary storage<br/>[storage.py]")]
  node_dawg["DAWG structures<br/>[dawg.py]"]
  node_xmlparse["OpenCorpora parser<br/>[parse.py]"]
  node_preprocess["Dictionary cleanup<br/>[preprocess.py]"]
  node_compile["Dictionary compiler<br/>[compile.py]"]
  node_probability["Parse probabilities<br/>[probability.py]"]
end

subgraph group_interfaces["User interfaces"]
  node_cli["Command-line interface<br/>[cli.py]"]
  node_tokenizer["Tokenization<br/>[tokenizers.py]"]
end

node_caller(("Application caller"))

node_caller -->|"calls"| node_analyzer
node_analyzer -->|"loads dictionary"| node_dictapi
node_dictapi -->|"loads data"| node_storage
node_storage -->|"loads DAWGs"| node_dawg
node_analyzer -.->|"selects config"| node_ru
node_analyzer -.->|"selects config"| node_uk
node_ru -->|"configures"| node_units
node_uk -->|"configures"| node_units
node_uk -->|"uses prefixes"| node_ukprefix
node_analyzer -.->|"dispatches analysis"| node_units
node_units -->|"extends behavior"| node_base
node_units -->|"uses shape rules"| node_shapes
node_analyzer -->|"estimates parses"| node_probability
node_compile -->|"parses input"| node_xmlparse
node_compile -->|"preprocesses data"| node_preprocess
node_compile -->|"builds structures"| node_dawg
node_compile -->|"writes dictionary"| node_storage
node_cli -->|"uses API"| node_analyzer
node_cli -->|"tokenizes input"| node_tokenizer
node_units -.->|"creates tags"| node_tagset
node_analyzer -.->|"caches results"| node_cache

click node_analyzer "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/analyzer.py"
click node_units "https://github.com/kmike/pymorphy2/tree/master/pymorphy2/units"
click node_base "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/units/base.py"
click node_tagset "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/tagset.py"
click node_shapes "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/shapes.py"
click node_cache "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/cache.py"
click node_ru "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/lang/ru/config.py"
click node_uk "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/lang/uk/config.py"
click node_ukprefix "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/lang/uk/_prefixes.py"
click node_dictapi "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/wrapper.py"
click node_storage "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/storage.py"
click node_dawg "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/dawg.py"
click node_xmlparse "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/parse.py"
click node_preprocess "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/preprocess.py"
click node_compile "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/compile.py"
click node_probability "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/probability.py"
click node_cli "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/cli.py"
click node_tokenizer "https://github.com/kmike/pymorphy2/blob/master/pymorphy2/tokenizers.py"

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_analyzer,node_units,node_base,node_tagset,node_shapes,node_cache toneBlue
class node_ru,node_uk,node_ukprefix toneAmber
class node_dictapi,node_storage,node_dawg,node_xmlparse,node_preprocess,node_compile,node_probability toneMint
class node_cli,node_tokenizer toneRose
class node_caller toneIndigo
```

## Components

### Word analysis

- [Morphology API](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/analyzer.py): `pymorphy2/analyzer.py`
- [Analysis units](https://github.com/kmike/pymorphy2/tree/master/pymorphy2/units): `pymorphy2/units`
- [Unit base behavior](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/units/base.py): `pymorphy2/units/base.py`
- [Grammatical tags](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/tagset.py): `pymorphy2/tagset.py`
- [Word shapes](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/shapes.py): `pymorphy2/shapes.py`
- [Result cache](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/cache.py): `pymorphy2/cache.py`

### Language behavior

- [Russian config](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/lang/ru/config.py): `pymorphy2/lang/ru/config.py`
- [Ukrainian config](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/lang/uk/config.py): `pymorphy2/lang/uk/config.py`
- [Ukrainian prefixes](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/lang/uk/_prefixes.py): `pymorphy2/lang/uk/_prefixes.py`

### Dictionary data

- [Dictionary wrapper](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/wrapper.py): `pymorphy2/opencorpora_dict/wrapper.py`
- [Dictionary storage](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/storage.py): `pymorphy2/opencorpora_dict/storage.py`
- [DAWG structures](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/dawg.py): `pymorphy2/dawg.py`
- [OpenCorpora parser](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/parse.py): `pymorphy2/opencorpora_dict/parse.py`
- [Dictionary cleanup](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/preprocess.py): `pymorphy2/opencorpora_dict/preprocess.py`
- [Dictionary compiler](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/compile.py): `pymorphy2/opencorpora_dict/compile.py`
- [Parse probabilities](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/opencorpora_dict/probability.py): `pymorphy2/opencorpora_dict/probability.py`

### User interfaces

- [Command-line interface](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/cli.py): `pymorphy2/cli.py`
- [Tokenization](https://github.com/kmike/pymorphy2/blob/master/pymorphy2/tokenizers.py): `pymorphy2/tokenizers.py`

### Other components

- **Application caller**

## Connections

- Application caller → Morphology API: calls
- Morphology API → Dictionary wrapper: loads dictionary (evidence: `pymorphy2/analyzer.py`)
- Dictionary wrapper → Dictionary storage: loads data (evidence: `pymorphy2/opencorpora_dict/wrapper.py`)
- Dictionary storage → DAWG structures: loads DAWGs (evidence: `pymorphy2/opencorpora_dict/storage.py`)
- Morphology API → Russian config: selects config
- Morphology API → Ukrainian config: selects config
- Russian config → Analysis units: configures (evidence: `pymorphy2/lang/ru/config.py`)
- Ukrainian config → Analysis units: configures (evidence: `pymorphy2/lang/uk/config.py`)
- Ukrainian config → Ukrainian prefixes: uses prefixes (evidence: `pymorphy2/lang/uk/config.py`)
- Morphology API → Analysis units: dispatches analysis
- Analysis units → Unit base behavior: extends behavior (evidence: `pymorphy2/units/by_analogy.py`)
- Analysis units → Word shapes: uses shape rules (evidence: `pymorphy2/units/by_shape.py`)
- Morphology API → Parse probabilities: estimates parses (evidence: `pymorphy2/analyzer.py`)
- Dictionary compiler → OpenCorpora parser: parses input (evidence: `pymorphy2/opencorpora_dict/compile.py`)
- Dictionary compiler → Dictionary cleanup: preprocesses data (evidence: `pymorphy2/opencorpora_dict/compile.py`)
- Dictionary compiler → DAWG structures: builds structures (evidence: `pymorphy2/opencorpora_dict/compile.py`)
- Dictionary compiler → Dictionary storage: writes dictionary (evidence: `pymorphy2/opencorpora_dict/compile.py`)
- Command-line interface → Morphology API: uses API (evidence: `pymorphy2/cli.py`)
- Command-line interface → Tokenization: tokenizes input (evidence: `pymorphy2/cli.py`)
- Analysis units → Grammatical tags: creates tags
- Morphology API → Result cache: caches results
