PyMorphy3
PyMorphy3 is a morphological analyzer (POS tagger + inflection engine) for the Russian language. It provides tools for parsing words, getting normal forms, and inflecting words into various grammatical forms. The current version is 2.0.6, and the library is actively maintained with frequent minor releases to support new Python versions and address issues.
Common errors
-
AttributeError: 'MorphAnalyzer' object has no attribute 'restore_word_case'
cause The `restore_word_case` function was removed in PyMorphy3 version 2.0.5.fixUpdate your code to remove any calls to `morph.restore_word_case()`. -
NoDictionariesForLanguage: No dictionaries for language "ru" are installed. Run 'pip install pymorphy3-dicts-ru'.
cause The required Russian dictionary package `pymorphy3-dicts-ru` is not installed.fixInstall the dictionary package: `pip install pymorphy3-dicts-ru`. -
ImportError: cannot import name 'MorphAnalyzer' from 'pymorphy3.analyzer' (C:\...\site-packages\pymorphy3\analyzer.py)
cause You are using an old import path for `MorphAnalyzer`. It's now available directly under the `pymorphy3` package.fixChange your import statement from `from pymorphy3.analyzer import MorphAnalyzer` to `from pymorphy3 import MorphAnalyzer`. -
ModuleNotFoundError: No module named 'dawg'
cause This error typically indicates that an underlying dependency for the data structures used by PyMorphy3 is missing, or the main `pymorphy3` package wasn't installed correctly.fixEnsure `pymorphy3` is properly installed: `pip install pymorphy3 pymorphy3-dicts-ru`.
Warnings
- breaking Support for Python 2 and Python 3.5 was officially dropped in version 2.0.0. If you are using these Python versions, you must stick to `pymorphy3 < 2.0.0` or upgrade your Python environment.
- breaking The `restore_word_case` function was removed in version 2.0.5 due to cleanup and deprecation. Code relying on this function will fail.
- gotcha For improved performance, especially with large texts, PyMorphy3 offers C-extension based 'fast' extras. These require Python 3.9+ and installing with `pymorphy3[fast]`.
- gotcha The command-line interface (CLI) dependency was switched from `docopt` to `Click` in version 1.3.0, making `Click` an optional dependency. If you use `pymorphy3` from the command line, you might encounter issues if `Click` is not installed.
Install
-
pip install pymorphy3 pymorphy3-dicts-ru -
pip install 'pymorphy3[fast]' pymorphy3-dicts-ru
Imports
- MorphAnalyzer
from pymorphy3.analyzer import MorphAnalyzer
from pymorphy3 import MorphAnalyzer
Quickstart
import pymorphy3
morph = pymorphy3.MorphAnalyzer()
# Parse a word
word_analysis = morph.parse('стали')[0]
print(f"Word: {word_analysis.word}")
print(f"Tag: {word_analysis.tag}")
print(f"Normal form: {word_analysis.normal_form}")
# Inflect a word to a different form
inflected_word = word_analysis.inflect({'sing', 'datv'})
if inflected_word:
print(f"Inflected form (singular, dative): {inflected_word.word}")